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"
33 extern const struct generic_mapping file_generic_mapping;
35 static char *nttrans_realloc(char **ptr, size_t size)
38 smb_panic("nttrans_realloc() called with NULL ptr");
41 *ptr = (char *)SMB_REALLOC(*ptr, size);
45 memset(*ptr,'\0',size);
49 /****************************************************************************
50 Send the required number of replies back.
51 We assume all fields other than the data fields are
52 set correctly for the type of call.
53 HACK ! Always assumes smb_setup field is zero.
54 ****************************************************************************/
56 static void send_nt_replies(connection_struct *conn,
57 struct smb_request *req, NTSTATUS nt_error,
58 char *params, int paramsize,
59 char *pdata, int datasize)
61 int data_to_send = datasize;
62 int params_to_send = paramsize;
66 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
67 int alignment_offset = 1;
68 int data_alignment_offset = 0;
69 struct smbd_server_connection *sconn = req->sconn;
70 int max_send = sconn->smb1.sessions.max_send;
73 * If there genuinely are no parameters or data to send just send
77 if(params_to_send == 0 && data_to_send == 0) {
78 reply_outbuf(req, 18, 0);
79 if (NT_STATUS_V(nt_error)) {
80 error_packet_set((char *)req->outbuf,
84 show_msg((char *)req->outbuf);
85 if (!srv_send_smb(sconn,
88 IS_CONN_ENCRYPTED(conn),
90 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
92 TALLOC_FREE(req->outbuf);
97 * When sending params and data ensure that both are nicely aligned.
98 * Only do this alignment when there is also data to send - else
99 * can cause NT redirector problems.
102 if (((params_to_send % 4) != 0) && (data_to_send != 0)) {
103 data_alignment_offset = 4 - (params_to_send % 4);
107 * Space is bufsize minus Netbios over TCP header minus SMB header.
108 * The alignment_offset is to align the param bytes on a four byte
109 * boundary (2 bytes for data len, one byte pad).
110 * NT needs this to work correctly.
113 useable_space = max_send - (smb_size
116 + data_alignment_offset);
118 if (useable_space < 0) {
119 char *msg = talloc_asprintf(
121 "send_nt_replies failed sanity useable_space = %d!!!",
123 DEBUG(0, ("%s\n", msg));
124 exit_server_cleanly(msg);
127 while (params_to_send || data_to_send) {
130 * Calculate whether we will totally or partially fill this packet.
133 total_sent_thistime = params_to_send + data_to_send;
136 * We can never send more than useable_space.
139 total_sent_thistime = MIN(total_sent_thistime, useable_space);
141 reply_outbuf(req, 18,
142 total_sent_thistime + alignment_offset
143 + data_alignment_offset);
146 * Set total params and data to be sent.
149 SIVAL(req->outbuf,smb_ntr_TotalParameterCount,paramsize);
150 SIVAL(req->outbuf,smb_ntr_TotalDataCount,datasize);
153 * Calculate how many parameters and data we can fit into
154 * this packet. Parameters get precedence.
157 params_sent_thistime = MIN(params_to_send,useable_space);
158 data_sent_thistime = useable_space - params_sent_thistime;
159 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
161 SIVAL(req->outbuf, smb_ntr_ParameterCount,
162 params_sent_thistime);
164 if(params_sent_thistime == 0) {
165 SIVAL(req->outbuf,smb_ntr_ParameterOffset,0);
166 SIVAL(req->outbuf,smb_ntr_ParameterDisplacement,0);
169 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
170 * parameter bytes, however the first 4 bytes of outbuf are
171 * the Netbios over TCP header. Thus use smb_base() to subtract
172 * them from the calculation.
175 SIVAL(req->outbuf,smb_ntr_ParameterOffset,
176 ((smb_buf(req->outbuf)+alignment_offset)
177 - smb_base(req->outbuf)));
179 * Absolute displacement of param bytes sent in this packet.
182 SIVAL(req->outbuf, smb_ntr_ParameterDisplacement,
187 * Deal with the data portion.
190 SIVAL(req->outbuf, smb_ntr_DataCount, data_sent_thistime);
192 if(data_sent_thistime == 0) {
193 SIVAL(req->outbuf,smb_ntr_DataOffset,0);
194 SIVAL(req->outbuf,smb_ntr_DataDisplacement, 0);
197 * The offset of the data bytes is the offset of the
198 * parameter bytes plus the number of parameters being sent this time.
201 SIVAL(req->outbuf, smb_ntr_DataOffset,
202 ((smb_buf(req->outbuf)+alignment_offset) -
203 smb_base(req->outbuf))
204 + params_sent_thistime + data_alignment_offset);
205 SIVAL(req->outbuf,smb_ntr_DataDisplacement, pd - pdata);
209 * Copy the param bytes into the packet.
212 if(params_sent_thistime) {
213 if (alignment_offset != 0) {
214 memset(smb_buf(req->outbuf), 0,
217 memcpy((smb_buf(req->outbuf)+alignment_offset), pp,
218 params_sent_thistime);
222 * Copy in the data bytes
225 if(data_sent_thistime) {
226 if (data_alignment_offset != 0) {
227 memset((smb_buf(req->outbuf)+alignment_offset+
228 params_sent_thistime), 0,
229 data_alignment_offset);
231 memcpy(smb_buf(req->outbuf)+alignment_offset
232 +params_sent_thistime+data_alignment_offset,
233 pd,data_sent_thistime);
236 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
237 params_sent_thistime, data_sent_thistime, useable_space));
238 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
239 params_to_send, data_to_send, paramsize, datasize));
241 if (NT_STATUS_V(nt_error)) {
242 error_packet_set((char *)req->outbuf,
247 /* Send the packet */
248 show_msg((char *)req->outbuf);
249 if (!srv_send_smb(sconn,
252 IS_CONN_ENCRYPTED(conn),
254 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
257 TALLOC_FREE(req->outbuf);
259 pp += params_sent_thistime;
260 pd += data_sent_thistime;
262 params_to_send -= params_sent_thistime;
263 data_to_send -= data_sent_thistime;
269 if(params_to_send < 0 || data_to_send < 0) {
270 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
271 params_to_send, data_to_send));
272 exit_server_cleanly("send_nt_replies: internal error");
277 /****************************************************************************
278 Reply to an NT create and X call on a pipe
279 ****************************************************************************/
281 static void nt_open_pipe(char *fname, connection_struct *conn,
282 struct smb_request *req, uint16_t *ppnum)
287 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
289 /* Strip \\ off the name if present. */
290 while (fname[0] == '\\') {
294 status = open_np_file(req, fname, &fsp);
295 if (!NT_STATUS_IS_OK(status)) {
296 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
297 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
301 reply_nterror(req, status);
309 /****************************************************************************
310 Reply to an NT create and X call for pipes.
311 ****************************************************************************/
313 static void do_ntcreate_pipe_open(connection_struct *conn,
314 struct smb_request *req)
317 uint16_t pnum = FNUM_FIELD_INVALID;
319 uint32 flags = IVAL(req->vwv+3, 1);
320 TALLOC_CTX *ctx = talloc_tos();
322 srvstr_pull_req_talloc(ctx, req, &fname, req->buf, STR_TERMINATE);
325 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
329 nt_open_pipe(fname, conn, req, &pnum);
337 * Deal with pipe return.
340 if (flags & EXTENDED_RESPONSE_REQUIRED) {
341 /* This is very strange. We
342 * return 50 words, but only set
343 * the wcnt to 42 ? It's definitely
344 * what happens on the wire....
346 reply_outbuf(req, 50, 0);
347 SCVAL(req->outbuf,smb_wct,42);
349 reply_outbuf(req, 34, 0);
352 SSVAL(req->outbuf, smb_vwv0, 0xff); /* andx chain ends */
353 SSVAL(req->outbuf, smb_vwv1, 0); /* no andx offset */
355 p = (char *)req->outbuf + smb_vwv2;
359 SIVAL(p,0,FILE_WAS_OPENED);
362 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
365 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
367 SSVAL(p,2, 0x5FF); /* ? */
370 if (flags & EXTENDED_RESPONSE_REQUIRED) {
372 SIVAL(p,0,FILE_GENERIC_ALL);
374 * For pipes W2K3 seems to return
376 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
378 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
381 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
384 struct case_semantics_state {
385 connection_struct *conn;
388 bool short_case_preserve;
391 /****************************************************************************
392 Restore case semantics.
393 ****************************************************************************/
395 static int restore_case_semantics(struct case_semantics_state *state)
397 state->conn->case_sensitive = state->case_sensitive;
398 state->conn->case_preserve = state->case_preserve;
399 state->conn->short_case_preserve = state->short_case_preserve;
403 /****************************************************************************
405 ****************************************************************************/
407 static struct case_semantics_state *set_posix_case_semantics(TALLOC_CTX *mem_ctx,
408 connection_struct *conn)
410 struct case_semantics_state *result;
412 if (!(result = talloc(mem_ctx, struct case_semantics_state))) {
417 result->case_sensitive = conn->case_sensitive;
418 result->case_preserve = conn->case_preserve;
419 result->short_case_preserve = conn->short_case_preserve;
422 conn->case_sensitive = True;
423 conn->case_preserve = True;
424 conn->short_case_preserve = True;
426 talloc_set_destructor(result, restore_case_semantics);
431 /****************************************************************************
432 Reply to an NT create and X call.
433 ****************************************************************************/
435 void reply_ntcreate_and_X(struct smb_request *req)
437 connection_struct *conn = req->conn;
438 struct smb_filename *smb_fname = NULL;
442 uint32 file_attributes;
444 uint32 create_disposition;
445 uint32 create_options;
447 uint64_t allocation_size;
448 /* Breakout the oplock request bits so we can set the
449 reply bits separately. */
453 files_struct *fsp = NULL;
455 struct timespec create_timespec;
456 struct timespec c_timespec;
457 struct timespec a_timespec;
458 struct timespec m_timespec;
459 struct timespec write_time_ts;
462 uint8_t oplock_granted = NO_OPLOCK_RETURN;
463 struct case_semantics_state *case_state = NULL;
464 TALLOC_CTX *ctx = talloc_tos();
466 START_PROFILE(SMBntcreateX);
469 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
473 flags = IVAL(req->vwv+3, 1);
474 access_mask = IVAL(req->vwv+7, 1);
475 file_attributes = IVAL(req->vwv+13, 1);
476 share_access = IVAL(req->vwv+15, 1);
477 create_disposition = IVAL(req->vwv+17, 1);
478 create_options = IVAL(req->vwv+19, 1);
479 root_dir_fid = (uint16)IVAL(req->vwv+5, 1);
481 allocation_size = 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_CREATING_FILE : 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;
659 /* Deal with other possible opens having a modified
661 ZERO_STRUCT(write_time_ts);
662 get_file_infos(fsp->file_id, 0, NULL, &write_time_ts);
663 if (!null_timespec(write_time_ts)) {
664 update_stat_ex_mtime(&smb_fname->st, write_time_ts);
668 create_timespec = get_create_timespec(conn, fsp, smb_fname);
669 a_timespec = smb_fname->st.st_ex_atime;
670 m_timespec = smb_fname->st.st_ex_mtime;
671 c_timespec = get_change_timespec(conn, fsp, smb_fname);
673 if (lp_dos_filetime_resolution(SNUM(conn))) {
674 dos_filetime_timespec(&create_timespec);
675 dos_filetime_timespec(&a_timespec);
676 dos_filetime_timespec(&m_timespec);
677 dos_filetime_timespec(&c_timespec);
680 put_long_date_timespec(conn->ts_res, p, create_timespec); /* create time. */
682 put_long_date_timespec(conn->ts_res, p, a_timespec); /* access time */
684 put_long_date_timespec(conn->ts_res, p, m_timespec); /* write time */
686 put_long_date_timespec(conn->ts_res, p, c_timespec); /* change time */
688 SIVAL(p,0,fattr); /* File Attributes. */
690 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn,fsp,&smb_fname->st));
692 SOFF_T(p,0,file_len);
694 if (flags & EXTENDED_RESPONSE_REQUIRED) {
695 uint16_t file_status = (NO_EAS|NO_SUBSTREAMS|NO_REPARSETAG);
696 size_t num_names = 0;
697 unsigned int num_streams = 0;
698 struct stream_struct *streams = NULL;
700 /* Do we have any EA's ? */
701 status = get_ea_names_from_file(ctx, conn, fsp,
702 smb_fname->base_name, NULL, &num_names);
703 if (NT_STATUS_IS_OK(status) && num_names) {
704 file_status &= ~NO_EAS;
706 status = vfs_streaminfo(conn, NULL, smb_fname->base_name, ctx,
707 &num_streams, &streams);
708 /* There is always one stream, ::$DATA. */
709 if (NT_STATUS_IS_OK(status) && num_streams > 1) {
710 file_status &= ~NO_SUBSTREAMS;
712 TALLOC_FREE(streams);
713 SSVAL(p,2,file_status);
716 SCVAL(p,0,fsp->is_directory ? 1 : 0);
718 if (flags & EXTENDED_RESPONSE_REQUIRED) {
721 if (fsp->is_directory ||
723 can_write_to_file(conn, smb_fname)) {
724 perms = FILE_GENERIC_ALL;
726 perms = FILE_GENERIC_READ|FILE_EXECUTE;
731 DEBUG(5,("reply_ntcreate_and_X: %s, open name = %s\n",
732 fsp_fnum_dbg(fsp), smb_fname_str_dbg(smb_fname)));
735 END_PROFILE(SMBntcreateX);
739 /****************************************************************************
740 Reply to a NT_TRANSACT_CREATE call to open a pipe.
741 ****************************************************************************/
743 static void do_nt_transact_create_pipe(connection_struct *conn,
744 struct smb_request *req,
745 uint16 **ppsetup, uint32 setup_count,
746 char **ppparams, uint32 parameter_count,
747 char **ppdata, uint32 data_count)
750 char *params = *ppparams;
751 uint16_t pnum = FNUM_FIELD_INVALID;
756 TALLOC_CTX *ctx = talloc_tos();
759 * Ensure minimum number of parameters sent.
762 if(parameter_count < 54) {
763 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
764 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
768 flags = IVAL(params,0);
770 srvstr_get_path(ctx, params, req->flags2, &fname, params+53,
771 parameter_count-53, STR_TERMINATE,
773 if (!NT_STATUS_IS_OK(status)) {
774 reply_nterror(req, status);
778 nt_open_pipe(fname, conn, req, &pnum);
785 /* Realloc the size of parameters and data we will return */
786 if (flags & EXTENDED_RESPONSE_REQUIRED) {
787 /* Extended response is 32 more byyes. */
792 params = nttrans_realloc(ppparams, param_len);
794 reply_nterror(req, NT_STATUS_NO_MEMORY);
799 SCVAL(p,0,NO_OPLOCK_RETURN);
804 SIVAL(p,0,FILE_WAS_OPENED);
808 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
811 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
813 SSVAL(p,2, 0x5FF); /* ? */
816 if (flags & EXTENDED_RESPONSE_REQUIRED) {
818 SIVAL(p,0,FILE_GENERIC_ALL);
820 * For pipes W2K3 seems to return
822 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
824 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
827 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
829 /* Send the required number of replies */
830 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
835 /*********************************************************************
836 Windows seems to do canonicalization of inheritance bits. Do the
838 *********************************************************************/
840 static void canonicalize_inheritance_bits(struct security_descriptor *psd)
842 bool set_auto_inherited = false;
845 * We need to filter out the
846 * SEC_DESC_DACL_AUTO_INHERITED|SEC_DESC_DACL_AUTO_INHERIT_REQ
847 * bits. If both are set we store SEC_DESC_DACL_AUTO_INHERITED
848 * as this alters whether SEC_ACE_FLAG_INHERITED_ACE is set
849 * when an ACE is inherited. Otherwise we zero these bits out.
852 * http://social.msdn.microsoft.com/Forums/eu/os_fileservices/thread/11f77b68-731e-407d-b1b3-064750716531
857 if ((psd->type & (SEC_DESC_DACL_AUTO_INHERITED|SEC_DESC_DACL_AUTO_INHERIT_REQ))
858 == (SEC_DESC_DACL_AUTO_INHERITED|SEC_DESC_DACL_AUTO_INHERIT_REQ)) {
859 set_auto_inherited = true;
862 psd->type &= ~(SEC_DESC_DACL_AUTO_INHERITED|SEC_DESC_DACL_AUTO_INHERIT_REQ);
863 if (set_auto_inherited) {
864 psd->type |= SEC_DESC_DACL_AUTO_INHERITED;
868 /****************************************************************************
869 Internal fn to set security descriptors.
870 ****************************************************************************/
872 NTSTATUS set_sd(files_struct *fsp, struct security_descriptor *psd,
873 uint32_t security_info_sent)
877 if (!CAN_WRITE(fsp->conn)) {
878 return NT_STATUS_ACCESS_DENIED;
881 if (!lp_nt_acl_support(SNUM(fsp->conn))) {
885 if (psd->owner_sid == NULL) {
886 security_info_sent &= ~SECINFO_OWNER;
888 if (psd->group_sid == NULL) {
889 security_info_sent &= ~SECINFO_GROUP;
892 /* Ensure we have at least one thing set. */
893 if ((security_info_sent & (SECINFO_OWNER|SECINFO_GROUP|SECINFO_DACL|SECINFO_SACL)) == 0) {
898 /* Ensure we have the rights to do this. */
899 if (security_info_sent & SECINFO_OWNER) {
900 if (!(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
901 return NT_STATUS_ACCESS_DENIED;
905 if (security_info_sent & SECINFO_GROUP) {
906 if (!(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
907 return NT_STATUS_ACCESS_DENIED;
911 if (security_info_sent & SECINFO_DACL) {
912 if (!(fsp->access_mask & SEC_STD_WRITE_DAC)) {
913 return NT_STATUS_ACCESS_DENIED;
915 /* Convert all the generic bits. */
917 security_acl_map_generic(psd->dacl, &file_generic_mapping);
921 if (security_info_sent & SECINFO_SACL) {
922 if (!(fsp->access_mask & SEC_FLAG_SYSTEM_SECURITY)) {
923 return NT_STATUS_ACCESS_DENIED;
925 /* Convert all the generic bits. */
927 security_acl_map_generic(psd->sacl, &file_generic_mapping);
931 canonicalize_inheritance_bits(psd);
933 if (DEBUGLEVEL >= 10) {
934 DEBUG(10,("set_sd for file %s\n", fsp_str_dbg(fsp)));
935 NDR_PRINT_DEBUG(security_descriptor, psd);
938 status = SMB_VFS_FSET_NT_ACL(fsp, security_info_sent, psd);
945 /****************************************************************************
946 Internal fn to set security descriptors from a data blob.
947 ****************************************************************************/
949 NTSTATUS set_sd_blob(files_struct *fsp, uint8_t *data, uint32_t sd_len,
950 uint32_t security_info_sent)
952 struct security_descriptor *psd = NULL;
956 return NT_STATUS_INVALID_PARAMETER;
959 status = unmarshall_sec_desc(talloc_tos(), data, sd_len, &psd);
961 if (!NT_STATUS_IS_OK(status)) {
965 return set_sd(fsp, psd, security_info_sent);
968 /****************************************************************************
969 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
970 ****************************************************************************/
972 struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
974 struct ea_list *ea_list_head = NULL;
981 while (offset + 4 <= data_size) {
982 size_t next_offset = IVAL(pdata,offset);
983 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
989 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
990 if (next_offset == 0) {
994 /* Integer wrap protection for the increment. */
995 if (offset + next_offset < offset) {
999 offset += next_offset;
1001 /* Integer wrap protection for while loop. */
1002 if (offset + 4 < offset) {
1008 return ea_list_head;
1011 /****************************************************************************
1012 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
1013 ****************************************************************************/
1015 static void call_nt_transact_create(connection_struct *conn,
1016 struct smb_request *req,
1017 uint16 **ppsetup, uint32 setup_count,
1018 char **ppparams, uint32 parameter_count,
1019 char **ppdata, uint32 data_count,
1020 uint32 max_data_count)
1022 struct smb_filename *smb_fname = NULL;
1024 char *params = *ppparams;
1025 char *data = *ppdata;
1026 /* Breakout the oplock request bits so we can set the reply bits separately. */
1030 files_struct *fsp = NULL;
1034 uint32 file_attributes;
1035 uint32 share_access;
1036 uint32 create_disposition;
1037 uint32 create_options;
1039 struct security_descriptor *sd = NULL;
1041 uint16 root_dir_fid;
1042 struct timespec create_timespec;
1043 struct timespec c_timespec;
1044 struct timespec a_timespec;
1045 struct timespec m_timespec;
1046 struct timespec write_time_ts;
1047 struct ea_list *ea_list = NULL;
1050 uint64_t allocation_size;
1052 uint8_t oplock_granted;
1053 struct case_semantics_state *case_state = NULL;
1054 TALLOC_CTX *ctx = talloc_tos();
1056 DEBUG(5,("call_nt_transact_create\n"));
1059 * If it's an IPC, use the pipe handler.
1063 if (lp_nt_pipe_support()) {
1064 do_nt_transact_create_pipe(
1066 ppsetup, setup_count,
1067 ppparams, parameter_count,
1068 ppdata, data_count);
1071 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1076 * Ensure minimum number of parameters sent.
1079 if(parameter_count < 54) {
1080 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1081 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1085 flags = IVAL(params,0);
1086 access_mask = IVAL(params,8);
1087 file_attributes = IVAL(params,20);
1088 share_access = IVAL(params,24);
1089 create_disposition = IVAL(params,28);
1090 create_options = IVAL(params,32);
1091 sd_len = IVAL(params,36);
1092 ea_len = IVAL(params,40);
1093 root_dir_fid = (uint16)IVAL(params,4);
1094 allocation_size = BVAL(params,12);
1097 * we need to remove ignored bits when they come directly from the client
1098 * because we reuse some of them for internal stuff
1100 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
1102 srvstr_get_path(ctx, params, req->flags2, &fname,
1103 params+53, parameter_count-53,
1104 STR_TERMINATE, &status);
1105 if (!NT_STATUS_IS_OK(status)) {
1106 reply_nterror(req, status);
1110 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1111 case_state = set_posix_case_semantics(ctx, conn);
1113 reply_nterror(req, NT_STATUS_NO_MEMORY);
1118 status = filename_convert(ctx,
1120 req->flags2 & FLAGS2_DFS_PATHNAMES,
1122 (create_disposition == FILE_CREATE)
1123 ? UCF_CREATING_FILE : 0,
1127 TALLOC_FREE(case_state);
1129 if (!NT_STATUS_IS_OK(status)) {
1130 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1131 reply_botherror(req,
1132 NT_STATUS_PATH_NOT_COVERED,
1133 ERRSRV, ERRbadpath);
1136 reply_nterror(req, status);
1140 /* Ensure the data_len is correct for the sd and ea values given. */
1141 if ((ea_len + sd_len > data_count)
1142 || (ea_len > data_count) || (sd_len > data_count)
1143 || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
1144 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
1145 "%u, data_count = %u\n", (unsigned int)ea_len,
1146 (unsigned int)sd_len, (unsigned int)data_count));
1147 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1152 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
1155 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
1157 if (!NT_STATUS_IS_OK(status)) {
1158 DEBUG(10, ("call_nt_transact_create: "
1159 "unmarshall_sec_desc failed: %s\n",
1160 nt_errstr(status)));
1161 reply_nterror(req, status);
1167 if (!lp_ea_support(SNUM(conn))) {
1168 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
1169 "EA's not supported.\n",
1170 (unsigned int)ea_len));
1171 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
1176 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
1177 "too small (should be more than 10)\n",
1178 (unsigned int)ea_len ));
1179 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1183 /* We have already checked that ea_len <= data_count here. */
1184 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
1186 if (ea_list == NULL) {
1187 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1191 if (ea_list_has_invalid_name(ea_list)) {
1192 /* Realloc the size of parameters and data we will return */
1193 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1194 /* Extended response is 32 more byyes. */
1199 params = nttrans_realloc(ppparams, param_len);
1200 if(params == NULL) {
1201 reply_nterror(req, NT_STATUS_NO_MEMORY);
1205 memset(params, '\0', param_len);
1206 send_nt_replies(conn, req, STATUS_INVALID_EA_NAME,
1207 params, param_len, NULL, 0);
1212 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1213 if (oplock_request) {
1214 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
1219 * Bug #6898 - clients using Windows opens should
1220 * never be able to set this attribute into the
1223 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
1225 status = SMB_VFS_CREATE_FILE(
1228 root_dir_fid, /* root_dir_fid */
1229 smb_fname, /* fname */
1230 access_mask, /* access_mask */
1231 share_access, /* share_access */
1232 create_disposition, /* create_disposition*/
1233 create_options, /* create_options */
1234 file_attributes, /* file_attributes */
1235 oplock_request, /* oplock_request */
1236 allocation_size, /* allocation_size */
1237 0, /* private_flags */
1239 ea_list, /* ea_list */
1243 if(!NT_STATUS_IS_OK(status)) {
1244 if (open_was_deferred(req->sconn, req->mid)) {
1245 /* We have re-scheduled this call, no error. */
1248 reply_openerror(req, status);
1252 /* Ensure we're pointing at the correct stat struct. */
1253 TALLOC_FREE(smb_fname);
1254 smb_fname = fsp->fsp_name;
1257 * If the caller set the extended oplock request bit
1258 * and we granted one (by whatever means) - set the
1259 * correct bit for extended oplock reply.
1262 if (oplock_request &&
1263 (lp_fake_oplocks(SNUM(conn))
1264 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
1267 * Exclusive oplock granted
1270 if (flags & REQUEST_BATCH_OPLOCK) {
1271 oplock_granted = BATCH_OPLOCK_RETURN;
1273 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
1275 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1276 oplock_granted = LEVEL_II_OPLOCK_RETURN;
1278 oplock_granted = NO_OPLOCK_RETURN;
1281 file_len = smb_fname->st.st_ex_size;
1283 /* Realloc the size of parameters and data we will return */
1284 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1285 /* Extended response is 32 more byyes. */
1290 params = nttrans_realloc(ppparams, param_len);
1291 if(params == NULL) {
1292 reply_nterror(req, NT_STATUS_NO_MEMORY);
1297 SCVAL(p, 0, oplock_granted);
1300 SSVAL(p,0,fsp->fnum);
1302 if ((create_disposition == FILE_SUPERSEDE)
1303 && (info == FILE_WAS_OVERWRITTEN)) {
1304 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1310 fattr = dos_mode(conn, smb_fname);
1312 fattr = FILE_ATTRIBUTE_NORMAL;
1315 /* Deal with other possible opens having a modified
1317 ZERO_STRUCT(write_time_ts);
1318 get_file_infos(fsp->file_id, 0, NULL, &write_time_ts);
1319 if (!null_timespec(write_time_ts)) {
1320 update_stat_ex_mtime(&smb_fname->st, write_time_ts);
1324 create_timespec = get_create_timespec(conn, fsp, smb_fname);
1325 a_timespec = smb_fname->st.st_ex_atime;
1326 m_timespec = smb_fname->st.st_ex_mtime;
1327 c_timespec = get_change_timespec(conn, fsp, smb_fname);
1329 if (lp_dos_filetime_resolution(SNUM(conn))) {
1330 dos_filetime_timespec(&create_timespec);
1331 dos_filetime_timespec(&a_timespec);
1332 dos_filetime_timespec(&m_timespec);
1333 dos_filetime_timespec(&c_timespec);
1336 put_long_date_timespec(conn->ts_res, p, create_timespec); /* create time. */
1338 put_long_date_timespec(conn->ts_res, p, a_timespec); /* access time */
1340 put_long_date_timespec(conn->ts_res, p, m_timespec); /* write time */
1342 put_long_date_timespec(conn->ts_res, p, c_timespec); /* change time */
1344 SIVAL(p,0,fattr); /* File Attributes. */
1346 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn, fsp, &smb_fname->st));
1348 SOFF_T(p,0,file_len);
1350 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1351 uint16_t file_status = (NO_EAS|NO_SUBSTREAMS|NO_REPARSETAG);
1352 size_t num_names = 0;
1353 unsigned int num_streams = 0;
1354 struct stream_struct *streams = NULL;
1356 /* Do we have any EA's ? */
1357 status = get_ea_names_from_file(ctx, conn, fsp,
1358 smb_fname->base_name, NULL, &num_names);
1359 if (NT_STATUS_IS_OK(status) && num_names) {
1360 file_status &= ~NO_EAS;
1362 status = vfs_streaminfo(conn, NULL, smb_fname->base_name, ctx,
1363 &num_streams, &streams);
1364 /* There is always one stream, ::$DATA. */
1365 if (NT_STATUS_IS_OK(status) && num_streams > 1) {
1366 file_status &= ~NO_SUBSTREAMS;
1368 TALLOC_FREE(streams);
1369 SSVAL(p,2,file_status);
1372 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1374 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1377 if (fsp->is_directory ||
1379 can_write_to_file(conn, smb_fname)) {
1380 perms = FILE_GENERIC_ALL;
1382 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1387 DEBUG(5,("call_nt_transact_create: open name = %s\n",
1388 smb_fname_str_dbg(smb_fname)));
1390 /* Send the required number of replies */
1391 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1396 /****************************************************************************
1397 Reply to a NT CANCEL request.
1398 conn POINTER CAN BE NULL HERE !
1399 ****************************************************************************/
1401 void reply_ntcancel(struct smb_request *req)
1404 * Go through and cancel any pending change notifies.
1407 START_PROFILE(SMBntcancel);
1408 srv_cancel_sign_response(req->sconn);
1409 remove_pending_change_notify_requests_by_mid(req->sconn, req->mid);
1410 remove_pending_lock_requests_by_mid_smb1(req->sconn, req->mid);
1412 DEBUG(3,("reply_ntcancel: cancel called on mid = %llu.\n",
1413 (unsigned long long)req->mid));
1415 END_PROFILE(SMBntcancel);
1419 /****************************************************************************
1421 ****************************************************************************/
1423 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
1424 connection_struct *conn,
1425 struct smb_request *req,
1426 struct smb_filename *smb_fname_src,
1427 struct smb_filename *smb_fname_dst,
1430 files_struct *fsp1,*fsp2;
1434 NTSTATUS status = NT_STATUS_OK;
1437 if (!CAN_WRITE(conn)) {
1438 status = NT_STATUS_MEDIA_WRITE_PROTECTED;
1442 /* Source must already exist. */
1443 if (!VALID_STAT(smb_fname_src->st)) {
1444 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
1448 /* Ensure attributes match. */
1449 fattr = dos_mode(conn, smb_fname_src);
1450 if ((fattr & ~attrs) & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)) {
1451 status = NT_STATUS_NO_SUCH_FILE;
1455 /* Disallow if dst file already exists. */
1456 if (VALID_STAT(smb_fname_dst->st)) {
1457 status = NT_STATUS_OBJECT_NAME_COLLISION;
1461 /* No links from a directory. */
1462 if (S_ISDIR(smb_fname_src->st.st_ex_mode)) {
1463 status = NT_STATUS_FILE_IS_A_DIRECTORY;
1467 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1468 smb_fname_str_dbg(smb_fname_src),
1469 smb_fname_str_dbg(smb_fname_dst)));
1471 status = SMB_VFS_CREATE_FILE(
1474 0, /* root_dir_fid */
1475 smb_fname_src, /* fname */
1476 FILE_READ_DATA|FILE_READ_ATTRIBUTES|
1477 FILE_READ_EA, /* access_mask */
1478 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1480 FILE_OPEN, /* create_disposition*/
1481 0, /* create_options */
1482 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
1483 NO_OPLOCK, /* oplock_request */
1484 0, /* allocation_size */
1485 0, /* private_flags */
1491 if (!NT_STATUS_IS_OK(status)) {
1495 status = SMB_VFS_CREATE_FILE(
1498 0, /* root_dir_fid */
1499 smb_fname_dst, /* fname */
1500 FILE_WRITE_DATA|FILE_WRITE_ATTRIBUTES|
1501 FILE_WRITE_EA, /* access_mask */
1502 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1504 FILE_CREATE, /* create_disposition*/
1505 0, /* create_options */
1506 fattr, /* file_attributes */
1507 NO_OPLOCK, /* oplock_request */
1508 0, /* allocation_size */
1509 0, /* private_flags */
1515 if (!NT_STATUS_IS_OK(status)) {
1516 close_file(NULL, fsp1, ERROR_CLOSE);
1520 if (smb_fname_src->st.st_ex_size) {
1521 ret = vfs_transfer_file(fsp1, fsp2, smb_fname_src->st.st_ex_size);
1525 * As we are opening fsp1 read-only we only expect
1526 * an error on close on fsp2 if we are out of space.
1527 * Thus we don't look at the error return from the
1530 close_file(NULL, fsp1, NORMAL_CLOSE);
1532 /* Ensure the modtime is set correctly on the destination file. */
1533 set_close_write_time(fsp2, smb_fname_src->st.st_ex_mtime);
1535 status = close_file(NULL, fsp2, NORMAL_CLOSE);
1537 /* Grrr. We have to do this as open_file_ntcreate adds FILE_ATTRIBUTE_ARCHIVE when it
1538 creates the file. This isn't the correct thing to do in the copy
1540 if (!parent_dirname(talloc_tos(), smb_fname_dst->base_name, &parent,
1542 status = NT_STATUS_NO_MEMORY;
1545 file_set_dosmode(conn, smb_fname_dst, fattr, parent, false);
1546 TALLOC_FREE(parent);
1548 if (ret < (off_t)smb_fname_src->st.st_ex_size) {
1549 status = NT_STATUS_DISK_FULL;
1553 if (!NT_STATUS_IS_OK(status)) {
1554 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1555 nt_errstr(status), smb_fname_str_dbg(smb_fname_src),
1556 smb_fname_str_dbg(smb_fname_dst)));
1562 /****************************************************************************
1563 Reply to a NT rename request.
1564 ****************************************************************************/
1566 void reply_ntrename(struct smb_request *req)
1568 connection_struct *conn = req->conn;
1569 struct smb_filename *smb_fname_old = NULL;
1570 struct smb_filename *smb_fname_new = NULL;
1571 char *oldname = NULL;
1572 char *newname = NULL;
1575 bool src_has_wcard = False;
1576 bool dest_has_wcard = False;
1578 uint32_t ucf_flags_src = 0;
1579 uint32_t ucf_flags_dst = 0;
1581 TALLOC_CTX *ctx = talloc_tos();
1582 bool stream_rename = false;
1584 START_PROFILE(SMBntrename);
1587 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1591 attrs = SVAL(req->vwv+0, 0);
1592 rename_type = SVAL(req->vwv+1, 0);
1594 p = (const char *)req->buf + 1;
1595 p += srvstr_get_path_req_wcard(ctx, req, &oldname, p, STR_TERMINATE,
1596 &status, &src_has_wcard);
1597 if (!NT_STATUS_IS_OK(status)) {
1598 reply_nterror(req, status);
1602 if (ms_has_wild(oldname)) {
1603 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1608 p += srvstr_get_path_req_wcard(ctx, req, &newname, p, STR_TERMINATE,
1609 &status, &dest_has_wcard);
1610 if (!NT_STATUS_IS_OK(status)) {
1611 reply_nterror(req, status);
1615 if (!lp_posix_pathnames()) {
1616 /* The newname must begin with a ':' if the
1617 oldname contains a ':'. */
1618 if (strchr_m(oldname, ':')) {
1619 if (newname[0] != ':') {
1620 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1623 stream_rename = true;
1628 * If this is a rename operation, allow wildcards and save the
1629 * destination's last component.
1631 if (rename_type == RENAME_FLAG_RENAME) {
1632 ucf_flags_src = UCF_COND_ALLOW_WCARD_LCOMP;
1633 ucf_flags_dst = UCF_COND_ALLOW_WCARD_LCOMP | UCF_SAVE_LCOMP;
1636 /* rename_internals() calls unix_convert(), so don't call it here. */
1637 status = filename_convert(ctx, conn,
1638 req->flags2 & FLAGS2_DFS_PATHNAMES,
1643 if (!NT_STATUS_IS_OK(status)) {
1644 if (NT_STATUS_EQUAL(status,
1645 NT_STATUS_PATH_NOT_COVERED)) {
1646 reply_botherror(req,
1647 NT_STATUS_PATH_NOT_COVERED,
1648 ERRSRV, ERRbadpath);
1651 reply_nterror(req, status);
1655 status = filename_convert(ctx, conn,
1656 req->flags2 & FLAGS2_DFS_PATHNAMES,
1661 if (!NT_STATUS_IS_OK(status)) {
1662 if (NT_STATUS_EQUAL(status,
1663 NT_STATUS_PATH_NOT_COVERED)) {
1664 reply_botherror(req,
1665 NT_STATUS_PATH_NOT_COVERED,
1666 ERRSRV, ERRbadpath);
1669 reply_nterror(req, status);
1673 if (stream_rename) {
1674 /* smb_fname_new must be the same as smb_fname_old. */
1675 TALLOC_FREE(smb_fname_new->base_name);
1676 smb_fname_new->base_name = talloc_strdup(smb_fname_new,
1677 smb_fname_old->base_name);
1678 if (!smb_fname_new->base_name) {
1679 reply_nterror(req, NT_STATUS_NO_MEMORY);
1684 DEBUG(3,("reply_ntrename: %s -> %s\n",
1685 smb_fname_str_dbg(smb_fname_old),
1686 smb_fname_str_dbg(smb_fname_new)));
1688 switch(rename_type) {
1689 case RENAME_FLAG_RENAME:
1690 status = rename_internals(ctx, conn, req,
1691 smb_fname_old, smb_fname_new,
1692 attrs, False, src_has_wcard,
1696 case RENAME_FLAG_HARD_LINK:
1697 if (src_has_wcard || dest_has_wcard) {
1699 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1701 status = hardlink_internals(ctx, conn,
1708 case RENAME_FLAG_COPY:
1709 if (src_has_wcard || dest_has_wcard) {
1711 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1713 status = copy_internals(ctx, conn, req,
1719 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1720 status = NT_STATUS_INVALID_PARAMETER;
1723 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1727 if (!NT_STATUS_IS_OK(status)) {
1728 if (open_was_deferred(req->sconn, req->mid)) {
1729 /* We have re-scheduled this call. */
1733 reply_nterror(req, status);
1737 reply_outbuf(req, 0, 0);
1739 END_PROFILE(SMBntrename);
1743 /****************************************************************************
1744 Reply to a notify change - queue the request and
1745 don't allow a directory to be opened.
1746 ****************************************************************************/
1748 static void smbd_smb1_notify_reply(struct smb_request *req,
1749 NTSTATUS error_code,
1750 uint8_t *buf, size_t len)
1752 send_nt_replies(req->conn, req, error_code, (char *)buf, len, NULL, 0);
1755 static void call_nt_transact_notify_change(connection_struct *conn,
1756 struct smb_request *req,
1760 uint32 parameter_count,
1761 char **ppdata, uint32 data_count,
1762 uint32 max_data_count,
1763 uint32 max_param_count)
1765 uint16 *setup = *ppsetup;
1771 if(setup_count < 6) {
1772 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1776 fsp = file_fsp(req, SVAL(setup,4));
1777 filter = IVAL(setup, 0);
1778 recursive = (SVAL(setup, 6) != 0) ? True : False;
1780 DEBUG(3,("call_nt_transact_notify_change\n"));
1783 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1788 char *filter_string;
1790 if (!(filter_string = notify_filter_string(NULL, filter))) {
1791 reply_nterror(req,NT_STATUS_NO_MEMORY);
1795 DEBUG(3,("call_nt_transact_notify_change: notify change "
1796 "called on %s, filter = %s, recursive = %d\n",
1797 fsp_str_dbg(fsp), filter_string, recursive));
1799 TALLOC_FREE(filter_string);
1802 if((!fsp->is_directory) || (conn != fsp->conn)) {
1803 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1807 if (fsp->notify == NULL) {
1809 status = change_notify_create(fsp, filter, recursive);
1811 if (!NT_STATUS_IS_OK(status)) {
1812 DEBUG(10, ("change_notify_create returned %s\n",
1813 nt_errstr(status)));
1814 reply_nterror(req, status);
1819 if (change_notify_fsp_has_changes(fsp)) {
1822 * We've got changes pending, respond immediately
1826 * TODO: write a torture test to check the filtering behaviour
1830 change_notify_reply(req,
1834 smbd_smb1_notify_reply);
1837 * change_notify_reply() above has independently sent its
1844 * No changes pending, queue the request
1847 status = change_notify_add_request(req,
1851 smbd_smb1_notify_reply);
1852 if (!NT_STATUS_IS_OK(status)) {
1853 reply_nterror(req, status);
1858 /****************************************************************************
1859 Reply to an NT transact rename command.
1860 ****************************************************************************/
1862 static void call_nt_transact_rename(connection_struct *conn,
1863 struct smb_request *req,
1864 uint16 **ppsetup, uint32 setup_count,
1865 char **ppparams, uint32 parameter_count,
1866 char **ppdata, uint32 data_count,
1867 uint32 max_data_count)
1869 char *params = *ppparams;
1870 char *new_name = NULL;
1871 files_struct *fsp = NULL;
1872 bool dest_has_wcard = False;
1874 TALLOC_CTX *ctx = talloc_tos();
1876 if(parameter_count < 5) {
1877 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1881 fsp = file_fsp(req, SVAL(params, 0));
1882 if (!check_fsp(conn, req, fsp)) {
1885 srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
1886 parameter_count - 4,
1887 STR_TERMINATE, &status, &dest_has_wcard);
1888 if (!NT_STATUS_IS_OK(status)) {
1889 reply_nterror(req, status);
1894 * W2K3 ignores this request as the RAW-RENAME test
1895 * demonstrates, so we do.
1897 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1899 DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1900 fsp_str_dbg(fsp), new_name));
1905 /******************************************************************************
1906 Fake up a completely empty SD.
1907 *******************************************************************************/
1909 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, struct security_descriptor **ppsd)
1913 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1915 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1916 return NT_STATUS_NO_MEMORY;
1919 return NT_STATUS_OK;
1922 /****************************************************************************
1923 Reply to query a security descriptor.
1924 Callable from SMB2 and SMB2.
1925 If it returns NT_STATUS_BUFFER_TOO_SMALL, pdata_size is initialized with
1927 ****************************************************************************/
1929 NTSTATUS smbd_do_query_security_desc(connection_struct *conn,
1930 TALLOC_CTX *mem_ctx,
1932 uint32_t security_info_wanted,
1933 uint32_t max_data_count,
1934 uint8_t **ppmarshalled_sd,
1938 struct security_descriptor *psd = NULL;
1939 TALLOC_CTX *frame = talloc_stackframe();
1942 * Get the permissions to return.
1945 if ((security_info_wanted & SECINFO_SACL) &&
1946 !(fsp->access_mask & SEC_FLAG_SYSTEM_SECURITY)) {
1947 DEBUG(10, ("Access to SACL denied.\n"));
1949 return NT_STATUS_ACCESS_DENIED;
1952 if ((security_info_wanted & (SECINFO_DACL|SECINFO_OWNER|SECINFO_GROUP)) &&
1953 !(fsp->access_mask & SEC_STD_READ_CONTROL)) {
1954 DEBUG(10, ("Access to DACL, OWNER, or GROUP denied.\n"));
1956 return NT_STATUS_ACCESS_DENIED;
1959 if (security_info_wanted & (SECINFO_DACL|SECINFO_OWNER|
1960 SECINFO_GROUP|SECINFO_SACL)) {
1961 /* Don't return SECINFO_LABEL if anything else was
1962 requested. See bug #8458. */
1963 security_info_wanted &= ~SECINFO_LABEL;
1966 if (!lp_nt_acl_support(SNUM(conn))) {
1967 status = get_null_nt_acl(frame, &psd);
1968 } else if (security_info_wanted & SECINFO_LABEL) {
1969 /* Like W2K3 return a null object. */
1970 status = get_null_nt_acl(frame, &psd);
1972 status = SMB_VFS_FGET_NT_ACL(
1973 fsp, security_info_wanted, frame, &psd);
1975 if (!NT_STATUS_IS_OK(status)) {
1980 if (!(security_info_wanted & SECINFO_OWNER)) {
1981 psd->owner_sid = NULL;
1983 if (!(security_info_wanted & SECINFO_GROUP)) {
1984 psd->group_sid = NULL;
1986 if (!(security_info_wanted & SECINFO_DACL)) {
1987 psd->type &= ~SEC_DESC_DACL_PRESENT;
1990 if (!(security_info_wanted & SECINFO_SACL)) {
1991 psd->type &= ~SEC_DESC_SACL_PRESENT;
1995 /* If the SACL/DACL is NULL, but was requested, we mark that it is
1996 * present in the reply to match Windows behavior */
1997 if (psd->sacl == NULL &&
1998 security_info_wanted & SECINFO_SACL)
1999 psd->type |= SEC_DESC_SACL_PRESENT;
2000 if (psd->dacl == NULL &&
2001 security_info_wanted & SECINFO_DACL)
2002 psd->type |= SEC_DESC_DACL_PRESENT;
2004 if (security_info_wanted & SECINFO_LABEL) {
2005 /* Like W2K3 return a null object. */
2006 psd->owner_sid = NULL;
2007 psd->group_sid = NULL;
2010 psd->type &= ~(SEC_DESC_DACL_PRESENT|SEC_DESC_SACL_PRESENT);
2013 *psd_size = ndr_size_security_descriptor(psd, 0);
2015 DEBUG(3,("smbd_do_query_security_desc: sd_size = %lu.\n",
2016 (unsigned long)*psd_size));
2018 if (DEBUGLEVEL >= 10) {
2019 DEBUG(10,("smbd_do_query_security_desc for file %s\n",
2021 NDR_PRINT_DEBUG(security_descriptor, psd);
2024 if (max_data_count < *psd_size) {
2026 return NT_STATUS_BUFFER_TOO_SMALL;
2029 status = marshall_sec_desc(mem_ctx, psd,
2030 ppmarshalled_sd, psd_size);
2032 if (!NT_STATUS_IS_OK(status)) {
2038 return NT_STATUS_OK;
2041 /****************************************************************************
2042 SMB1 reply to query a security descriptor.
2043 ****************************************************************************/
2045 static void call_nt_transact_query_security_desc(connection_struct *conn,
2046 struct smb_request *req,
2050 uint32 parameter_count,
2053 uint32 max_data_count)
2055 char *params = *ppparams;
2056 char *data = *ppdata;
2058 uint32 security_info_wanted;
2059 files_struct *fsp = NULL;
2061 uint8_t *marshalled_sd = NULL;
2063 if(parameter_count < 8) {
2064 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2068 fsp = file_fsp(req, SVAL(params,0));
2070 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2074 security_info_wanted = IVAL(params,4);
2076 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, "
2077 "info_wanted = 0x%x\n", fsp_str_dbg(fsp),
2078 (unsigned int)security_info_wanted));
2080 params = nttrans_realloc(ppparams, 4);
2081 if(params == NULL) {
2082 reply_nterror(req, NT_STATUS_NO_MEMORY);
2087 * Get the permissions to return.
2090 status = smbd_do_query_security_desc(conn,
2093 security_info_wanted,
2098 if (NT_STATUS_EQUAL(status, NT_STATUS_BUFFER_TOO_SMALL)) {
2099 SIVAL(params,0,(uint32_t)sd_size);
2100 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
2101 params, 4, NULL, 0);
2105 if (!NT_STATUS_IS_OK(status)) {
2106 reply_nterror(req, status);
2110 SMB_ASSERT(sd_size > 0);
2112 SIVAL(params,0,(uint32_t)sd_size);
2114 if (max_data_count < sd_size) {
2115 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
2116 params, 4, NULL, 0);
2121 * Allocate the data we will return.
2124 data = nttrans_realloc(ppdata, sd_size);
2126 reply_nterror(req, NT_STATUS_NO_MEMORY);
2130 memcpy(data, marshalled_sd, sd_size);
2132 send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
2137 /****************************************************************************
2138 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
2139 ****************************************************************************/
2141 static void call_nt_transact_set_security_desc(connection_struct *conn,
2142 struct smb_request *req,
2146 uint32 parameter_count,
2149 uint32 max_data_count)
2151 char *params= *ppparams;
2152 char *data = *ppdata;
2153 files_struct *fsp = NULL;
2154 uint32 security_info_sent = 0;
2157 if(parameter_count < 8) {
2158 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2162 if((fsp = file_fsp(req, SVAL(params,0))) == NULL) {
2163 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2167 if (!CAN_WRITE(fsp->conn)) {
2168 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2172 if(!lp_nt_acl_support(SNUM(conn))) {
2176 security_info_sent = IVAL(params,4);
2178 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n",
2179 fsp_str_dbg(fsp), (unsigned int)security_info_sent));
2181 if (data_count == 0) {
2182 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2186 status = set_sd_blob(fsp, (uint8 *)data, data_count, security_info_sent);
2188 if (!NT_STATUS_IS_OK(status)) {
2189 reply_nterror(req, status);
2194 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2198 /****************************************************************************
2200 ****************************************************************************/
2202 static void call_nt_transact_ioctl(connection_struct *conn,
2203 struct smb_request *req,
2204 uint16 **ppsetup, uint32 setup_count,
2205 char **ppparams, uint32 parameter_count,
2206 char **ppdata, uint32 data_count,
2207 uint32 max_data_count)
2215 char *out_data = NULL;
2216 uint32 out_data_len = 0;
2217 char *pdata = *ppdata;
2218 TALLOC_CTX *ctx = talloc_tos();
2220 if (setup_count != 8) {
2221 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
2222 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2226 function = IVAL(*ppsetup, 0);
2227 fidnum = SVAL(*ppsetup, 4);
2228 isFSctl = CVAL(*ppsetup, 6);
2229 compfilter = CVAL(*ppsetup, 7);
2231 DEBUG(10, ("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
2232 function, fidnum, isFSctl, compfilter));
2234 fsp=file_fsp(req, fidnum);
2237 * We don't really implement IOCTLs, especially on files.
2240 DEBUG(10, ("isFSctl: 0x%02X indicates IOCTL, not FSCTL!\n",
2242 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2246 /* Has to be for an open file! */
2247 if (!check_fsp_open(conn, req, fsp)) {
2251 SMB_PERFCOUNT_SET_IOCTL(&req->pcd, function);
2254 * out_data might be allocated by the VFS module, but talloc should be
2255 * used, and should be cleaned up when the request ends.
2257 status = SMB_VFS_FSCTL(fsp,
2263 (uint8_t **)&out_data,
2266 if (!NT_STATUS_IS_OK(status)) {
2267 reply_nterror(req, status);
2269 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, out_data, out_data_len);
2274 #ifdef HAVE_SYS_QUOTAS
2275 /****************************************************************************
2276 Reply to get user quota
2277 ****************************************************************************/
2279 static void call_nt_transact_get_user_quota(connection_struct *conn,
2280 struct smb_request *req,
2284 uint32 parameter_count,
2287 uint32 max_data_count)
2289 NTSTATUS nt_status = NT_STATUS_OK;
2290 char *params = *ppparams;
2291 char *pdata = *ppdata;
2293 int data_len=0,param_len=0;
2296 files_struct *fsp = NULL;
2300 bool start_enum = True;
2301 SMB_NTQUOTA_STRUCT qt;
2302 SMB_NTQUOTA_LIST *tmp_list;
2303 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2308 if (get_current_uid(conn) != 0) {
2309 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2310 "[%s]\n", lp_servicename(talloc_tos(), SNUM(conn)),
2311 conn->session_info->unix_info->unix_name));
2312 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2317 * Ensure minimum number of parameters sent.
2320 if (parameter_count < 4) {
2321 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2322 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2326 /* maybe we can check the quota_fnum */
2327 fsp = file_fsp(req, SVAL(params,0));
2328 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2329 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2330 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2334 /* the NULL pointer checking for fsp->fake_file_handle->pd
2335 * is done by CHECK_NTQUOTA_HANDLE_OK()
2337 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->private_data;
2339 level = SVAL(params,2);
2341 /* unknown 12 bytes leading in params */
2344 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2345 /* seems that we should continue with the enum here --metze */
2347 if (qt_handle->quota_list!=NULL &&
2348 qt_handle->tmp_list==NULL) {
2351 free_ntquota_list(&(qt_handle->quota_list));
2353 /* Realloc the size of parameters and data we will return */
2355 params = nttrans_realloc(ppparams, param_len);
2356 if(params == NULL) {
2357 reply_nterror(req, NT_STATUS_NO_MEMORY);
2362 SIVAL(params,0,data_len);
2369 case TRANSACT_GET_USER_QUOTA_LIST_START:
2371 if (qt_handle->quota_list==NULL &&
2372 qt_handle->tmp_list==NULL) {
2376 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2377 reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
2381 /* Realloc the size of parameters and data we will return */
2383 params = nttrans_realloc(ppparams, param_len);
2384 if(params == NULL) {
2385 reply_nterror(req, NT_STATUS_NO_MEMORY);
2389 /* we should not trust the value in max_data_count*/
2390 max_data_count = MIN(max_data_count,2048);
2392 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2394 reply_nterror(req, NT_STATUS_NO_MEMORY);
2400 /* set params Size of returned Quota Data 4 bytes*/
2401 /* but set it later when we know it */
2403 /* for each entry push the data */
2406 qt_handle->tmp_list = qt_handle->quota_list;
2409 tmp_list = qt_handle->tmp_list;
2411 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2412 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2414 sid_len = ndr_size_dom_sid(
2415 &tmp_list->quotas->sid, 0);
2416 entry_len = 40 + sid_len;
2418 /* nextoffset entry 4 bytes */
2419 SIVAL(entry,0,entry_len);
2421 /* then the len of the SID 4 bytes */
2422 SIVAL(entry,4,sid_len);
2424 /* unknown data 8 bytes uint64_t */
2425 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-metze*/
2427 /* the used disk space 8 bytes uint64_t */
2428 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2430 /* the soft quotas 8 bytes uint64_t */
2431 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2433 /* the hard quotas 8 bytes uint64_t */
2434 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2436 /* and now the SID */
2437 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2440 qt_handle->tmp_list = tmp_list;
2442 /* overwrite the offset of the last entry */
2443 SIVAL(entry-entry_len,0,0);
2445 data_len = 4+qt_len;
2446 /* overwrite the params quota_data_len */
2447 SIVAL(params,0,data_len);
2451 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2453 /* unknown 4 bytes IVAL(pdata,0) */
2455 if (data_count < 8) {
2456 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2457 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2461 sid_len = IVAL(pdata,4);
2462 /* Ensure this is less than 1mb. */
2463 if (sid_len > (1024*1024)) {
2464 reply_nterror(req, NT_STATUS_NO_MEMORY);
2468 if (data_count < 8+sid_len) {
2469 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2470 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2474 data_len = 4+40+sid_len;
2476 if (max_data_count < data_len) {
2477 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2478 max_data_count, data_len));
2480 SIVAL(params,0,data_len);
2482 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2486 if (!sid_parse(pdata+8,sid_len,&sid)) {
2487 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2491 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2494 * we have to return zero's in all fields
2495 * instead of returning an error here
2500 /* Realloc the size of parameters and data we will return */
2502 params = nttrans_realloc(ppparams, param_len);
2503 if(params == NULL) {
2504 reply_nterror(req, NT_STATUS_NO_MEMORY);
2508 pdata = nttrans_realloc(ppdata, data_len);
2510 reply_nterror(req, NT_STATUS_NO_MEMORY);
2516 /* set params Size of returned Quota Data 4 bytes*/
2517 SIVAL(params,0,data_len);
2519 /* nextoffset entry 4 bytes */
2522 /* then the len of the SID 4 bytes */
2523 SIVAL(entry,4,sid_len);
2525 /* unknown data 8 bytes uint64_t */
2526 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-mezte*/
2528 /* the used disk space 8 bytes uint64_t */
2529 SBIG_UINT(entry,16,qt.usedspace);
2531 /* the soft quotas 8 bytes uint64_t */
2532 SBIG_UINT(entry,24,qt.softlim);
2534 /* the hard quotas 8 bytes uint64_t */
2535 SBIG_UINT(entry,32,qt.hardlim);
2537 /* and now the SID */
2538 sid_linearize(entry+40, sid_len, &sid);
2543 DEBUG(0, ("do_nt_transact_get_user_quota: %s: unknown "
2545 fsp_fnum_dbg(fsp), level));
2546 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2551 send_nt_replies(conn, req, nt_status, params, param_len,
2555 /****************************************************************************
2556 Reply to set user quota
2557 ****************************************************************************/
2559 static void call_nt_transact_set_user_quota(connection_struct *conn,
2560 struct smb_request *req,
2564 uint32 parameter_count,
2567 uint32 max_data_count)
2569 char *params = *ppparams;
2570 char *pdata = *ppdata;
2571 int data_len=0,param_len=0;
2572 SMB_NTQUOTA_STRUCT qt;
2575 files_struct *fsp = NULL;
2580 if (get_current_uid(conn) != 0) {
2581 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2582 "[%s]\n", lp_servicename(talloc_tos(), SNUM(conn)),
2583 conn->session_info->unix_info->unix_name));
2584 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2589 * Ensure minimum number of parameters sent.
2592 if (parameter_count < 2) {
2593 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2594 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2598 /* maybe we can check the quota_fnum */
2599 fsp = file_fsp(req, SVAL(params,0));
2600 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2601 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2602 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2606 if (data_count < 40) {
2607 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2608 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2612 /* offset to next quota record.
2613 * 4 bytes IVAL(pdata,0)
2618 sid_len = IVAL(pdata,4);
2620 if (data_count < 40+sid_len || (40+sid_len < sid_len)) {
2621 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2622 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2626 /* unknown 8 bytes in pdata
2627 * maybe its the change time in NTTIME
2630 /* the used space 8 bytes (uint64_t)*/
2631 qt.usedspace = BVAL(pdata,16);
2633 /* the soft quotas 8 bytes (uint64_t)*/
2634 qt.softlim = BVAL(pdata,24);
2636 /* the hard quotas 8 bytes (uint64_t)*/
2637 qt.hardlim = BVAL(pdata,32);
2639 if (!sid_parse(pdata+40,sid_len,&sid)) {
2640 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2644 DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid)));
2646 /* 44 unknown bytes left... */
2648 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2649 reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
2653 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2656 #endif /* HAVE_SYS_QUOTAS */
2658 static void handle_nttrans(connection_struct *conn,
2659 struct trans_state *state,
2660 struct smb_request *req)
2662 if (get_Protocol() >= PROTOCOL_NT1) {
2663 req->flags2 |= 0x40; /* IS_LONG_NAME */
2664 SSVAL(discard_const_p(uint8_t, req->inbuf),smb_flg2,req->flags2);
2668 SMB_PERFCOUNT_SET_SUBOP(&req->pcd, state->call);
2670 /* Now we must call the relevant NT_TRANS function */
2671 switch(state->call) {
2672 case NT_TRANSACT_CREATE:
2674 START_PROFILE(NT_transact_create);
2675 call_nt_transact_create(
2677 &state->setup, state->setup_count,
2678 &state->param, state->total_param,
2679 &state->data, state->total_data,
2680 state->max_data_return);
2681 END_PROFILE(NT_transact_create);
2685 case NT_TRANSACT_IOCTL:
2687 START_PROFILE(NT_transact_ioctl);
2688 call_nt_transact_ioctl(
2690 &state->setup, state->setup_count,
2691 &state->param, state->total_param,
2692 &state->data, state->total_data,
2693 state->max_data_return);
2694 END_PROFILE(NT_transact_ioctl);
2698 case NT_TRANSACT_SET_SECURITY_DESC:
2700 START_PROFILE(NT_transact_set_security_desc);
2701 call_nt_transact_set_security_desc(
2703 &state->setup, state->setup_count,
2704 &state->param, state->total_param,
2705 &state->data, state->total_data,
2706 state->max_data_return);
2707 END_PROFILE(NT_transact_set_security_desc);
2711 case NT_TRANSACT_NOTIFY_CHANGE:
2713 START_PROFILE(NT_transact_notify_change);
2714 call_nt_transact_notify_change(
2716 &state->setup, state->setup_count,
2717 &state->param, state->total_param,
2718 &state->data, state->total_data,
2719 state->max_data_return,
2720 state->max_param_return);
2721 END_PROFILE(NT_transact_notify_change);
2725 case NT_TRANSACT_RENAME:
2727 START_PROFILE(NT_transact_rename);
2728 call_nt_transact_rename(
2730 &state->setup, state->setup_count,
2731 &state->param, state->total_param,
2732 &state->data, state->total_data,
2733 state->max_data_return);
2734 END_PROFILE(NT_transact_rename);
2738 case NT_TRANSACT_QUERY_SECURITY_DESC:
2740 START_PROFILE(NT_transact_query_security_desc);
2741 call_nt_transact_query_security_desc(
2743 &state->setup, state->setup_count,
2744 &state->param, state->total_param,
2745 &state->data, state->total_data,
2746 state->max_data_return);
2747 END_PROFILE(NT_transact_query_security_desc);
2751 #ifdef HAVE_SYS_QUOTAS
2752 case NT_TRANSACT_GET_USER_QUOTA:
2754 START_PROFILE(NT_transact_get_user_quota);
2755 call_nt_transact_get_user_quota(
2757 &state->setup, state->setup_count,
2758 &state->param, state->total_param,
2759 &state->data, state->total_data,
2760 state->max_data_return);
2761 END_PROFILE(NT_transact_get_user_quota);
2765 case NT_TRANSACT_SET_USER_QUOTA:
2767 START_PROFILE(NT_transact_set_user_quota);
2768 call_nt_transact_set_user_quota(
2770 &state->setup, state->setup_count,
2771 &state->param, state->total_param,
2772 &state->data, state->total_data,
2773 state->max_data_return);
2774 END_PROFILE(NT_transact_set_user_quota);
2777 #endif /* HAVE_SYS_QUOTAS */
2780 /* Error in request */
2781 DEBUG(0,("handle_nttrans: Unknown request %d in "
2782 "nttrans call\n", state->call));
2783 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2789 /****************************************************************************
2790 Reply to a SMBNTtrans.
2791 ****************************************************************************/
2793 void reply_nttrans(struct smb_request *req)
2795 connection_struct *conn = req->conn;
2800 uint16 function_code;
2802 struct trans_state *state;
2804 START_PROFILE(SMBnttrans);
2806 if (req->wct < 19) {
2807 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2808 END_PROFILE(SMBnttrans);
2812 pscnt = IVAL(req->vwv+9, 1);
2813 psoff = IVAL(req->vwv+11, 1);
2814 dscnt = IVAL(req->vwv+13, 1);
2815 dsoff = IVAL(req->vwv+15, 1);
2816 function_code = SVAL(req->vwv+18, 0);
2818 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2819 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2820 END_PROFILE(SMBnttrans);
2824 result = allow_new_trans(conn->pending_trans, req->mid);
2825 if (!NT_STATUS_IS_OK(result)) {
2826 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
2827 reply_nterror(req, result);
2828 END_PROFILE(SMBnttrans);
2832 if ((state = talloc(conn, struct trans_state)) == NULL) {
2833 reply_nterror(req, NT_STATUS_NO_MEMORY);
2834 END_PROFILE(SMBnttrans);
2838 state->cmd = SMBnttrans;
2840 state->mid = req->mid;
2841 state->vuid = req->vuid;
2842 state->total_data = IVAL(req->vwv+3, 1);
2844 state->total_param = IVAL(req->vwv+1, 1);
2845 state->param = NULL;
2846 state->max_data_return = IVAL(req->vwv+7, 1);
2847 state->max_param_return = IVAL(req->vwv+5, 1);
2849 /* setup count is in *words* */
2850 state->setup_count = 2*CVAL(req->vwv+17, 1);
2851 state->setup = NULL;
2852 state->call = function_code;
2854 DEBUG(10, ("num_setup=%u, "
2855 "param_total=%u, this_param=%u, max_param=%u, "
2856 "data_total=%u, this_data=%u, max_data=%u, "
2857 "param_offset=%u, data_offset=%u\n",
2858 (unsigned)state->setup_count,
2859 (unsigned)state->total_param, (unsigned)pscnt,
2860 (unsigned)state->max_param_return,
2861 (unsigned)state->total_data, (unsigned)dscnt,
2862 (unsigned)state->max_data_return,
2863 (unsigned)psoff, (unsigned)dsoff));
2866 * All nttrans messages we handle have smb_wct == 19 +
2867 * state->setup_count. Ensure this is so as a sanity check.
2870 if(req->wct != 19 + (state->setup_count/2)) {
2871 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2872 req->wct, 19 + (state->setup_count/2)));
2876 /* Don't allow more than 128mb for each value. */
2877 if ((state->total_data > (1024*1024*128)) ||
2878 (state->total_param > (1024*1024*128))) {
2879 reply_nterror(req, NT_STATUS_NO_MEMORY);
2880 END_PROFILE(SMBnttrans);
2884 if ((dscnt > state->total_data) || (pscnt > state->total_param))
2887 if (state->total_data) {
2889 if (trans_oob(state->total_data, 0, dscnt)
2890 || trans_oob(smb_len(req->inbuf), dsoff, dscnt)) {
2894 /* Can't use talloc here, the core routines do realloc on the
2895 * params and data. */
2896 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
2897 DEBUG(0,("reply_nttrans: data malloc fail for %u "
2898 "bytes !\n", (unsigned int)state->total_data));
2900 reply_nterror(req, NT_STATUS_NO_MEMORY);
2901 END_PROFILE(SMBnttrans);
2905 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
2908 if (state->total_param) {
2910 if (trans_oob(state->total_param, 0, pscnt)
2911 || trans_oob(smb_len(req->inbuf), psoff, pscnt)) {
2915 /* Can't use talloc here, the core routines do realloc on the
2916 * params and data. */
2917 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
2918 DEBUG(0,("reply_nttrans: param malloc fail for %u "
2919 "bytes !\n", (unsigned int)state->total_param));
2920 SAFE_FREE(state->data);
2922 reply_nterror(req, NT_STATUS_NO_MEMORY);
2923 END_PROFILE(SMBnttrans);
2927 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
2930 state->received_data = dscnt;
2931 state->received_param = pscnt;
2933 if(state->setup_count > 0) {
2934 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2935 state->setup_count));
2938 * No overflow possible here, state->setup_count is an
2939 * unsigned int, being filled by a single byte from
2940 * CVAL(req->vwv+13, 0) above. The cast in the comparison
2941 * below is not necessary, it's here to clarify things. The
2942 * validity of req->vwv and req->wct has been checked in
2943 * init_smb_request already.
2945 if ((state->setup_count/2) + 19 > (unsigned int)req->wct) {
2949 state->setup = (uint16 *)TALLOC(state, state->setup_count);
2950 if (state->setup == NULL) {
2951 DEBUG(0,("reply_nttrans : Out of memory\n"));
2952 SAFE_FREE(state->data);
2953 SAFE_FREE(state->param);
2955 reply_nterror(req, NT_STATUS_NO_MEMORY);
2956 END_PROFILE(SMBnttrans);
2960 memcpy(state->setup, req->vwv+19, state->setup_count);
2961 dump_data(10, (uint8 *)state->setup, state->setup_count);
2964 if ((state->received_data == state->total_data) &&
2965 (state->received_param == state->total_param)) {
2966 handle_nttrans(conn, state, req);
2967 SAFE_FREE(state->param);
2968 SAFE_FREE(state->data);
2970 END_PROFILE(SMBnttrans);
2974 DLIST_ADD(conn->pending_trans, state);
2976 /* We need to send an interim response then receive the rest
2977 of the parameter/data bytes */
2978 reply_outbuf(req, 0, 0);
2979 show_msg((char *)req->outbuf);
2980 END_PROFILE(SMBnttrans);
2985 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
2986 SAFE_FREE(state->data);
2987 SAFE_FREE(state->param);
2989 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2990 END_PROFILE(SMBnttrans);
2994 /****************************************************************************
2995 Reply to a SMBnttranss
2996 ****************************************************************************/
2998 void reply_nttranss(struct smb_request *req)
3000 connection_struct *conn = req->conn;
3001 uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
3002 struct trans_state *state;
3004 START_PROFILE(SMBnttranss);
3006 show_msg((const char *)req->inbuf);
3008 /* Windows clients expect all replies to
3009 an NT transact secondary (SMBnttranss 0xA1)
3010 to have a command code of NT transact
3011 (SMBnttrans 0xA0). See bug #8989 for details. */
3012 req->cmd = SMBnttrans;
3014 if (req->wct < 18) {
3015 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3016 END_PROFILE(SMBnttranss);
3020 for (state = conn->pending_trans; state != NULL;
3021 state = state->next) {
3022 if (state->mid == req->mid) {
3027 if ((state == NULL) || (state->cmd != SMBnttrans)) {
3028 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3029 END_PROFILE(SMBnttranss);
3033 /* Revise state->total_param and state->total_data in case they have
3034 changed downwards */
3035 if (IVAL(req->vwv+1, 1) < state->total_param) {
3036 state->total_param = IVAL(req->vwv+1, 1);
3038 if (IVAL(req->vwv+3, 1) < state->total_data) {
3039 state->total_data = IVAL(req->vwv+3, 1);
3042 pcnt = IVAL(req->vwv+5, 1);
3043 poff = IVAL(req->vwv+7, 1);
3044 pdisp = IVAL(req->vwv+9, 1);
3046 dcnt = IVAL(req->vwv+11, 1);
3047 doff = IVAL(req->vwv+13, 1);
3048 ddisp = IVAL(req->vwv+15, 1);
3050 state->received_param += pcnt;
3051 state->received_data += dcnt;
3053 if ((state->received_data > state->total_data) ||
3054 (state->received_param > state->total_param))
3058 if (trans_oob(state->total_param, pdisp, pcnt)
3059 || trans_oob(smb_len(req->inbuf), poff, pcnt)) {
3062 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,pcnt);
3066 if (trans_oob(state->total_data, ddisp, dcnt)
3067 || trans_oob(smb_len(req->inbuf), doff, dcnt)) {
3070 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
3073 if ((state->received_param < state->total_param) ||
3074 (state->received_data < state->total_data)) {
3075 END_PROFILE(SMBnttranss);
3079 handle_nttrans(conn, state, req);
3081 DLIST_REMOVE(conn->pending_trans, state);
3082 SAFE_FREE(state->data);
3083 SAFE_FREE(state->param);
3085 END_PROFILE(SMBnttranss);
3090 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
3091 DLIST_REMOVE(conn->pending_trans, state);
3092 SAFE_FREE(state->data);
3093 SAFE_FREE(state->param);
3095 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3096 END_PROFILE(SMBnttranss);