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 smbXsrv_connection *xconn = req->xconn;
71 struct smbd_server_connection *sconn = xconn->sconn;
72 int max_send = xconn->smb1.sessions.max_send;
75 * If there genuinely are no parameters or data to send just send
79 if(params_to_send == 0 && data_to_send == 0) {
80 reply_outbuf(req, 18, 0);
81 if (NT_STATUS_V(nt_error)) {
82 error_packet_set((char *)req->outbuf,
86 show_msg((char *)req->outbuf);
87 if (!srv_send_smb(sconn,
90 IS_CONN_ENCRYPTED(conn),
92 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
94 TALLOC_FREE(req->outbuf);
99 * When sending params and data ensure that both are nicely aligned.
100 * Only do this alignment when there is also data to send - else
101 * can cause NT redirector problems.
104 if (((params_to_send % 4) != 0) && (data_to_send != 0)) {
105 data_alignment_offset = 4 - (params_to_send % 4);
109 * Space is bufsize minus Netbios over TCP header minus SMB header.
110 * The alignment_offset is to align the param bytes on a four byte
111 * boundary (2 bytes for data len, one byte pad).
112 * NT needs this to work correctly.
115 useable_space = max_send - (smb_size
118 + data_alignment_offset);
120 if (useable_space < 0) {
121 char *msg = talloc_asprintf(
123 "send_nt_replies failed sanity useable_space = %d!!!",
125 DEBUG(0, ("%s\n", msg));
126 exit_server_cleanly(msg);
129 while (params_to_send || data_to_send) {
132 * Calculate whether we will totally or partially fill this packet.
135 total_sent_thistime = params_to_send + data_to_send;
138 * We can never send more than useable_space.
141 total_sent_thistime = MIN(total_sent_thistime, useable_space);
143 reply_outbuf(req, 18,
144 total_sent_thistime + alignment_offset
145 + data_alignment_offset);
148 * Set total params and data to be sent.
151 SIVAL(req->outbuf,smb_ntr_TotalParameterCount,paramsize);
152 SIVAL(req->outbuf,smb_ntr_TotalDataCount,datasize);
155 * Calculate how many parameters and data we can fit into
156 * this packet. Parameters get precedence.
159 params_sent_thistime = MIN(params_to_send,useable_space);
160 data_sent_thistime = useable_space - params_sent_thistime;
161 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
163 SIVAL(req->outbuf, smb_ntr_ParameterCount,
164 params_sent_thistime);
166 if(params_sent_thistime == 0) {
167 SIVAL(req->outbuf,smb_ntr_ParameterOffset,0);
168 SIVAL(req->outbuf,smb_ntr_ParameterDisplacement,0);
171 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
172 * parameter bytes, however the first 4 bytes of outbuf are
173 * the Netbios over TCP header. Thus use smb_base() to subtract
174 * them from the calculation.
177 SIVAL(req->outbuf,smb_ntr_ParameterOffset,
178 ((smb_buf(req->outbuf)+alignment_offset)
179 - smb_base(req->outbuf)));
181 * Absolute displacement of param bytes sent in this packet.
184 SIVAL(req->outbuf, smb_ntr_ParameterDisplacement,
189 * Deal with the data portion.
192 SIVAL(req->outbuf, smb_ntr_DataCount, data_sent_thistime);
194 if(data_sent_thistime == 0) {
195 SIVAL(req->outbuf,smb_ntr_DataOffset,0);
196 SIVAL(req->outbuf,smb_ntr_DataDisplacement, 0);
199 * The offset of the data bytes is the offset of the
200 * parameter bytes plus the number of parameters being sent this time.
203 SIVAL(req->outbuf, smb_ntr_DataOffset,
204 ((smb_buf(req->outbuf)+alignment_offset) -
205 smb_base(req->outbuf))
206 + params_sent_thistime + data_alignment_offset);
207 SIVAL(req->outbuf,smb_ntr_DataDisplacement, pd - pdata);
211 * Copy the param bytes into the packet.
214 if(params_sent_thistime) {
215 if (alignment_offset != 0) {
216 memset(smb_buf(req->outbuf), 0,
219 memcpy((smb_buf(req->outbuf)+alignment_offset), pp,
220 params_sent_thistime);
224 * Copy in the data bytes
227 if(data_sent_thistime) {
228 if (data_alignment_offset != 0) {
229 memset((smb_buf(req->outbuf)+alignment_offset+
230 params_sent_thistime), 0,
231 data_alignment_offset);
233 memcpy(smb_buf(req->outbuf)+alignment_offset
234 +params_sent_thistime+data_alignment_offset,
235 pd,data_sent_thistime);
238 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
239 params_sent_thistime, data_sent_thistime, useable_space));
240 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
241 params_to_send, data_to_send, paramsize, datasize));
243 if (NT_STATUS_V(nt_error)) {
244 error_packet_set((char *)req->outbuf,
249 /* Send the packet */
250 show_msg((char *)req->outbuf);
251 if (!srv_send_smb(sconn,
254 IS_CONN_ENCRYPTED(conn),
256 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
259 TALLOC_FREE(req->outbuf);
261 pp += params_sent_thistime;
262 pd += data_sent_thistime;
264 params_to_send -= params_sent_thistime;
265 data_to_send -= data_sent_thistime;
271 if(params_to_send < 0 || data_to_send < 0) {
272 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
273 params_to_send, data_to_send));
274 exit_server_cleanly("send_nt_replies: internal error");
279 /****************************************************************************
280 Reply to an NT create and X call on a pipe
281 ****************************************************************************/
283 static void nt_open_pipe(char *fname, connection_struct *conn,
284 struct smb_request *req, uint16_t *ppnum)
289 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
291 /* Strip \\ off the name if present. */
292 while (fname[0] == '\\') {
296 status = open_np_file(req, fname, &fsp);
297 if (!NT_STATUS_IS_OK(status)) {
298 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
299 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
303 reply_nterror(req, status);
311 /****************************************************************************
312 Reply to an NT create and X call for pipes.
313 ****************************************************************************/
315 static void do_ntcreate_pipe_open(connection_struct *conn,
316 struct smb_request *req)
319 uint16_t pnum = FNUM_FIELD_INVALID;
321 uint32 flags = IVAL(req->vwv+3, 1);
322 TALLOC_CTX *ctx = talloc_tos();
324 srvstr_pull_req_talloc(ctx, req, &fname, req->buf, STR_TERMINATE);
327 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
331 nt_open_pipe(fname, conn, req, &pnum);
339 * Deal with pipe return.
342 if (flags & EXTENDED_RESPONSE_REQUIRED) {
343 /* This is very strange. We
344 * return 50 words, but only set
345 * the wcnt to 42 ? It's definitely
346 * what happens on the wire....
348 reply_outbuf(req, 50, 0);
349 SCVAL(req->outbuf,smb_wct,42);
351 reply_outbuf(req, 34, 0);
354 SSVAL(req->outbuf, smb_vwv0, 0xff); /* andx chain ends */
355 SSVAL(req->outbuf, smb_vwv1, 0); /* no andx offset */
357 p = (char *)req->outbuf + smb_vwv2;
361 SIVAL(p,0,FILE_WAS_OPENED);
364 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
367 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
369 SSVAL(p,2, 0x5FF); /* ? */
372 if (flags & EXTENDED_RESPONSE_REQUIRED) {
374 SIVAL(p,0,FILE_GENERIC_ALL);
376 * For pipes W2K3 seems to return
378 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
380 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
383 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
386 struct case_semantics_state {
387 connection_struct *conn;
390 bool short_case_preserve;
393 /****************************************************************************
394 Restore case semantics.
395 ****************************************************************************/
397 static int restore_case_semantics(struct case_semantics_state *state)
399 state->conn->case_sensitive = state->case_sensitive;
400 state->conn->case_preserve = state->case_preserve;
401 state->conn->short_case_preserve = state->short_case_preserve;
405 /****************************************************************************
407 ****************************************************************************/
409 static struct case_semantics_state *set_posix_case_semantics(TALLOC_CTX *mem_ctx,
410 connection_struct *conn)
412 struct case_semantics_state *result;
414 if (!(result = talloc(mem_ctx, struct case_semantics_state))) {
419 result->case_sensitive = conn->case_sensitive;
420 result->case_preserve = conn->case_preserve;
421 result->short_case_preserve = conn->short_case_preserve;
424 conn->case_sensitive = True;
425 conn->case_preserve = True;
426 conn->short_case_preserve = True;
428 talloc_set_destructor(result, restore_case_semantics);
433 /****************************************************************************
434 Reply to an NT create and X call.
435 ****************************************************************************/
437 void reply_ntcreate_and_X(struct smb_request *req)
439 connection_struct *conn = req->conn;
440 struct smb_filename *smb_fname = NULL;
444 uint32 file_attributes;
446 uint32 create_disposition;
447 uint32 create_options;
449 uint64_t allocation_size;
450 /* Breakout the oplock request bits so we can set the
451 reply bits separately. */
455 files_struct *fsp = NULL;
457 struct timespec create_timespec;
458 struct timespec c_timespec;
459 struct timespec a_timespec;
460 struct timespec m_timespec;
463 uint8_t oplock_granted = NO_OPLOCK_RETURN;
464 struct case_semantics_state *case_state = NULL;
465 TALLOC_CTX *ctx = talloc_tos();
467 START_PROFILE(SMBntcreateX);
470 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
474 flags = IVAL(req->vwv+3, 1);
475 access_mask = IVAL(req->vwv+7, 1);
476 file_attributes = IVAL(req->vwv+13, 1);
477 share_access = IVAL(req->vwv+15, 1);
478 create_disposition = IVAL(req->vwv+17, 1);
479 create_options = IVAL(req->vwv+19, 1);
480 root_dir_fid = (uint16)IVAL(req->vwv+5, 1);
482 allocation_size = BVAL(req->vwv+9, 1);
484 srvstr_get_path_req(ctx, req, &fname, (const char *)req->buf,
485 STR_TERMINATE, &status);
487 if (!NT_STATUS_IS_OK(status)) {
488 reply_nterror(req, status);
492 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
493 "file_attributes = 0x%x, share_access = 0x%x, "
494 "create_disposition = 0x%x create_options = 0x%x "
495 "root_dir_fid = 0x%x, fname = %s\n",
497 (unsigned int)access_mask,
498 (unsigned int)file_attributes,
499 (unsigned int)share_access,
500 (unsigned int)create_disposition,
501 (unsigned int)create_options,
502 (unsigned int)root_dir_fid,
506 * we need to remove ignored bits when they come directly from the client
507 * because we reuse some of them for internal stuff
509 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
512 * If it's an IPC, use the pipe handler.
516 if (lp_nt_pipe_support()) {
517 do_ntcreate_pipe_open(conn, req);
520 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
524 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
525 if (oplock_request) {
526 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
530 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
531 case_state = set_posix_case_semantics(ctx, conn);
533 reply_nterror(req, NT_STATUS_NO_MEMORY);
538 status = filename_convert(ctx,
540 req->flags2 & FLAGS2_DFS_PATHNAMES,
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 UCF_PREP_CREATEFILE,
1074 TALLOC_FREE(case_state);
1076 if (!NT_STATUS_IS_OK(status)) {
1077 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1078 reply_botherror(req,
1079 NT_STATUS_PATH_NOT_COVERED,
1080 ERRSRV, ERRbadpath);
1083 reply_nterror(req, status);
1087 /* Ensure the data_len is correct for the sd and ea values given. */
1088 if ((ea_len + sd_len > data_count)
1089 || (ea_len > data_count) || (sd_len > data_count)
1090 || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
1091 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
1092 "%u, data_count = %u\n", (unsigned int)ea_len,
1093 (unsigned int)sd_len, (unsigned int)data_count));
1094 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1099 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
1102 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
1104 if (!NT_STATUS_IS_OK(status)) {
1105 DEBUG(10, ("call_nt_transact_create: "
1106 "unmarshall_sec_desc failed: %s\n",
1107 nt_errstr(status)));
1108 reply_nterror(req, status);
1114 if (!lp_ea_support(SNUM(conn))) {
1115 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
1116 "EA's not supported.\n",
1117 (unsigned int)ea_len));
1118 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
1123 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
1124 "too small (should be more than 10)\n",
1125 (unsigned int)ea_len ));
1126 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1130 /* We have already checked that ea_len <= data_count here. */
1131 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
1133 if (ea_list == NULL) {
1134 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1138 if (ea_list_has_invalid_name(ea_list)) {
1139 /* Realloc the size of parameters and data we will return */
1140 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1141 /* Extended response is 32 more byyes. */
1146 params = nttrans_realloc(ppparams, param_len);
1147 if(params == NULL) {
1148 reply_nterror(req, NT_STATUS_NO_MEMORY);
1152 memset(params, '\0', param_len);
1153 send_nt_replies(conn, req, STATUS_INVALID_EA_NAME,
1154 params, param_len, NULL, 0);
1159 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1160 if (oplock_request) {
1161 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
1166 * Bug #6898 - clients using Windows opens should
1167 * never be able to set this attribute into the
1170 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
1172 status = SMB_VFS_CREATE_FILE(
1175 root_dir_fid, /* root_dir_fid */
1176 smb_fname, /* fname */
1177 access_mask, /* access_mask */
1178 share_access, /* share_access */
1179 create_disposition, /* create_disposition*/
1180 create_options, /* create_options */
1181 file_attributes, /* file_attributes */
1182 oplock_request, /* oplock_request */
1183 allocation_size, /* allocation_size */
1184 0, /* private_flags */
1186 ea_list, /* ea_list */
1190 if(!NT_STATUS_IS_OK(status)) {
1191 if (open_was_deferred(req->sconn, req->mid)) {
1192 /* We have re-scheduled this call, no error. */
1195 reply_openerror(req, status);
1199 /* Ensure we're pointing at the correct stat struct. */
1200 TALLOC_FREE(smb_fname);
1201 smb_fname = fsp->fsp_name;
1204 * If the caller set the extended oplock request bit
1205 * and we granted one (by whatever means) - set the
1206 * correct bit for extended oplock reply.
1209 if (oplock_request &&
1210 (lp_fake_oplocks(SNUM(conn))
1211 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
1214 * Exclusive oplock granted
1217 if (flags & REQUEST_BATCH_OPLOCK) {
1218 oplock_granted = BATCH_OPLOCK_RETURN;
1220 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
1222 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1223 oplock_granted = LEVEL_II_OPLOCK_RETURN;
1225 oplock_granted = NO_OPLOCK_RETURN;
1228 file_len = smb_fname->st.st_ex_size;
1230 /* Realloc the size of parameters and data we will return */
1231 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1232 /* Extended response is 32 more byyes. */
1237 params = nttrans_realloc(ppparams, param_len);
1238 if(params == NULL) {
1239 reply_nterror(req, NT_STATUS_NO_MEMORY);
1244 SCVAL(p, 0, oplock_granted);
1247 SSVAL(p,0,fsp->fnum);
1249 if ((create_disposition == FILE_SUPERSEDE)
1250 && (info == FILE_WAS_OVERWRITTEN)) {
1251 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1257 fattr = dos_mode(conn, smb_fname);
1259 fattr = FILE_ATTRIBUTE_NORMAL;
1263 create_timespec = get_create_timespec(conn, fsp, smb_fname);
1264 a_timespec = smb_fname->st.st_ex_atime;
1265 m_timespec = smb_fname->st.st_ex_mtime;
1266 c_timespec = get_change_timespec(conn, fsp, smb_fname);
1268 if (lp_dos_filetime_resolution(SNUM(conn))) {
1269 dos_filetime_timespec(&create_timespec);
1270 dos_filetime_timespec(&a_timespec);
1271 dos_filetime_timespec(&m_timespec);
1272 dos_filetime_timespec(&c_timespec);
1275 put_long_date_timespec(conn->ts_res, p, create_timespec); /* create time. */
1277 put_long_date_timespec(conn->ts_res, p, a_timespec); /* access time */
1279 put_long_date_timespec(conn->ts_res, p, m_timespec); /* write time */
1281 put_long_date_timespec(conn->ts_res, p, c_timespec); /* change time */
1283 SIVAL(p,0,fattr); /* File Attributes. */
1285 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn, fsp, &smb_fname->st));
1287 SOFF_T(p,0,file_len);
1289 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1290 uint16_t file_status = (NO_EAS|NO_SUBSTREAMS|NO_REPARSETAG);
1291 size_t num_names = 0;
1292 unsigned int num_streams = 0;
1293 struct stream_struct *streams = NULL;
1295 /* Do we have any EA's ? */
1296 status = get_ea_names_from_file(ctx, conn, fsp,
1297 smb_fname->base_name, NULL, &num_names);
1298 if (NT_STATUS_IS_OK(status) && num_names) {
1299 file_status &= ~NO_EAS;
1301 status = vfs_streaminfo(conn, NULL, smb_fname->base_name, ctx,
1302 &num_streams, &streams);
1303 /* There is always one stream, ::$DATA. */
1304 if (NT_STATUS_IS_OK(status) && num_streams > 1) {
1305 file_status &= ~NO_SUBSTREAMS;
1307 TALLOC_FREE(streams);
1308 SSVAL(p,2,file_status);
1311 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1313 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1316 if (fsp->is_directory ||
1318 can_write_to_file(conn, smb_fname)) {
1319 perms = FILE_GENERIC_ALL;
1321 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1326 DEBUG(5,("call_nt_transact_create: open name = %s\n",
1327 smb_fname_str_dbg(smb_fname)));
1329 /* Send the required number of replies */
1330 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1335 /****************************************************************************
1336 Reply to a NT CANCEL request.
1337 conn POINTER CAN BE NULL HERE !
1338 ****************************************************************************/
1340 void reply_ntcancel(struct smb_request *req)
1342 struct smbXsrv_connection *xconn = req->xconn;
1343 struct smbd_server_connection *sconn = xconn->sconn;
1346 * Go through and cancel any pending change notifies.
1349 START_PROFILE(SMBntcancel);
1350 srv_cancel_sign_response(xconn);
1351 remove_pending_change_notify_requests_by_mid(sconn, req->mid);
1352 remove_pending_lock_requests_by_mid_smb1(sconn, req->mid);
1354 DEBUG(3,("reply_ntcancel: cancel called on mid = %llu.\n",
1355 (unsigned long long)req->mid));
1357 END_PROFILE(SMBntcancel);
1361 /****************************************************************************
1363 ****************************************************************************/
1365 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
1366 connection_struct *conn,
1367 struct smb_request *req,
1368 struct smb_filename *smb_fname_src,
1369 struct smb_filename *smb_fname_dst,
1372 files_struct *fsp1,*fsp2;
1376 NTSTATUS status = NT_STATUS_OK;
1379 if (!CAN_WRITE(conn)) {
1380 status = NT_STATUS_MEDIA_WRITE_PROTECTED;
1384 /* Source must already exist. */
1385 if (!VALID_STAT(smb_fname_src->st)) {
1386 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
1390 /* Ensure attributes match. */
1391 fattr = dos_mode(conn, smb_fname_src);
1392 if ((fattr & ~attrs) & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)) {
1393 status = NT_STATUS_NO_SUCH_FILE;
1397 /* Disallow if dst file already exists. */
1398 if (VALID_STAT(smb_fname_dst->st)) {
1399 status = NT_STATUS_OBJECT_NAME_COLLISION;
1403 /* No links from a directory. */
1404 if (S_ISDIR(smb_fname_src->st.st_ex_mode)) {
1405 status = NT_STATUS_FILE_IS_A_DIRECTORY;
1409 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1410 smb_fname_str_dbg(smb_fname_src),
1411 smb_fname_str_dbg(smb_fname_dst)));
1413 status = SMB_VFS_CREATE_FILE(
1416 0, /* root_dir_fid */
1417 smb_fname_src, /* fname */
1418 FILE_READ_DATA|FILE_READ_ATTRIBUTES|
1419 FILE_READ_EA, /* access_mask */
1420 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1422 FILE_OPEN, /* create_disposition*/
1423 0, /* create_options */
1424 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
1425 NO_OPLOCK, /* oplock_request */
1426 0, /* allocation_size */
1427 0, /* private_flags */
1433 if (!NT_STATUS_IS_OK(status)) {
1437 status = SMB_VFS_CREATE_FILE(
1440 0, /* root_dir_fid */
1441 smb_fname_dst, /* fname */
1442 FILE_WRITE_DATA|FILE_WRITE_ATTRIBUTES|
1443 FILE_WRITE_EA, /* access_mask */
1444 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1446 FILE_CREATE, /* create_disposition*/
1447 0, /* create_options */
1448 fattr, /* file_attributes */
1449 NO_OPLOCK, /* oplock_request */
1450 0, /* allocation_size */
1451 0, /* private_flags */
1457 if (!NT_STATUS_IS_OK(status)) {
1458 close_file(NULL, fsp1, ERROR_CLOSE);
1462 if (smb_fname_src->st.st_ex_size) {
1463 ret = vfs_transfer_file(fsp1, fsp2, smb_fname_src->st.st_ex_size);
1467 * As we are opening fsp1 read-only we only expect
1468 * an error on close on fsp2 if we are out of space.
1469 * Thus we don't look at the error return from the
1472 close_file(NULL, fsp1, NORMAL_CLOSE);
1474 /* Ensure the modtime is set correctly on the destination file. */
1475 set_close_write_time(fsp2, smb_fname_src->st.st_ex_mtime);
1477 status = close_file(NULL, fsp2, NORMAL_CLOSE);
1479 /* Grrr. We have to do this as open_file_ntcreate adds FILE_ATTRIBUTE_ARCHIVE when it
1480 creates the file. This isn't the correct thing to do in the copy
1482 if (!parent_dirname(talloc_tos(), smb_fname_dst->base_name, &parent,
1484 status = NT_STATUS_NO_MEMORY;
1487 file_set_dosmode(conn, smb_fname_dst, fattr, parent, false);
1488 TALLOC_FREE(parent);
1490 if (ret < (off_t)smb_fname_src->st.st_ex_size) {
1491 status = NT_STATUS_DISK_FULL;
1495 if (!NT_STATUS_IS_OK(status)) {
1496 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1497 nt_errstr(status), smb_fname_str_dbg(smb_fname_src),
1498 smb_fname_str_dbg(smb_fname_dst)));
1504 /****************************************************************************
1505 Reply to a NT rename request.
1506 ****************************************************************************/
1508 void reply_ntrename(struct smb_request *req)
1510 connection_struct *conn = req->conn;
1511 struct smb_filename *smb_fname_old = NULL;
1512 struct smb_filename *smb_fname_new = NULL;
1513 char *oldname = NULL;
1514 char *newname = NULL;
1517 bool src_has_wcard = False;
1518 bool dest_has_wcard = False;
1520 uint32_t ucf_flags_src = 0;
1521 uint32_t ucf_flags_dst = 0;
1523 TALLOC_CTX *ctx = talloc_tos();
1524 bool stream_rename = false;
1526 START_PROFILE(SMBntrename);
1529 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1533 attrs = SVAL(req->vwv+0, 0);
1534 rename_type = SVAL(req->vwv+1, 0);
1536 p = (const char *)req->buf + 1;
1537 p += srvstr_get_path_req_wcard(ctx, req, &oldname, p, STR_TERMINATE,
1538 &status, &src_has_wcard);
1539 if (!NT_STATUS_IS_OK(status)) {
1540 reply_nterror(req, status);
1544 if (ms_has_wild(oldname)) {
1545 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1550 p += srvstr_get_path_req_wcard(ctx, req, &newname, p, STR_TERMINATE,
1551 &status, &dest_has_wcard);
1552 if (!NT_STATUS_IS_OK(status)) {
1553 reply_nterror(req, status);
1557 if (!lp_posix_pathnames()) {
1558 /* The newname must begin with a ':' if the
1559 oldname contains a ':'. */
1560 if (strchr_m(oldname, ':')) {
1561 if (newname[0] != ':') {
1562 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1565 stream_rename = true;
1570 * If this is a rename operation, allow wildcards and save the
1571 * destination's last component.
1573 if (rename_type == RENAME_FLAG_RENAME) {
1574 ucf_flags_src = UCF_COND_ALLOW_WCARD_LCOMP;
1575 ucf_flags_dst = UCF_COND_ALLOW_WCARD_LCOMP | UCF_SAVE_LCOMP;
1578 /* rename_internals() calls unix_convert(), so don't call it here. */
1579 status = filename_convert(ctx, conn,
1580 req->flags2 & FLAGS2_DFS_PATHNAMES,
1585 if (!NT_STATUS_IS_OK(status)) {
1586 if (NT_STATUS_EQUAL(status,
1587 NT_STATUS_PATH_NOT_COVERED)) {
1588 reply_botherror(req,
1589 NT_STATUS_PATH_NOT_COVERED,
1590 ERRSRV, ERRbadpath);
1593 reply_nterror(req, status);
1597 status = filename_convert(ctx, conn,
1598 req->flags2 & FLAGS2_DFS_PATHNAMES,
1603 if (!NT_STATUS_IS_OK(status)) {
1604 if (NT_STATUS_EQUAL(status,
1605 NT_STATUS_PATH_NOT_COVERED)) {
1606 reply_botherror(req,
1607 NT_STATUS_PATH_NOT_COVERED,
1608 ERRSRV, ERRbadpath);
1611 reply_nterror(req, status);
1615 if (stream_rename) {
1616 /* smb_fname_new must be the same as smb_fname_old. */
1617 TALLOC_FREE(smb_fname_new->base_name);
1618 smb_fname_new->base_name = talloc_strdup(smb_fname_new,
1619 smb_fname_old->base_name);
1620 if (!smb_fname_new->base_name) {
1621 reply_nterror(req, NT_STATUS_NO_MEMORY);
1626 DEBUG(3,("reply_ntrename: %s -> %s\n",
1627 smb_fname_str_dbg(smb_fname_old),
1628 smb_fname_str_dbg(smb_fname_new)));
1630 switch(rename_type) {
1631 case RENAME_FLAG_RENAME:
1632 status = rename_internals(ctx, conn, req,
1633 smb_fname_old, smb_fname_new,
1634 attrs, False, src_has_wcard,
1638 case RENAME_FLAG_HARD_LINK:
1639 if (src_has_wcard || dest_has_wcard) {
1641 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1643 status = hardlink_internals(ctx, conn,
1650 case RENAME_FLAG_COPY:
1651 if (src_has_wcard || dest_has_wcard) {
1653 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1655 status = copy_internals(ctx, conn, req,
1661 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1662 status = NT_STATUS_INVALID_PARAMETER;
1665 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1669 if (!NT_STATUS_IS_OK(status)) {
1670 if (open_was_deferred(req->sconn, req->mid)) {
1671 /* We have re-scheduled this call. */
1675 reply_nterror(req, status);
1679 reply_outbuf(req, 0, 0);
1681 END_PROFILE(SMBntrename);
1685 /****************************************************************************
1686 Reply to a notify change - queue the request and
1687 don't allow a directory to be opened.
1688 ****************************************************************************/
1690 static void smbd_smb1_notify_reply(struct smb_request *req,
1691 NTSTATUS error_code,
1692 uint8_t *buf, size_t len)
1694 send_nt_replies(req->conn, req, error_code, (char *)buf, len, NULL, 0);
1697 static void call_nt_transact_notify_change(connection_struct *conn,
1698 struct smb_request *req,
1702 uint32 parameter_count,
1703 char **ppdata, uint32 data_count,
1704 uint32 max_data_count,
1705 uint32 max_param_count)
1707 uint16 *setup = *ppsetup;
1713 if(setup_count < 6) {
1714 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1718 fsp = file_fsp(req, SVAL(setup,4));
1719 filter = IVAL(setup, 0);
1720 recursive = (SVAL(setup, 6) != 0) ? True : False;
1722 DEBUG(3,("call_nt_transact_notify_change\n"));
1725 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1730 char *filter_string;
1732 if (!(filter_string = notify_filter_string(NULL, filter))) {
1733 reply_nterror(req,NT_STATUS_NO_MEMORY);
1737 DEBUG(3,("call_nt_transact_notify_change: notify change "
1738 "called on %s, filter = %s, recursive = %d\n",
1739 fsp_str_dbg(fsp), filter_string, recursive));
1741 TALLOC_FREE(filter_string);
1744 if((!fsp->is_directory) || (conn != fsp->conn)) {
1745 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1749 if (fsp->notify == NULL) {
1751 status = change_notify_create(fsp, filter, recursive);
1753 if (!NT_STATUS_IS_OK(status)) {
1754 DEBUG(10, ("change_notify_create returned %s\n",
1755 nt_errstr(status)));
1756 reply_nterror(req, status);
1761 if (change_notify_fsp_has_changes(fsp)) {
1764 * We've got changes pending, respond immediately
1768 * TODO: write a torture test to check the filtering behaviour
1772 change_notify_reply(req,
1776 smbd_smb1_notify_reply);
1779 * change_notify_reply() above has independently sent its
1786 * No changes pending, queue the request
1789 status = change_notify_add_request(req,
1793 smbd_smb1_notify_reply);
1794 if (!NT_STATUS_IS_OK(status)) {
1795 reply_nterror(req, status);
1800 /****************************************************************************
1801 Reply to an NT transact rename command.
1802 ****************************************************************************/
1804 static void call_nt_transact_rename(connection_struct *conn,
1805 struct smb_request *req,
1806 uint16 **ppsetup, uint32 setup_count,
1807 char **ppparams, uint32 parameter_count,
1808 char **ppdata, uint32 data_count,
1809 uint32 max_data_count)
1811 char *params = *ppparams;
1812 char *new_name = NULL;
1813 files_struct *fsp = NULL;
1814 bool dest_has_wcard = False;
1816 TALLOC_CTX *ctx = talloc_tos();
1818 if(parameter_count < 5) {
1819 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1823 fsp = file_fsp(req, SVAL(params, 0));
1824 if (!check_fsp(conn, req, fsp)) {
1827 srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
1828 parameter_count - 4,
1829 STR_TERMINATE, &status, &dest_has_wcard);
1830 if (!NT_STATUS_IS_OK(status)) {
1831 reply_nterror(req, status);
1836 * W2K3 ignores this request as the RAW-RENAME test
1837 * demonstrates, so we do.
1839 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1841 DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1842 fsp_str_dbg(fsp), new_name));
1847 /******************************************************************************
1848 Fake up a completely empty SD.
1849 *******************************************************************************/
1851 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, struct security_descriptor **ppsd)
1855 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1857 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1858 return NT_STATUS_NO_MEMORY;
1861 return NT_STATUS_OK;
1864 /****************************************************************************
1865 Reply to query a security descriptor.
1866 Callable from SMB2 and SMB2.
1867 If it returns NT_STATUS_BUFFER_TOO_SMALL, pdata_size is initialized with
1869 ****************************************************************************/
1871 NTSTATUS smbd_do_query_security_desc(connection_struct *conn,
1872 TALLOC_CTX *mem_ctx,
1874 uint32_t security_info_wanted,
1875 uint32_t max_data_count,
1876 uint8_t **ppmarshalled_sd,
1880 struct security_descriptor *psd = NULL;
1881 TALLOC_CTX *frame = talloc_stackframe();
1884 * Get the permissions to return.
1887 if ((security_info_wanted & SECINFO_SACL) &&
1888 !(fsp->access_mask & SEC_FLAG_SYSTEM_SECURITY)) {
1889 DEBUG(10, ("Access to SACL denied.\n"));
1891 return NT_STATUS_ACCESS_DENIED;
1894 if ((security_info_wanted & (SECINFO_DACL|SECINFO_OWNER|SECINFO_GROUP)) &&
1895 !(fsp->access_mask & SEC_STD_READ_CONTROL)) {
1896 DEBUG(10, ("Access to DACL, OWNER, or GROUP denied.\n"));
1898 return NT_STATUS_ACCESS_DENIED;
1901 if (security_info_wanted & (SECINFO_DACL|SECINFO_OWNER|
1902 SECINFO_GROUP|SECINFO_SACL)) {
1903 /* Don't return SECINFO_LABEL if anything else was
1904 requested. See bug #8458. */
1905 security_info_wanted &= ~SECINFO_LABEL;
1908 if (!lp_nt_acl_support(SNUM(conn))) {
1909 status = get_null_nt_acl(frame, &psd);
1910 } else if (security_info_wanted & SECINFO_LABEL) {
1911 /* Like W2K3 return a null object. */
1912 status = get_null_nt_acl(frame, &psd);
1914 status = SMB_VFS_FGET_NT_ACL(
1915 fsp, security_info_wanted, frame, &psd);
1917 if (!NT_STATUS_IS_OK(status)) {
1922 if (!(security_info_wanted & SECINFO_OWNER)) {
1923 psd->owner_sid = NULL;
1925 if (!(security_info_wanted & SECINFO_GROUP)) {
1926 psd->group_sid = NULL;
1928 if (!(security_info_wanted & SECINFO_DACL)) {
1929 psd->type &= ~SEC_DESC_DACL_PRESENT;
1932 if (!(security_info_wanted & SECINFO_SACL)) {
1933 psd->type &= ~SEC_DESC_SACL_PRESENT;
1937 /* If the SACL/DACL is NULL, but was requested, we mark that it is
1938 * present in the reply to match Windows behavior */
1939 if (psd->sacl == NULL &&
1940 security_info_wanted & SECINFO_SACL)
1941 psd->type |= SEC_DESC_SACL_PRESENT;
1942 if (psd->dacl == NULL &&
1943 security_info_wanted & SECINFO_DACL)
1944 psd->type |= SEC_DESC_DACL_PRESENT;
1946 if (security_info_wanted & SECINFO_LABEL) {
1947 /* Like W2K3 return a null object. */
1948 psd->owner_sid = NULL;
1949 psd->group_sid = NULL;
1952 psd->type &= ~(SEC_DESC_DACL_PRESENT|SEC_DESC_SACL_PRESENT);
1955 *psd_size = ndr_size_security_descriptor(psd, 0);
1957 DEBUG(3,("smbd_do_query_security_desc: sd_size = %lu.\n",
1958 (unsigned long)*psd_size));
1960 if (DEBUGLEVEL >= 10) {
1961 DEBUG(10,("smbd_do_query_security_desc for file %s\n",
1963 NDR_PRINT_DEBUG(security_descriptor, psd);
1966 if (max_data_count < *psd_size) {
1968 return NT_STATUS_BUFFER_TOO_SMALL;
1971 status = marshall_sec_desc(mem_ctx, psd,
1972 ppmarshalled_sd, psd_size);
1974 if (!NT_STATUS_IS_OK(status)) {
1980 return NT_STATUS_OK;
1983 /****************************************************************************
1984 SMB1 reply to query a security descriptor.
1985 ****************************************************************************/
1987 static void call_nt_transact_query_security_desc(connection_struct *conn,
1988 struct smb_request *req,
1992 uint32 parameter_count,
1995 uint32 max_data_count)
1997 char *params = *ppparams;
1998 char *data = *ppdata;
2000 uint32 security_info_wanted;
2001 files_struct *fsp = NULL;
2003 uint8_t *marshalled_sd = NULL;
2005 if(parameter_count < 8) {
2006 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2010 fsp = file_fsp(req, SVAL(params,0));
2012 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2016 security_info_wanted = IVAL(params,4);
2018 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, "
2019 "info_wanted = 0x%x\n", fsp_str_dbg(fsp),
2020 (unsigned int)security_info_wanted));
2022 params = nttrans_realloc(ppparams, 4);
2023 if(params == NULL) {
2024 reply_nterror(req, NT_STATUS_NO_MEMORY);
2029 * Get the permissions to return.
2032 status = smbd_do_query_security_desc(conn,
2035 security_info_wanted,
2040 if (NT_STATUS_EQUAL(status, NT_STATUS_BUFFER_TOO_SMALL)) {
2041 SIVAL(params,0,(uint32_t)sd_size);
2042 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
2043 params, 4, NULL, 0);
2047 if (!NT_STATUS_IS_OK(status)) {
2048 reply_nterror(req, status);
2052 SMB_ASSERT(sd_size > 0);
2054 SIVAL(params,0,(uint32_t)sd_size);
2056 if (max_data_count < sd_size) {
2057 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
2058 params, 4, NULL, 0);
2063 * Allocate the data we will return.
2066 data = nttrans_realloc(ppdata, sd_size);
2068 reply_nterror(req, NT_STATUS_NO_MEMORY);
2072 memcpy(data, marshalled_sd, sd_size);
2074 send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
2079 /****************************************************************************
2080 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
2081 ****************************************************************************/
2083 static void call_nt_transact_set_security_desc(connection_struct *conn,
2084 struct smb_request *req,
2088 uint32 parameter_count,
2091 uint32 max_data_count)
2093 char *params= *ppparams;
2094 char *data = *ppdata;
2095 files_struct *fsp = NULL;
2096 uint32 security_info_sent = 0;
2099 if(parameter_count < 8) {
2100 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2104 if((fsp = file_fsp(req, SVAL(params,0))) == NULL) {
2105 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2109 if (!CAN_WRITE(fsp->conn)) {
2110 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2114 if(!lp_nt_acl_support(SNUM(conn))) {
2118 security_info_sent = IVAL(params,4);
2120 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n",
2121 fsp_str_dbg(fsp), (unsigned int)security_info_sent));
2123 if (data_count == 0) {
2124 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2128 status = set_sd_blob(fsp, (uint8 *)data, data_count, security_info_sent);
2130 if (!NT_STATUS_IS_OK(status)) {
2131 reply_nterror(req, status);
2136 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2140 /****************************************************************************
2142 ****************************************************************************/
2144 static void call_nt_transact_ioctl(connection_struct *conn,
2145 struct smb_request *req,
2146 uint16 **ppsetup, uint32 setup_count,
2147 char **ppparams, uint32 parameter_count,
2148 char **ppdata, uint32 data_count,
2149 uint32 max_data_count)
2157 char *out_data = NULL;
2158 uint32 out_data_len = 0;
2159 char *pdata = *ppdata;
2160 TALLOC_CTX *ctx = talloc_tos();
2162 if (setup_count != 8) {
2163 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
2164 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2168 function = IVAL(*ppsetup, 0);
2169 fidnum = SVAL(*ppsetup, 4);
2170 isFSctl = CVAL(*ppsetup, 6);
2171 compfilter = CVAL(*ppsetup, 7);
2173 DEBUG(10, ("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
2174 function, fidnum, isFSctl, compfilter));
2176 fsp=file_fsp(req, fidnum);
2179 * We don't really implement IOCTLs, especially on files.
2182 DEBUG(10, ("isFSctl: 0x%02X indicates IOCTL, not FSCTL!\n",
2184 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2188 /* Has to be for an open file! */
2189 if (!check_fsp_open(conn, req, fsp)) {
2193 SMB_PERFCOUNT_SET_IOCTL(&req->pcd, function);
2196 * out_data might be allocated by the VFS module, but talloc should be
2197 * used, and should be cleaned up when the request ends.
2199 status = SMB_VFS_FSCTL(fsp,
2205 (uint8_t **)&out_data,
2208 if (!NT_STATUS_IS_OK(status)) {
2209 reply_nterror(req, status);
2211 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, out_data, out_data_len);
2216 #ifdef HAVE_SYS_QUOTAS
2217 /****************************************************************************
2218 Reply to get user quota
2219 ****************************************************************************/
2221 static void call_nt_transact_get_user_quota(connection_struct *conn,
2222 struct smb_request *req,
2226 uint32 parameter_count,
2229 uint32 max_data_count)
2231 NTSTATUS nt_status = NT_STATUS_OK;
2232 char *params = *ppparams;
2233 char *pdata = *ppdata;
2235 int data_len=0,param_len=0;
2238 files_struct *fsp = NULL;
2242 bool start_enum = True;
2243 SMB_NTQUOTA_STRUCT qt;
2244 SMB_NTQUOTA_LIST *tmp_list;
2245 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2250 if (get_current_uid(conn) != 0) {
2251 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2252 "[%s]\n", lp_servicename(talloc_tos(), SNUM(conn)),
2253 conn->session_info->unix_info->unix_name));
2254 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2259 * Ensure minimum number of parameters sent.
2262 if (parameter_count < 4) {
2263 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2264 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2268 /* maybe we can check the quota_fnum */
2269 fsp = file_fsp(req, SVAL(params,0));
2270 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2271 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2272 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2276 /* the NULL pointer checking for fsp->fake_file_handle->pd
2277 * is done by CHECK_NTQUOTA_HANDLE_OK()
2279 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->private_data;
2281 level = SVAL(params,2);
2283 /* unknown 12 bytes leading in params */
2286 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2287 /* seems that we should continue with the enum here --metze */
2289 if (qt_handle->quota_list!=NULL &&
2290 qt_handle->tmp_list==NULL) {
2293 free_ntquota_list(&(qt_handle->quota_list));
2295 /* Realloc the size of parameters and data we will return */
2297 params = nttrans_realloc(ppparams, param_len);
2298 if(params == NULL) {
2299 reply_nterror(req, NT_STATUS_NO_MEMORY);
2304 SIVAL(params,0,data_len);
2311 case TRANSACT_GET_USER_QUOTA_LIST_START:
2313 if (qt_handle->quota_list==NULL &&
2314 qt_handle->tmp_list==NULL) {
2318 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2319 reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
2323 /* Realloc the size of parameters and data we will return */
2325 params = nttrans_realloc(ppparams, param_len);
2326 if(params == NULL) {
2327 reply_nterror(req, NT_STATUS_NO_MEMORY);
2331 /* we should not trust the value in max_data_count*/
2332 max_data_count = MIN(max_data_count,2048);
2334 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2336 reply_nterror(req, NT_STATUS_NO_MEMORY);
2342 /* set params Size of returned Quota Data 4 bytes*/
2343 /* but set it later when we know it */
2345 /* for each entry push the data */
2348 qt_handle->tmp_list = qt_handle->quota_list;
2351 tmp_list = qt_handle->tmp_list;
2353 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2354 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2356 sid_len = ndr_size_dom_sid(
2357 &tmp_list->quotas->sid, 0);
2358 entry_len = 40 + sid_len;
2360 /* nextoffset entry 4 bytes */
2361 SIVAL(entry,0,entry_len);
2363 /* then the len of the SID 4 bytes */
2364 SIVAL(entry,4,sid_len);
2366 /* unknown data 8 bytes uint64_t */
2367 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-metze*/
2369 /* the used disk space 8 bytes uint64_t */
2370 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2372 /* the soft quotas 8 bytes uint64_t */
2373 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2375 /* the hard quotas 8 bytes uint64_t */
2376 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2378 /* and now the SID */
2379 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2382 qt_handle->tmp_list = tmp_list;
2384 /* overwrite the offset of the last entry */
2385 SIVAL(entry-entry_len,0,0);
2387 data_len = 4+qt_len;
2388 /* overwrite the params quota_data_len */
2389 SIVAL(params,0,data_len);
2393 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2395 /* unknown 4 bytes IVAL(pdata,0) */
2397 if (data_count < 8) {
2398 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2399 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2403 sid_len = IVAL(pdata,4);
2404 /* Ensure this is less than 1mb. */
2405 if (sid_len > (1024*1024)) {
2406 reply_nterror(req, NT_STATUS_NO_MEMORY);
2410 if (data_count < 8+sid_len) {
2411 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2412 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2416 data_len = 4+40+sid_len;
2418 if (max_data_count < data_len) {
2419 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2420 max_data_count, data_len));
2422 SIVAL(params,0,data_len);
2424 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2428 if (!sid_parse(pdata+8,sid_len,&sid)) {
2429 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2433 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2436 * we have to return zero's in all fields
2437 * instead of returning an error here
2442 /* Realloc the size of parameters and data we will return */
2444 params = nttrans_realloc(ppparams, param_len);
2445 if(params == NULL) {
2446 reply_nterror(req, NT_STATUS_NO_MEMORY);
2450 pdata = nttrans_realloc(ppdata, data_len);
2452 reply_nterror(req, NT_STATUS_NO_MEMORY);
2458 /* set params Size of returned Quota Data 4 bytes*/
2459 SIVAL(params,0,data_len);
2461 /* nextoffset entry 4 bytes */
2464 /* then the len of the SID 4 bytes */
2465 SIVAL(entry,4,sid_len);
2467 /* unknown data 8 bytes uint64_t */
2468 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-mezte*/
2470 /* the used disk space 8 bytes uint64_t */
2471 SBIG_UINT(entry,16,qt.usedspace);
2473 /* the soft quotas 8 bytes uint64_t */
2474 SBIG_UINT(entry,24,qt.softlim);
2476 /* the hard quotas 8 bytes uint64_t */
2477 SBIG_UINT(entry,32,qt.hardlim);
2479 /* and now the SID */
2480 sid_linearize(entry+40, sid_len, &sid);
2485 DEBUG(0, ("do_nt_transact_get_user_quota: %s: unknown "
2487 fsp_fnum_dbg(fsp), level));
2488 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2493 send_nt_replies(conn, req, nt_status, params, param_len,
2497 /****************************************************************************
2498 Reply to set user quota
2499 ****************************************************************************/
2501 static void call_nt_transact_set_user_quota(connection_struct *conn,
2502 struct smb_request *req,
2506 uint32 parameter_count,
2509 uint32 max_data_count)
2511 char *params = *ppparams;
2512 char *pdata = *ppdata;
2513 int data_len=0,param_len=0;
2514 SMB_NTQUOTA_STRUCT qt;
2517 files_struct *fsp = NULL;
2522 if (get_current_uid(conn) != 0) {
2523 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2524 "[%s]\n", lp_servicename(talloc_tos(), SNUM(conn)),
2525 conn->session_info->unix_info->unix_name));
2526 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2531 * Ensure minimum number of parameters sent.
2534 if (parameter_count < 2) {
2535 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2536 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2540 /* maybe we can check the quota_fnum */
2541 fsp = file_fsp(req, SVAL(params,0));
2542 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2543 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2544 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2548 if (data_count < 40) {
2549 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2550 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2554 /* offset to next quota record.
2555 * 4 bytes IVAL(pdata,0)
2560 sid_len = IVAL(pdata,4);
2562 if (data_count < 40+sid_len || (40+sid_len < sid_len)) {
2563 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2564 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2568 /* unknown 8 bytes in pdata
2569 * maybe its the change time in NTTIME
2572 /* the used space 8 bytes (uint64_t)*/
2573 qt.usedspace = BVAL(pdata,16);
2575 /* the soft quotas 8 bytes (uint64_t)*/
2576 qt.softlim = BVAL(pdata,24);
2578 /* the hard quotas 8 bytes (uint64_t)*/
2579 qt.hardlim = BVAL(pdata,32);
2581 if (!sid_parse(pdata+40,sid_len,&sid)) {
2582 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2586 DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid)));
2588 /* 44 unknown bytes left... */
2590 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2591 reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
2595 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2598 #endif /* HAVE_SYS_QUOTAS */
2600 static void handle_nttrans(connection_struct *conn,
2601 struct trans_state *state,
2602 struct smb_request *req)
2604 if (get_Protocol() >= PROTOCOL_NT1) {
2605 req->flags2 |= 0x40; /* IS_LONG_NAME */
2606 SSVAL(discard_const_p(uint8_t, req->inbuf),smb_flg2,req->flags2);
2610 SMB_PERFCOUNT_SET_SUBOP(&req->pcd, state->call);
2612 /* Now we must call the relevant NT_TRANS function */
2613 switch(state->call) {
2614 case NT_TRANSACT_CREATE:
2616 START_PROFILE(NT_transact_create);
2617 call_nt_transact_create(
2619 &state->setup, state->setup_count,
2620 &state->param, state->total_param,
2621 &state->data, state->total_data,
2622 state->max_data_return);
2623 END_PROFILE(NT_transact_create);
2627 case NT_TRANSACT_IOCTL:
2629 START_PROFILE(NT_transact_ioctl);
2630 call_nt_transact_ioctl(
2632 &state->setup, state->setup_count,
2633 &state->param, state->total_param,
2634 &state->data, state->total_data,
2635 state->max_data_return);
2636 END_PROFILE(NT_transact_ioctl);
2640 case NT_TRANSACT_SET_SECURITY_DESC:
2642 START_PROFILE(NT_transact_set_security_desc);
2643 call_nt_transact_set_security_desc(
2645 &state->setup, state->setup_count,
2646 &state->param, state->total_param,
2647 &state->data, state->total_data,
2648 state->max_data_return);
2649 END_PROFILE(NT_transact_set_security_desc);
2653 case NT_TRANSACT_NOTIFY_CHANGE:
2655 START_PROFILE(NT_transact_notify_change);
2656 call_nt_transact_notify_change(
2658 &state->setup, state->setup_count,
2659 &state->param, state->total_param,
2660 &state->data, state->total_data,
2661 state->max_data_return,
2662 state->max_param_return);
2663 END_PROFILE(NT_transact_notify_change);
2667 case NT_TRANSACT_RENAME:
2669 START_PROFILE(NT_transact_rename);
2670 call_nt_transact_rename(
2672 &state->setup, state->setup_count,
2673 &state->param, state->total_param,
2674 &state->data, state->total_data,
2675 state->max_data_return);
2676 END_PROFILE(NT_transact_rename);
2680 case NT_TRANSACT_QUERY_SECURITY_DESC:
2682 START_PROFILE(NT_transact_query_security_desc);
2683 call_nt_transact_query_security_desc(
2685 &state->setup, state->setup_count,
2686 &state->param, state->total_param,
2687 &state->data, state->total_data,
2688 state->max_data_return);
2689 END_PROFILE(NT_transact_query_security_desc);
2693 #ifdef HAVE_SYS_QUOTAS
2694 case NT_TRANSACT_GET_USER_QUOTA:
2696 START_PROFILE(NT_transact_get_user_quota);
2697 call_nt_transact_get_user_quota(
2699 &state->setup, state->setup_count,
2700 &state->param, state->total_param,
2701 &state->data, state->total_data,
2702 state->max_data_return);
2703 END_PROFILE(NT_transact_get_user_quota);
2707 case NT_TRANSACT_SET_USER_QUOTA:
2709 START_PROFILE(NT_transact_set_user_quota);
2710 call_nt_transact_set_user_quota(
2712 &state->setup, state->setup_count,
2713 &state->param, state->total_param,
2714 &state->data, state->total_data,
2715 state->max_data_return);
2716 END_PROFILE(NT_transact_set_user_quota);
2719 #endif /* HAVE_SYS_QUOTAS */
2722 /* Error in request */
2723 DEBUG(0,("handle_nttrans: Unknown request %d in "
2724 "nttrans call\n", state->call));
2725 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2731 /****************************************************************************
2732 Reply to a SMBNTtrans.
2733 ****************************************************************************/
2735 void reply_nttrans(struct smb_request *req)
2737 connection_struct *conn = req->conn;
2742 uint16 function_code;
2744 struct trans_state *state;
2746 START_PROFILE(SMBnttrans);
2748 if (req->wct < 19) {
2749 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2750 END_PROFILE(SMBnttrans);
2754 pscnt = IVAL(req->vwv+9, 1);
2755 psoff = IVAL(req->vwv+11, 1);
2756 dscnt = IVAL(req->vwv+13, 1);
2757 dsoff = IVAL(req->vwv+15, 1);
2758 function_code = SVAL(req->vwv+18, 0);
2760 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2761 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2762 END_PROFILE(SMBnttrans);
2766 result = allow_new_trans(conn->pending_trans, req->mid);
2767 if (!NT_STATUS_IS_OK(result)) {
2768 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
2769 reply_nterror(req, result);
2770 END_PROFILE(SMBnttrans);
2774 if ((state = talloc(conn, struct trans_state)) == NULL) {
2775 reply_nterror(req, NT_STATUS_NO_MEMORY);
2776 END_PROFILE(SMBnttrans);
2780 state->cmd = SMBnttrans;
2782 state->mid = req->mid;
2783 state->vuid = req->vuid;
2784 state->total_data = IVAL(req->vwv+3, 1);
2786 state->total_param = IVAL(req->vwv+1, 1);
2787 state->param = NULL;
2788 state->max_data_return = IVAL(req->vwv+7, 1);
2789 state->max_param_return = IVAL(req->vwv+5, 1);
2791 /* setup count is in *words* */
2792 state->setup_count = 2*CVAL(req->vwv+17, 1);
2793 state->setup = NULL;
2794 state->call = function_code;
2796 DEBUG(10, ("num_setup=%u, "
2797 "param_total=%u, this_param=%u, max_param=%u, "
2798 "data_total=%u, this_data=%u, max_data=%u, "
2799 "param_offset=%u, data_offset=%u\n",
2800 (unsigned)state->setup_count,
2801 (unsigned)state->total_param, (unsigned)pscnt,
2802 (unsigned)state->max_param_return,
2803 (unsigned)state->total_data, (unsigned)dscnt,
2804 (unsigned)state->max_data_return,
2805 (unsigned)psoff, (unsigned)dsoff));
2808 * All nttrans messages we handle have smb_wct == 19 +
2809 * state->setup_count. Ensure this is so as a sanity check.
2812 if(req->wct != 19 + (state->setup_count/2)) {
2813 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2814 req->wct, 19 + (state->setup_count/2)));
2818 /* Don't allow more than 128mb for each value. */
2819 if ((state->total_data > (1024*1024*128)) ||
2820 (state->total_param > (1024*1024*128))) {
2821 reply_nterror(req, NT_STATUS_NO_MEMORY);
2822 END_PROFILE(SMBnttrans);
2826 if ((dscnt > state->total_data) || (pscnt > state->total_param))
2829 if (state->total_data) {
2831 if (trans_oob(state->total_data, 0, dscnt)
2832 || trans_oob(smb_len(req->inbuf), dsoff, dscnt)) {
2836 /* Can't use talloc here, the core routines do realloc on the
2837 * params and data. */
2838 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
2839 DEBUG(0,("reply_nttrans: data malloc fail for %u "
2840 "bytes !\n", (unsigned int)state->total_data));
2842 reply_nterror(req, NT_STATUS_NO_MEMORY);
2843 END_PROFILE(SMBnttrans);
2847 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
2850 if (state->total_param) {
2852 if (trans_oob(state->total_param, 0, pscnt)
2853 || trans_oob(smb_len(req->inbuf), psoff, pscnt)) {
2857 /* Can't use talloc here, the core routines do realloc on the
2858 * params and data. */
2859 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
2860 DEBUG(0,("reply_nttrans: param malloc fail for %u "
2861 "bytes !\n", (unsigned int)state->total_param));
2862 SAFE_FREE(state->data);
2864 reply_nterror(req, NT_STATUS_NO_MEMORY);
2865 END_PROFILE(SMBnttrans);
2869 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
2872 state->received_data = dscnt;
2873 state->received_param = pscnt;
2875 if(state->setup_count > 0) {
2876 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2877 state->setup_count));
2880 * No overflow possible here, state->setup_count is an
2881 * unsigned int, being filled by a single byte from
2882 * CVAL(req->vwv+13, 0) above. The cast in the comparison
2883 * below is not necessary, it's here to clarify things. The
2884 * validity of req->vwv and req->wct has been checked in
2885 * init_smb_request already.
2887 if ((state->setup_count/2) + 19 > (unsigned int)req->wct) {
2891 state->setup = (uint16 *)TALLOC(state, state->setup_count);
2892 if (state->setup == NULL) {
2893 DEBUG(0,("reply_nttrans : Out of memory\n"));
2894 SAFE_FREE(state->data);
2895 SAFE_FREE(state->param);
2897 reply_nterror(req, NT_STATUS_NO_MEMORY);
2898 END_PROFILE(SMBnttrans);
2902 memcpy(state->setup, req->vwv+19, state->setup_count);
2903 dump_data(10, (uint8 *)state->setup, state->setup_count);
2906 if ((state->received_data == state->total_data) &&
2907 (state->received_param == state->total_param)) {
2908 handle_nttrans(conn, state, req);
2909 SAFE_FREE(state->param);
2910 SAFE_FREE(state->data);
2912 END_PROFILE(SMBnttrans);
2916 DLIST_ADD(conn->pending_trans, state);
2918 /* We need to send an interim response then receive the rest
2919 of the parameter/data bytes */
2920 reply_outbuf(req, 0, 0);
2921 show_msg((char *)req->outbuf);
2922 END_PROFILE(SMBnttrans);
2927 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
2928 SAFE_FREE(state->data);
2929 SAFE_FREE(state->param);
2931 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2932 END_PROFILE(SMBnttrans);
2936 /****************************************************************************
2937 Reply to a SMBnttranss
2938 ****************************************************************************/
2940 void reply_nttranss(struct smb_request *req)
2942 connection_struct *conn = req->conn;
2943 uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
2944 struct trans_state *state;
2946 START_PROFILE(SMBnttranss);
2948 show_msg((const char *)req->inbuf);
2950 /* Windows clients expect all replies to
2951 an NT transact secondary (SMBnttranss 0xA1)
2952 to have a command code of NT transact
2953 (SMBnttrans 0xA0). See bug #8989 for details. */
2954 req->cmd = SMBnttrans;
2956 if (req->wct < 18) {
2957 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2958 END_PROFILE(SMBnttranss);
2962 for (state = conn->pending_trans; state != NULL;
2963 state = state->next) {
2964 if (state->mid == req->mid) {
2969 if ((state == NULL) || (state->cmd != SMBnttrans)) {
2970 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2971 END_PROFILE(SMBnttranss);
2975 /* Revise state->total_param and state->total_data in case they have
2976 changed downwards */
2977 if (IVAL(req->vwv+1, 1) < state->total_param) {
2978 state->total_param = IVAL(req->vwv+1, 1);
2980 if (IVAL(req->vwv+3, 1) < state->total_data) {
2981 state->total_data = IVAL(req->vwv+3, 1);
2984 pcnt = IVAL(req->vwv+5, 1);
2985 poff = IVAL(req->vwv+7, 1);
2986 pdisp = IVAL(req->vwv+9, 1);
2988 dcnt = IVAL(req->vwv+11, 1);
2989 doff = IVAL(req->vwv+13, 1);
2990 ddisp = IVAL(req->vwv+15, 1);
2992 state->received_param += pcnt;
2993 state->received_data += dcnt;
2995 if ((state->received_data > state->total_data) ||
2996 (state->received_param > state->total_param))
3000 if (trans_oob(state->total_param, pdisp, pcnt)
3001 || trans_oob(smb_len(req->inbuf), poff, pcnt)) {
3004 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,pcnt);
3008 if (trans_oob(state->total_data, ddisp, dcnt)
3009 || trans_oob(smb_len(req->inbuf), doff, dcnt)) {
3012 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
3015 if ((state->received_param < state->total_param) ||
3016 (state->received_data < state->total_data)) {
3017 END_PROFILE(SMBnttranss);
3021 handle_nttrans(conn, state, req);
3023 DLIST_REMOVE(conn->pending_trans, state);
3024 SAFE_FREE(state->data);
3025 SAFE_FREE(state->param);
3027 END_PROFILE(SMBnttranss);
3032 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
3033 DLIST_REMOVE(conn->pending_trans, state);
3034 SAFE_FREE(state->data);
3035 SAFE_FREE(state->param);
3037 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3038 END_PROFILE(SMBnttranss);