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"
32 extern const struct generic_mapping file_generic_mapping;
34 static char *nttrans_realloc(char **ptr, size_t size)
37 smb_panic("nttrans_realloc() called with NULL ptr");
40 *ptr = (char *)SMB_REALLOC(*ptr, size);
44 memset(*ptr,'\0',size);
48 /****************************************************************************
49 Send the required number of replies back.
50 We assume all fields other than the data fields are
51 set correctly for the type of call.
52 HACK ! Always assumes smb_setup field is zero.
53 ****************************************************************************/
55 static void send_nt_replies(connection_struct *conn,
56 struct smb_request *req, NTSTATUS nt_error,
57 char *params, int paramsize,
58 char *pdata, int datasize)
60 int data_to_send = datasize;
61 int params_to_send = paramsize;
65 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
66 int alignment_offset = 1;
67 int data_alignment_offset = 0;
68 struct smbd_server_connection *sconn = req->sconn;
69 int max_send = sconn->smb1.sessions.max_send;
72 * If there genuinely are no parameters or data to send just send
76 if(params_to_send == 0 && data_to_send == 0) {
77 reply_outbuf(req, 18, 0);
78 if (NT_STATUS_V(nt_error)) {
79 error_packet_set((char *)req->outbuf,
83 show_msg((char *)req->outbuf);
84 if (!srv_send_smb(sconn,
87 IS_CONN_ENCRYPTED(conn),
89 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
91 TALLOC_FREE(req->outbuf);
96 * When sending params and data ensure that both are nicely aligned.
97 * Only do this alignment when there is also data to send - else
98 * can cause NT redirector problems.
101 if (((params_to_send % 4) != 0) && (data_to_send != 0)) {
102 data_alignment_offset = 4 - (params_to_send % 4);
106 * Space is bufsize minus Netbios over TCP header minus SMB header.
107 * The alignment_offset is to align the param bytes on a four byte
108 * boundary (2 bytes for data len, one byte pad).
109 * NT needs this to work correctly.
112 useable_space = max_send - (smb_size
115 + data_alignment_offset);
117 if (useable_space < 0) {
118 char *msg = talloc_asprintf(
120 "send_nt_replies failed sanity useable_space = %d!!!",
122 DEBUG(0, ("%s\n", msg));
123 exit_server_cleanly(msg);
126 while (params_to_send || data_to_send) {
129 * Calculate whether we will totally or partially fill this packet.
132 total_sent_thistime = params_to_send + data_to_send;
135 * We can never send more than useable_space.
138 total_sent_thistime = MIN(total_sent_thistime, useable_space);
140 reply_outbuf(req, 18,
141 total_sent_thistime + alignment_offset
142 + data_alignment_offset);
145 * We might have had SMBnttranss in req->inbuf, fix that.
147 SCVAL(req->outbuf, smb_com, SMBnttrans);
150 * Set total params and data to be sent.
153 SIVAL(req->outbuf,smb_ntr_TotalParameterCount,paramsize);
154 SIVAL(req->outbuf,smb_ntr_TotalDataCount,datasize);
157 * Calculate how many parameters and data we can fit into
158 * this packet. Parameters get precedence.
161 params_sent_thistime = MIN(params_to_send,useable_space);
162 data_sent_thistime = useable_space - params_sent_thistime;
163 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
165 SIVAL(req->outbuf, smb_ntr_ParameterCount,
166 params_sent_thistime);
168 if(params_sent_thistime == 0) {
169 SIVAL(req->outbuf,smb_ntr_ParameterOffset,0);
170 SIVAL(req->outbuf,smb_ntr_ParameterDisplacement,0);
173 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
174 * parameter bytes, however the first 4 bytes of outbuf are
175 * the Netbios over TCP header. Thus use smb_base() to subtract
176 * them from the calculation.
179 SIVAL(req->outbuf,smb_ntr_ParameterOffset,
180 ((smb_buf(req->outbuf)+alignment_offset)
181 - smb_base(req->outbuf)));
183 * Absolute displacement of param bytes sent in this packet.
186 SIVAL(req->outbuf, smb_ntr_ParameterDisplacement,
191 * Deal with the data portion.
194 SIVAL(req->outbuf, smb_ntr_DataCount, data_sent_thistime);
196 if(data_sent_thistime == 0) {
197 SIVAL(req->outbuf,smb_ntr_DataOffset,0);
198 SIVAL(req->outbuf,smb_ntr_DataDisplacement, 0);
201 * The offset of the data bytes is the offset of the
202 * parameter bytes plus the number of parameters being sent this time.
205 SIVAL(req->outbuf, smb_ntr_DataOffset,
206 ((smb_buf(req->outbuf)+alignment_offset) -
207 smb_base(req->outbuf))
208 + params_sent_thistime + data_alignment_offset);
209 SIVAL(req->outbuf,smb_ntr_DataDisplacement, pd - pdata);
213 * Copy the param bytes into the packet.
216 if(params_sent_thistime) {
217 if (alignment_offset != 0) {
218 memset(smb_buf(req->outbuf), 0,
221 memcpy((smb_buf(req->outbuf)+alignment_offset), pp,
222 params_sent_thistime);
226 * Copy in the data bytes
229 if(data_sent_thistime) {
230 if (data_alignment_offset != 0) {
231 memset((smb_buf(req->outbuf)+alignment_offset+
232 params_sent_thistime), 0,
233 data_alignment_offset);
235 memcpy(smb_buf(req->outbuf)+alignment_offset
236 +params_sent_thistime+data_alignment_offset,
237 pd,data_sent_thistime);
240 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
241 params_sent_thistime, data_sent_thistime, useable_space));
242 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
243 params_to_send, data_to_send, paramsize, datasize));
245 if (NT_STATUS_V(nt_error)) {
246 error_packet_set((char *)req->outbuf,
251 /* Send the packet */
252 show_msg((char *)req->outbuf);
253 if (!srv_send_smb(sconn,
256 IS_CONN_ENCRYPTED(conn),
258 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
261 TALLOC_FREE(req->outbuf);
263 pp += params_sent_thistime;
264 pd += data_sent_thistime;
266 params_to_send -= params_sent_thistime;
267 data_to_send -= data_sent_thistime;
273 if(params_to_send < 0 || data_to_send < 0) {
274 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
275 params_to_send, data_to_send));
276 exit_server_cleanly("send_nt_replies: internal error");
281 /****************************************************************************
282 Reply to an NT create and X call on a pipe
283 ****************************************************************************/
285 static void nt_open_pipe(char *fname, connection_struct *conn,
286 struct smb_request *req, int *ppnum)
291 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
293 /* Strip \\ off the name. */
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)
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 definately
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 p = (char *)req->outbuf + smb_vwv2;
358 SIVAL(p,0,FILE_WAS_OPENED);
361 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
364 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
366 SSVAL(p,2, 0x5FF); /* ? */
369 if (flags & EXTENDED_RESPONSE_REQUIRED) {
371 SIVAL(p,0,FILE_GENERIC_ALL);
373 * For pipes W2K3 seems to return
375 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
377 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
380 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
385 struct case_semantics_state {
386 connection_struct *conn;
389 bool short_case_preserve;
392 /****************************************************************************
393 Restore case semantics.
394 ****************************************************************************/
396 static int restore_case_semantics(struct case_semantics_state *state)
398 state->conn->case_sensitive = state->case_sensitive;
399 state->conn->case_preserve = state->case_preserve;
400 state->conn->short_case_preserve = state->short_case_preserve;
404 /****************************************************************************
406 ****************************************************************************/
408 static struct case_semantics_state *set_posix_case_semantics(TALLOC_CTX *mem_ctx,
409 connection_struct *conn)
411 struct case_semantics_state *result;
413 if (!(result = talloc(mem_ctx, struct case_semantics_state))) {
418 result->case_sensitive = conn->case_sensitive;
419 result->case_preserve = conn->case_preserve;
420 result->short_case_preserve = conn->short_case_preserve;
423 conn->case_sensitive = True;
424 conn->case_preserve = True;
425 conn->short_case_preserve = True;
427 talloc_set_destructor(result, restore_case_semantics);
432 /****************************************************************************
433 Reply to an NT create and X call.
434 ****************************************************************************/
436 void reply_ntcreate_and_X(struct smb_request *req)
438 connection_struct *conn = req->conn;
439 struct smb_filename *smb_fname = NULL;
443 uint32 file_attributes;
445 uint32 create_disposition;
446 uint32 create_options;
448 uint64_t allocation_size;
449 /* Breakout the oplock request bits so we can set the
450 reply bits separately. */
452 SMB_OFF_T file_len = 0;
454 files_struct *fsp = NULL;
456 struct timespec create_timespec;
457 struct timespec c_timespec;
458 struct timespec a_timespec;
459 struct timespec m_timespec;
460 struct timespec write_time_ts;
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 = (uint64_t)IVAL(req->vwv+9, 1);
483 #ifdef LARGE_SMB_OFF_T
484 allocation_size |= (((uint64_t)IVAL(req->vwv+11, 1)) << 32);
487 srvstr_get_path_req(ctx, req, &fname, (const char *)req->buf,
488 STR_TERMINATE, &status);
490 if (!NT_STATUS_IS_OK(status)) {
491 reply_nterror(req, status);
495 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
496 "file_attributes = 0x%x, share_access = 0x%x, "
497 "create_disposition = 0x%x create_options = 0x%x "
498 "root_dir_fid = 0x%x, fname = %s\n",
500 (unsigned int)access_mask,
501 (unsigned int)file_attributes,
502 (unsigned int)share_access,
503 (unsigned int)create_disposition,
504 (unsigned int)create_options,
505 (unsigned int)root_dir_fid,
509 * we need to remove ignored bits when they come directly from the client
510 * because we reuse some of them for internal stuff
512 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
515 * If it's an IPC, use the pipe handler.
519 if (lp_nt_pipe_support()) {
520 do_ntcreate_pipe_open(conn, req);
523 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
527 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
528 if (oplock_request) {
529 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
533 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
534 case_state = set_posix_case_semantics(ctx, conn);
536 reply_nterror(req, NT_STATUS_NO_MEMORY);
541 status = filename_convert(ctx,
543 req->flags2 & FLAGS2_DFS_PATHNAMES,
549 TALLOC_FREE(case_state);
551 if (!NT_STATUS_IS_OK(status)) {
552 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
554 NT_STATUS_PATH_NOT_COVERED,
558 reply_nterror(req, status);
563 * Bug #6898 - clients using Windows opens should
564 * never be able to set this attribute into the
567 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
569 status = SMB_VFS_CREATE_FILE(
572 root_dir_fid, /* root_dir_fid */
573 smb_fname, /* fname */
574 access_mask, /* access_mask */
575 share_access, /* share_access */
576 create_disposition, /* create_disposition*/
577 create_options, /* create_options */
578 file_attributes, /* file_attributes */
579 oplock_request, /* oplock_request */
580 allocation_size, /* allocation_size */
581 0, /* private_flags */
587 if (!NT_STATUS_IS_OK(status)) {
588 if (open_was_deferred(req->mid)) {
589 /* We have re-scheduled this call, no error. */
592 reply_openerror(req, status);
596 /* Ensure we're pointing at the correct stat struct. */
597 TALLOC_FREE(smb_fname);
598 smb_fname = fsp->fsp_name;
601 * If the caller set the extended oplock request bit
602 * and we granted one (by whatever means) - set the
603 * correct bit for extended oplock reply.
606 if (oplock_request &&
607 (lp_fake_oplocks(SNUM(conn))
608 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
611 * Exclusive oplock granted
614 if (flags & REQUEST_BATCH_OPLOCK) {
615 oplock_granted = BATCH_OPLOCK_RETURN;
617 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
619 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
620 oplock_granted = LEVEL_II_OPLOCK_RETURN;
622 oplock_granted = NO_OPLOCK_RETURN;
625 file_len = smb_fname->st.st_ex_size;
627 if (flags & EXTENDED_RESPONSE_REQUIRED) {
628 /* This is very strange. We
629 * return 50 words, but only set
630 * the wcnt to 42 ? It's definately
631 * what happens on the wire....
633 reply_outbuf(req, 50, 0);
634 SCVAL(req->outbuf,smb_wct,42);
636 reply_outbuf(req, 34, 0);
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;
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 = SMB_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 ||
722 can_write_to_file(conn, smb_fname)) {
723 perms = FILE_GENERIC_ALL;
725 perms = FILE_GENERIC_READ|FILE_EXECUTE;
730 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n",
731 fsp->fnum, 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;
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 Internal fn to set security descriptors.
837 ****************************************************************************/
839 NTSTATUS set_sd(files_struct *fsp, uint8_t *data, uint32_t sd_len,
840 uint32_t security_info_sent)
842 struct security_descriptor *psd = NULL;
846 return NT_STATUS_INVALID_PARAMETER;
849 if (!CAN_WRITE(fsp->conn)) {
850 return NT_STATUS_ACCESS_DENIED;
853 if (!lp_nt_acl_support(SNUM(fsp->conn))) {
857 status = unmarshall_sec_desc(talloc_tos(), data, sd_len, &psd);
859 if (!NT_STATUS_IS_OK(status)) {
863 if (psd->owner_sid == NULL) {
864 security_info_sent &= ~SECINFO_OWNER;
866 if (psd->group_sid == NULL) {
867 security_info_sent &= ~SECINFO_GROUP;
870 /* Ensure we have at least one thing set. */
871 if ((security_info_sent & (SECINFO_OWNER|SECINFO_GROUP|SECINFO_DACL|SECINFO_SACL)) == 0) {
872 return NT_STATUS_INVALID_PARAMETER;
875 /* Ensure we have the rights to do this. */
876 if (security_info_sent & SECINFO_OWNER) {
877 if (!(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
878 return NT_STATUS_ACCESS_DENIED;
882 if (security_info_sent & SECINFO_GROUP) {
883 if (!(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
884 return NT_STATUS_ACCESS_DENIED;
888 if (security_info_sent & SECINFO_DACL) {
889 if (!(fsp->access_mask & SEC_STD_WRITE_DAC)) {
890 return NT_STATUS_ACCESS_DENIED;
892 /* Convert all the generic bits. */
894 security_acl_map_generic(psd->dacl, &file_generic_mapping);
898 if (security_info_sent & SECINFO_SACL) {
899 if (!(fsp->access_mask & SEC_FLAG_SYSTEM_SECURITY)) {
900 return NT_STATUS_ACCESS_DENIED;
902 /* Convert all the generic bits. */
904 security_acl_map_generic(psd->sacl, &file_generic_mapping);
908 if (DEBUGLEVEL >= 10) {
909 DEBUG(10,("set_sd for file %s\n", fsp_str_dbg(fsp)));
910 NDR_PRINT_DEBUG(security_descriptor, psd);
913 status = SMB_VFS_FSET_NT_ACL(fsp, security_info_sent, psd);
920 /****************************************************************************
921 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
922 ****************************************************************************/
924 struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
926 struct ea_list *ea_list_head = NULL;
933 while (offset + 4 <= data_size) {
934 size_t next_offset = IVAL(pdata,offset);
935 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
941 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
942 if (next_offset == 0) {
945 offset += next_offset;
951 /****************************************************************************
952 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
953 ****************************************************************************/
955 static void call_nt_transact_create(connection_struct *conn,
956 struct smb_request *req,
957 uint16 **ppsetup, uint32 setup_count,
958 char **ppparams, uint32 parameter_count,
959 char **ppdata, uint32 data_count,
960 uint32 max_data_count)
962 struct smb_filename *smb_fname = NULL;
964 char *params = *ppparams;
965 char *data = *ppdata;
966 /* Breakout the oplock request bits so we can set the reply bits separately. */
968 SMB_OFF_T file_len = 0;
970 files_struct *fsp = NULL;
974 uint32 file_attributes;
976 uint32 create_disposition;
977 uint32 create_options;
979 struct security_descriptor *sd = NULL;
982 struct timespec create_timespec;
983 struct timespec c_timespec;
984 struct timespec a_timespec;
985 struct timespec m_timespec;
986 struct timespec write_time_ts;
987 struct ea_list *ea_list = NULL;
990 uint64_t allocation_size;
992 uint8_t oplock_granted;
993 struct case_semantics_state *case_state = NULL;
994 TALLOC_CTX *ctx = talloc_tos();
996 DEBUG(5,("call_nt_transact_create\n"));
999 * If it's an IPC, use the pipe handler.
1003 if (lp_nt_pipe_support()) {
1004 do_nt_transact_create_pipe(
1006 ppsetup, setup_count,
1007 ppparams, parameter_count,
1008 ppdata, data_count);
1011 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1016 * Ensure minimum number of parameters sent.
1019 if(parameter_count < 54) {
1020 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1021 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1025 flags = IVAL(params,0);
1026 access_mask = IVAL(params,8);
1027 file_attributes = IVAL(params,20);
1028 share_access = IVAL(params,24);
1029 create_disposition = IVAL(params,28);
1030 create_options = IVAL(params,32);
1031 sd_len = IVAL(params,36);
1032 ea_len = IVAL(params,40);
1033 root_dir_fid = (uint16)IVAL(params,4);
1034 allocation_size = (uint64_t)IVAL(params,12);
1035 #ifdef LARGE_SMB_OFF_T
1036 allocation_size |= (((uint64_t)IVAL(params,16)) << 32);
1040 * we need to remove ignored bits when they come directly from the client
1041 * because we reuse some of them for internal stuff
1043 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
1045 /* Ensure the data_len is correct for the sd and ea values given. */
1046 if ((ea_len + sd_len > data_count)
1047 || (ea_len > data_count) || (sd_len > data_count)
1048 || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
1049 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
1050 "%u, data_count = %u\n", (unsigned int)ea_len,
1051 (unsigned int)sd_len, (unsigned int)data_count));
1052 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1057 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
1060 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
1062 if (!NT_STATUS_IS_OK(status)) {
1063 DEBUG(10, ("call_nt_transact_create: "
1064 "unmarshall_sec_desc failed: %s\n",
1065 nt_errstr(status)));
1066 reply_nterror(req, status);
1072 if (!lp_ea_support(SNUM(conn))) {
1073 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
1074 "EA's not supported.\n",
1075 (unsigned int)ea_len));
1076 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
1081 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
1082 "too small (should be more than 10)\n",
1083 (unsigned int)ea_len ));
1084 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1088 /* We have already checked that ea_len <= data_count here. */
1089 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
1091 if (ea_list == NULL) {
1092 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1097 srvstr_get_path(ctx, params, req->flags2, &fname,
1098 params+53, parameter_count-53,
1099 STR_TERMINATE, &status);
1100 if (!NT_STATUS_IS_OK(status)) {
1101 reply_nterror(req, status);
1105 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1106 case_state = set_posix_case_semantics(ctx, conn);
1108 reply_nterror(req, NT_STATUS_NO_MEMORY);
1113 status = filename_convert(ctx,
1115 req->flags2 & FLAGS2_DFS_PATHNAMES,
1121 TALLOC_FREE(case_state);
1123 if (!NT_STATUS_IS_OK(status)) {
1124 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1125 reply_botherror(req,
1126 NT_STATUS_PATH_NOT_COVERED,
1127 ERRSRV, ERRbadpath);
1130 reply_nterror(req, status);
1134 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1135 if (oplock_request) {
1136 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
1141 * Bug #6898 - clients using Windows opens should
1142 * never be able to set this attribute into the
1145 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
1147 status = SMB_VFS_CREATE_FILE(
1150 root_dir_fid, /* root_dir_fid */
1151 smb_fname, /* fname */
1152 access_mask, /* access_mask */
1153 share_access, /* share_access */
1154 create_disposition, /* create_disposition*/
1155 create_options, /* create_options */
1156 file_attributes, /* file_attributes */
1157 oplock_request, /* oplock_request */
1158 allocation_size, /* allocation_size */
1159 0, /* private_flags */
1161 ea_list, /* ea_list */
1165 if(!NT_STATUS_IS_OK(status)) {
1166 if (open_was_deferred(req->mid)) {
1167 /* We have re-scheduled this call, no error. */
1170 reply_openerror(req, status);
1174 /* Ensure we're pointing at the correct stat struct. */
1175 TALLOC_FREE(smb_fname);
1176 smb_fname = fsp->fsp_name;
1179 * If the caller set the extended oplock request bit
1180 * and we granted one (by whatever means) - set the
1181 * correct bit for extended oplock reply.
1184 if (oplock_request &&
1185 (lp_fake_oplocks(SNUM(conn))
1186 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
1189 * Exclusive oplock granted
1192 if (flags & REQUEST_BATCH_OPLOCK) {
1193 oplock_granted = BATCH_OPLOCK_RETURN;
1195 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
1197 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1198 oplock_granted = LEVEL_II_OPLOCK_RETURN;
1200 oplock_granted = NO_OPLOCK_RETURN;
1203 file_len = smb_fname->st.st_ex_size;
1205 /* Realloc the size of parameters and data we will return */
1206 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1207 /* Extended response is 32 more byyes. */
1212 params = nttrans_realloc(ppparams, param_len);
1213 if(params == NULL) {
1214 reply_nterror(req, NT_STATUS_NO_MEMORY);
1219 SCVAL(p, 0, oplock_granted);
1222 SSVAL(p,0,fsp->fnum);
1224 if ((create_disposition == FILE_SUPERSEDE)
1225 && (info == FILE_WAS_OVERWRITTEN)) {
1226 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1232 fattr = dos_mode(conn, smb_fname);
1234 fattr = FILE_ATTRIBUTE_NORMAL;
1237 /* Deal with other possible opens having a modified
1239 ZERO_STRUCT(write_time_ts);
1240 get_file_infos(fsp->file_id, 0, NULL, &write_time_ts);
1241 if (!null_timespec(write_time_ts)) {
1242 update_stat_ex_mtime(&smb_fname->st, write_time_ts);
1246 create_timespec = get_create_timespec(conn, fsp, smb_fname);
1247 a_timespec = smb_fname->st.st_ex_atime;
1248 m_timespec = smb_fname->st.st_ex_mtime;
1249 c_timespec = get_change_timespec(conn, fsp, smb_fname);
1251 if (lp_dos_filetime_resolution(SNUM(conn))) {
1252 dos_filetime_timespec(&create_timespec);
1253 dos_filetime_timespec(&a_timespec);
1254 dos_filetime_timespec(&m_timespec);
1255 dos_filetime_timespec(&c_timespec);
1258 put_long_date_timespec(conn->ts_res, p, create_timespec); /* create time. */
1260 put_long_date_timespec(conn->ts_res, p, a_timespec); /* access time */
1262 put_long_date_timespec(conn->ts_res, p, m_timespec); /* write time */
1264 put_long_date_timespec(conn->ts_res, p, c_timespec); /* change time */
1266 SIVAL(p,0,fattr); /* File Attributes. */
1268 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn, fsp, &smb_fname->st));
1270 SOFF_T(p,0,file_len);
1272 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1273 uint16_t file_status = (NO_EAS|NO_SUBSTREAMS|NO_REPARSETAG);
1274 size_t num_names = 0;
1275 unsigned int num_streams;
1276 struct stream_struct *streams = NULL;
1278 /* Do we have any EA's ? */
1279 status = get_ea_names_from_file(ctx, conn, fsp,
1280 smb_fname->base_name, NULL, &num_names);
1281 if (NT_STATUS_IS_OK(status) && num_names) {
1282 file_status &= ~NO_EAS;
1284 status = SMB_VFS_STREAMINFO(conn, NULL, smb_fname->base_name, ctx,
1285 &num_streams, &streams);
1286 /* There is always one stream, ::$DATA. */
1287 if (NT_STATUS_IS_OK(status) && num_streams > 1) {
1288 file_status &= ~NO_SUBSTREAMS;
1290 TALLOC_FREE(streams);
1291 SSVAL(p,2,file_status);
1294 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1296 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1299 if (fsp->is_directory ||
1300 can_write_to_file(conn, smb_fname)) {
1301 perms = FILE_GENERIC_ALL;
1303 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1308 DEBUG(5,("call_nt_transact_create: open name = %s\n",
1309 smb_fname_str_dbg(smb_fname)));
1311 /* Send the required number of replies */
1312 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1317 /****************************************************************************
1318 Reply to a NT CANCEL request.
1319 conn POINTER CAN BE NULL HERE !
1320 ****************************************************************************/
1322 void reply_ntcancel(struct smb_request *req)
1325 * Go through and cancel any pending change notifies.
1328 START_PROFILE(SMBntcancel);
1329 srv_cancel_sign_response(req->sconn);
1330 remove_pending_change_notify_requests_by_mid(req->sconn, req->mid);
1331 remove_pending_lock_requests_by_mid_smb1(req->sconn, req->mid);
1333 DEBUG(3,("reply_ntcancel: cancel called on mid = %llu.\n",
1334 (unsigned long long)req->mid));
1336 END_PROFILE(SMBntcancel);
1340 /****************************************************************************
1342 ****************************************************************************/
1344 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
1345 connection_struct *conn,
1346 struct smb_request *req,
1347 struct smb_filename *smb_fname_src,
1348 struct smb_filename *smb_fname_dst,
1351 files_struct *fsp1,*fsp2;
1355 NTSTATUS status = NT_STATUS_OK;
1358 if (!CAN_WRITE(conn)) {
1359 status = NT_STATUS_MEDIA_WRITE_PROTECTED;
1363 /* Source must already exist. */
1364 if (!VALID_STAT(smb_fname_src->st)) {
1365 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
1369 /* Ensure attributes match. */
1370 fattr = dos_mode(conn, smb_fname_src);
1371 if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
1372 status = NT_STATUS_NO_SUCH_FILE;
1376 /* Disallow if dst file already exists. */
1377 if (VALID_STAT(smb_fname_dst->st)) {
1378 status = NT_STATUS_OBJECT_NAME_COLLISION;
1382 /* No links from a directory. */
1383 if (S_ISDIR(smb_fname_src->st.st_ex_mode)) {
1384 status = NT_STATUS_FILE_IS_A_DIRECTORY;
1388 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1389 smb_fname_str_dbg(smb_fname_src),
1390 smb_fname_str_dbg(smb_fname_dst)));
1392 status = SMB_VFS_CREATE_FILE(
1395 0, /* root_dir_fid */
1396 smb_fname_src, /* fname */
1397 FILE_READ_DATA|FILE_READ_ATTRIBUTES|
1398 FILE_READ_EA, /* access_mask */
1399 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1401 FILE_OPEN, /* create_disposition*/
1402 0, /* create_options */
1403 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
1404 NO_OPLOCK, /* oplock_request */
1405 0, /* allocation_size */
1406 0, /* private_flags */
1412 if (!NT_STATUS_IS_OK(status)) {
1416 status = SMB_VFS_CREATE_FILE(
1419 0, /* root_dir_fid */
1420 smb_fname_dst, /* fname */
1421 FILE_WRITE_DATA|FILE_WRITE_ATTRIBUTES|
1422 FILE_WRITE_EA, /* access_mask */
1423 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1425 FILE_CREATE, /* create_disposition*/
1426 0, /* create_options */
1427 fattr, /* file_attributes */
1428 NO_OPLOCK, /* oplock_request */
1429 0, /* allocation_size */
1430 0, /* private_flags */
1436 if (!NT_STATUS_IS_OK(status)) {
1437 close_file(NULL, fsp1, ERROR_CLOSE);
1441 if (smb_fname_src->st.st_ex_size) {
1442 ret = vfs_transfer_file(fsp1, fsp2, smb_fname_src->st.st_ex_size);
1446 * As we are opening fsp1 read-only we only expect
1447 * an error on close on fsp2 if we are out of space.
1448 * Thus we don't look at the error return from the
1451 close_file(NULL, fsp1, NORMAL_CLOSE);
1453 /* Ensure the modtime is set correctly on the destination file. */
1454 set_close_write_time(fsp2, smb_fname_src->st.st_ex_mtime);
1456 status = close_file(NULL, fsp2, NORMAL_CLOSE);
1458 /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
1459 creates the file. This isn't the correct thing to do in the copy
1461 if (!parent_dirname(talloc_tos(), smb_fname_dst->base_name, &parent,
1463 status = NT_STATUS_NO_MEMORY;
1466 file_set_dosmode(conn, smb_fname_dst, fattr, parent, false);
1467 TALLOC_FREE(parent);
1469 if (ret < (SMB_OFF_T)smb_fname_src->st.st_ex_size) {
1470 status = NT_STATUS_DISK_FULL;
1474 if (!NT_STATUS_IS_OK(status)) {
1475 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1476 nt_errstr(status), smb_fname_str_dbg(smb_fname_src),
1477 smb_fname_str_dbg(smb_fname_dst)));
1483 /****************************************************************************
1484 Reply to a NT rename request.
1485 ****************************************************************************/
1487 void reply_ntrename(struct smb_request *req)
1489 connection_struct *conn = req->conn;
1490 struct smb_filename *smb_fname_old = NULL;
1491 struct smb_filename *smb_fname_new = NULL;
1492 char *oldname = NULL;
1493 char *newname = NULL;
1496 bool src_has_wcard = False;
1497 bool dest_has_wcard = False;
1499 uint32_t ucf_flags_src = 0;
1500 uint32_t ucf_flags_dst = 0;
1502 TALLOC_CTX *ctx = talloc_tos();
1503 bool stream_rename = false;
1505 START_PROFILE(SMBntrename);
1508 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1512 attrs = SVAL(req->vwv+0, 0);
1513 rename_type = SVAL(req->vwv+1, 0);
1515 p = (const char *)req->buf + 1;
1516 p += srvstr_get_path_req_wcard(ctx, req, &oldname, p, STR_TERMINATE,
1517 &status, &src_has_wcard);
1518 if (!NT_STATUS_IS_OK(status)) {
1519 reply_nterror(req, status);
1523 if (ms_has_wild(oldname)) {
1524 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1529 p += srvstr_get_path_req_wcard(ctx, req, &newname, p, STR_TERMINATE,
1530 &status, &dest_has_wcard);
1531 if (!NT_STATUS_IS_OK(status)) {
1532 reply_nterror(req, status);
1536 if (!lp_posix_pathnames()) {
1537 /* The newname must begin with a ':' if the
1538 oldname contains a ':'. */
1539 if (strchr_m(oldname, ':')) {
1540 if (newname[0] != ':') {
1541 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1544 stream_rename = true;
1549 * If this is a rename operation, allow wildcards and save the
1550 * destination's last component.
1552 if (rename_type == RENAME_FLAG_RENAME) {
1553 ucf_flags_src = UCF_COND_ALLOW_WCARD_LCOMP;
1554 ucf_flags_dst = UCF_COND_ALLOW_WCARD_LCOMP | UCF_SAVE_LCOMP;
1557 /* rename_internals() calls unix_convert(), so don't call it here. */
1558 status = filename_convert(ctx, conn,
1559 req->flags2 & FLAGS2_DFS_PATHNAMES,
1564 if (!NT_STATUS_IS_OK(status)) {
1565 if (NT_STATUS_EQUAL(status,
1566 NT_STATUS_PATH_NOT_COVERED)) {
1567 reply_botherror(req,
1568 NT_STATUS_PATH_NOT_COVERED,
1569 ERRSRV, ERRbadpath);
1572 reply_nterror(req, status);
1576 status = filename_convert(ctx, conn,
1577 req->flags2 & FLAGS2_DFS_PATHNAMES,
1582 if (!NT_STATUS_IS_OK(status)) {
1583 if (NT_STATUS_EQUAL(status,
1584 NT_STATUS_PATH_NOT_COVERED)) {
1585 reply_botherror(req,
1586 NT_STATUS_PATH_NOT_COVERED,
1587 ERRSRV, ERRbadpath);
1590 reply_nterror(req, status);
1594 if (stream_rename) {
1595 /* smb_fname_new must be the same as smb_fname_old. */
1596 TALLOC_FREE(smb_fname_new->base_name);
1597 smb_fname_new->base_name = talloc_strdup(smb_fname_new,
1598 smb_fname_old->base_name);
1599 if (!smb_fname_new->base_name) {
1600 reply_nterror(req, NT_STATUS_NO_MEMORY);
1605 DEBUG(3,("reply_ntrename: %s -> %s\n",
1606 smb_fname_str_dbg(smb_fname_old),
1607 smb_fname_str_dbg(smb_fname_new)));
1609 switch(rename_type) {
1610 case RENAME_FLAG_RENAME:
1611 status = rename_internals(ctx, conn, req,
1612 smb_fname_old, smb_fname_new,
1613 attrs, False, src_has_wcard,
1617 case RENAME_FLAG_HARD_LINK:
1618 if (src_has_wcard || dest_has_wcard) {
1620 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1622 status = hardlink_internals(ctx, conn,
1629 case RENAME_FLAG_COPY:
1630 if (src_has_wcard || dest_has_wcard) {
1632 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1634 status = copy_internals(ctx, conn, req,
1640 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1641 status = NT_STATUS_INVALID_PARAMETER;
1644 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1648 if (!NT_STATUS_IS_OK(status)) {
1649 if (open_was_deferred(req->mid)) {
1650 /* We have re-scheduled this call. */
1654 reply_nterror(req, status);
1658 reply_outbuf(req, 0, 0);
1660 END_PROFILE(SMBntrename);
1664 /****************************************************************************
1665 Reply to a notify change - queue the request and
1666 don't allow a directory to be opened.
1667 ****************************************************************************/
1669 static void smbd_smb1_notify_reply(struct smb_request *req,
1670 NTSTATUS error_code,
1671 uint8_t *buf, size_t len)
1673 send_nt_replies(req->conn, req, error_code, (char *)buf, len, NULL, 0);
1676 static void call_nt_transact_notify_change(connection_struct *conn,
1677 struct smb_request *req,
1681 uint32 parameter_count,
1682 char **ppdata, uint32 data_count,
1683 uint32 max_data_count,
1684 uint32 max_param_count)
1686 uint16 *setup = *ppsetup;
1692 if(setup_count < 6) {
1693 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1697 fsp = file_fsp(req, SVAL(setup,4));
1698 filter = IVAL(setup, 0);
1699 recursive = (SVAL(setup, 6) != 0) ? True : False;
1701 DEBUG(3,("call_nt_transact_notify_change\n"));
1704 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1709 char *filter_string;
1711 if (!(filter_string = notify_filter_string(NULL, filter))) {
1712 reply_nterror(req,NT_STATUS_NO_MEMORY);
1716 DEBUG(3,("call_nt_transact_notify_change: notify change "
1717 "called on %s, filter = %s, recursive = %d\n",
1718 fsp_str_dbg(fsp), filter_string, recursive));
1720 TALLOC_FREE(filter_string);
1723 if((!fsp->is_directory) || (conn != fsp->conn)) {
1724 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1728 if (fsp->notify == NULL) {
1730 status = change_notify_create(fsp, filter, recursive);
1732 if (!NT_STATUS_IS_OK(status)) {
1733 DEBUG(10, ("change_notify_create returned %s\n",
1734 nt_errstr(status)));
1735 reply_nterror(req, status);
1740 if (fsp->notify->num_changes != 0) {
1743 * We've got changes pending, respond immediately
1747 * TODO: write a torture test to check the filtering behaviour
1751 change_notify_reply(req,
1755 smbd_smb1_notify_reply);
1758 * change_notify_reply() above has independently sent its
1765 * No changes pending, queue the request
1768 status = change_notify_add_request(req,
1772 smbd_smb1_notify_reply);
1773 if (!NT_STATUS_IS_OK(status)) {
1774 reply_nterror(req, status);
1779 /****************************************************************************
1780 Reply to an NT transact rename command.
1781 ****************************************************************************/
1783 static void call_nt_transact_rename(connection_struct *conn,
1784 struct smb_request *req,
1785 uint16 **ppsetup, uint32 setup_count,
1786 char **ppparams, uint32 parameter_count,
1787 char **ppdata, uint32 data_count,
1788 uint32 max_data_count)
1790 char *params = *ppparams;
1791 char *new_name = NULL;
1792 files_struct *fsp = NULL;
1793 bool dest_has_wcard = False;
1795 TALLOC_CTX *ctx = talloc_tos();
1797 if(parameter_count < 5) {
1798 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1802 fsp = file_fsp(req, SVAL(params, 0));
1803 if (!check_fsp(conn, req, fsp)) {
1806 srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
1807 parameter_count - 4,
1808 STR_TERMINATE, &status, &dest_has_wcard);
1809 if (!NT_STATUS_IS_OK(status)) {
1810 reply_nterror(req, status);
1815 * W2K3 ignores this request as the RAW-RENAME test
1816 * demonstrates, so we do.
1818 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1820 DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1821 fsp_str_dbg(fsp), new_name));
1826 /******************************************************************************
1827 Fake up a completely empty SD.
1828 *******************************************************************************/
1830 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, struct security_descriptor **ppsd)
1834 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1836 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1837 return NT_STATUS_NO_MEMORY;
1840 return NT_STATUS_OK;
1843 /****************************************************************************
1844 Reply to query a security descriptor.
1845 Callable from SMB2 and SMB2.
1846 If it returns NT_STATUS_BUFFER_TOO_SMALL, pdata_size is initialized with
1848 ****************************************************************************/
1850 NTSTATUS smbd_do_query_security_desc(connection_struct *conn,
1851 TALLOC_CTX *mem_ctx,
1853 uint32_t security_info_wanted,
1854 uint32_t max_data_count,
1855 uint8_t **ppmarshalled_sd,
1859 struct security_descriptor *psd = NULL;
1862 * Get the permissions to return.
1865 if ((security_info_wanted & SECINFO_SACL) &&
1866 !(fsp->access_mask & SEC_FLAG_SYSTEM_SECURITY)) {
1867 return NT_STATUS_ACCESS_DENIED;
1870 if ((security_info_wanted & (SECINFO_DACL|SECINFO_OWNER|SECINFO_GROUP)) &&
1871 !(fsp->access_mask & SEC_STD_READ_CONTROL)) {
1872 return NT_STATUS_ACCESS_DENIED;
1875 if (!lp_nt_acl_support(SNUM(conn))) {
1876 status = get_null_nt_acl(mem_ctx, &psd);
1878 status = SMB_VFS_FGET_NT_ACL(
1879 fsp, security_info_wanted, &psd);
1881 if (!NT_STATUS_IS_OK(status)) {
1885 if (!(security_info_wanted & SECINFO_OWNER)) {
1886 psd->owner_sid = NULL;
1888 if (!(security_info_wanted & SECINFO_GROUP)) {
1889 psd->group_sid = NULL;
1891 if (!(security_info_wanted & SECINFO_DACL)) {
1894 if (!(security_info_wanted & SECINFO_SACL)) {
1898 /* If the SACL/DACL is NULL, but was requested, we mark that it is
1899 * present in the reply to match Windows behavior */
1900 if (psd->sacl == NULL &&
1901 security_info_wanted & SECINFO_SACL)
1902 psd->type |= SEC_DESC_SACL_PRESENT;
1903 if (psd->dacl == NULL &&
1904 security_info_wanted & SECINFO_DACL)
1905 psd->type |= SEC_DESC_DACL_PRESENT;
1907 *psd_size = ndr_size_security_descriptor(psd, 0);
1909 DEBUG(3,("smbd_do_query_security_desc: sd_size = %lu.\n",
1910 (unsigned long)*psd_size));
1912 if (DEBUGLEVEL >= 10) {
1913 DEBUG(10,("smbd_do_query_security_desc for file %s\n",
1915 NDR_PRINT_DEBUG(security_descriptor, psd);
1918 if (max_data_count < *psd_size) {
1920 return NT_STATUS_BUFFER_TOO_SMALL;
1923 status = marshall_sec_desc(mem_ctx, psd,
1924 ppmarshalled_sd, psd_size);
1926 if (!NT_STATUS_IS_OK(status)) {
1932 return NT_STATUS_OK;
1935 /****************************************************************************
1936 SMB1 reply to query a security descriptor.
1937 ****************************************************************************/
1939 static void call_nt_transact_query_security_desc(connection_struct *conn,
1940 struct smb_request *req,
1944 uint32 parameter_count,
1947 uint32 max_data_count)
1949 char *params = *ppparams;
1950 char *data = *ppdata;
1952 uint32 security_info_wanted;
1953 files_struct *fsp = NULL;
1955 uint8_t *marshalled_sd = NULL;
1957 if(parameter_count < 8) {
1958 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1962 fsp = file_fsp(req, SVAL(params,0));
1964 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1968 security_info_wanted = IVAL(params,4);
1970 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, "
1971 "info_wanted = 0x%x\n", fsp_str_dbg(fsp),
1972 (unsigned int)security_info_wanted));
1974 params = nttrans_realloc(ppparams, 4);
1975 if(params == NULL) {
1976 reply_nterror(req, NT_STATUS_NO_MEMORY);
1981 * Get the permissions to return.
1984 status = smbd_do_query_security_desc(conn,
1987 security_info_wanted,
1992 if (NT_STATUS_EQUAL(status, NT_STATUS_BUFFER_TOO_SMALL)) {
1993 SIVAL(params,0,(uint32_t)sd_size);
1994 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
1995 params, 4, NULL, 0);
1999 if (!NT_STATUS_IS_OK(status)) {
2000 reply_nterror(req, status);
2004 SMB_ASSERT(sd_size > 0);
2006 SIVAL(params,0,(uint32_t)sd_size);
2008 if (max_data_count < sd_size) {
2009 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
2010 params, 4, NULL, 0);
2015 * Allocate the data we will return.
2018 data = nttrans_realloc(ppdata, sd_size);
2020 reply_nterror(req, NT_STATUS_NO_MEMORY);
2024 memcpy(data, marshalled_sd, sd_size);
2026 send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
2031 /****************************************************************************
2032 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
2033 ****************************************************************************/
2035 static void call_nt_transact_set_security_desc(connection_struct *conn,
2036 struct smb_request *req,
2040 uint32 parameter_count,
2043 uint32 max_data_count)
2045 char *params= *ppparams;
2046 char *data = *ppdata;
2047 files_struct *fsp = NULL;
2048 uint32 security_info_sent = 0;
2051 if(parameter_count < 8) {
2052 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2056 if((fsp = file_fsp(req, SVAL(params,0))) == NULL) {
2057 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2061 if (!CAN_WRITE(fsp->conn)) {
2062 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2066 if(!lp_nt_acl_support(SNUM(conn))) {
2070 security_info_sent = IVAL(params,4);
2072 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n",
2073 fsp_str_dbg(fsp), (unsigned int)security_info_sent));
2075 if (data_count == 0) {
2076 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2080 status = set_sd(fsp, (uint8 *)data, data_count, security_info_sent);
2082 if (!NT_STATUS_IS_OK(status)) {
2083 reply_nterror(req, status);
2088 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2092 /****************************************************************************
2094 ****************************************************************************/
2096 static void call_nt_transact_ioctl(connection_struct *conn,
2097 struct smb_request *req,
2098 uint16 **ppsetup, uint32 setup_count,
2099 char **ppparams, uint32 parameter_count,
2100 char **ppdata, uint32 data_count,
2101 uint32 max_data_count)
2108 char *pdata = *ppdata;
2110 if (setup_count != 8) {
2111 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
2112 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2116 function = IVAL(*ppsetup, 0);
2117 fidnum = SVAL(*ppsetup, 4);
2118 isFSctl = CVAL(*ppsetup, 6);
2119 compfilter = CVAL(*ppsetup, 7);
2121 DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
2122 function, fidnum, isFSctl, compfilter));
2124 fsp=file_fsp(req, fidnum);
2125 /* this check is done in each implemented function case for now
2126 because I don't want to break anything... --metze
2127 FSP_BELONGS_CONN(fsp,conn);*/
2129 SMB_PERFCOUNT_SET_IOCTL(&req->pcd, function);
2132 case FSCTL_SET_SPARSE:
2134 bool set_sparse = true;
2137 if (data_count >= 1 && pdata[0] == 0) {
2141 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X]set[%u]\n",
2142 fidnum, set_sparse));
2144 if (!check_fsp_open(conn, req, fsp)) {
2148 status = file_set_sparse(conn, fsp, set_sparse);
2149 if (!NT_STATUS_IS_OK(status)) {
2150 DEBUG(9,("FSCTL_SET_SPARSE: fname[%s] set[%u] - %s\n",
2151 smb_fname_str_dbg(fsp->fsp_name), set_sparse, nt_errstr(status)));
2152 reply_nterror(req, status);
2156 DEBUG(10,("FSCTL_SET_SPARSE: fname[%s] set[%u] - %s\n",
2157 smb_fname_str_dbg(fsp->fsp_name), set_sparse, nt_errstr(status)));
2158 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2161 case FSCTL_CREATE_OR_GET_OBJECT_ID:
2163 unsigned char objid[16];
2165 /* This should return the object-id on this file.
2166 * I think I'll make this be the inode+dev. JRA.
2169 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
2171 if (!check_fsp_open(conn, req, fsp)) {
2176 pdata = nttrans_realloc(ppdata, data_count);
2177 if (pdata == NULL) {
2178 reply_nterror(req, NT_STATUS_NO_MEMORY);
2182 /* For backwards compatibility only store the dev/inode. */
2183 push_file_id_16(pdata, &fsp->file_id);
2184 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
2185 push_file_id_16(pdata+32, &fsp->file_id);
2186 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
2191 case FSCTL_GET_REPARSE_POINT:
2192 /* pretend this fail - my winXP does it like this
2196 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2197 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
2200 case FSCTL_SET_REPARSE_POINT:
2201 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
2205 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2206 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
2209 case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
2212 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
2213 * and return their volume names. If max_data_count is 16, then it is just
2214 * asking for the number of volumes and length of the combined names.
2216 * pdata is the data allocated by our caller, but that uses
2217 * total_data_count (which is 0 in our case) rather than max_data_count.
2218 * Allocate the correct amount and return the pointer to let
2219 * it be deallocated when we return.
2221 SHADOW_COPY_DATA *shadow_data = NULL;
2222 TALLOC_CTX *shadow_mem_ctx = NULL;
2223 bool labels = False;
2224 uint32 labels_data_count = 0;
2228 if (!check_fsp_open(conn, req, fsp)) {
2232 if (max_data_count < 16) {
2233 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
2235 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2239 if (max_data_count > 16) {
2243 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
2244 if (shadow_mem_ctx == NULL) {
2245 DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
2246 reply_nterror(req, NT_STATUS_NO_MEMORY);
2250 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
2251 if (shadow_data == NULL) {
2252 DEBUG(0,("TALLOC_ZERO() failed!\n"));
2253 talloc_destroy(shadow_mem_ctx);
2254 reply_nterror(req, NT_STATUS_NO_MEMORY);
2258 shadow_data->mem_ctx = shadow_mem_ctx;
2261 * Call the VFS routine to actually do the work.
2263 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
2264 talloc_destroy(shadow_data->mem_ctx);
2265 if (errno == ENOSYS) {
2266 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
2267 conn->connectpath));
2268 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2271 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
2272 conn->connectpath));
2273 reply_nterror(req, NT_STATUS_UNSUCCESSFUL);
2278 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
2283 data_count = 12+labels_data_count+4;
2286 if (max_data_count<data_count) {
2287 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
2288 max_data_count,data_count));
2289 talloc_destroy(shadow_data->mem_ctx);
2290 reply_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
2294 pdata = nttrans_realloc(ppdata, data_count);
2295 if (pdata == NULL) {
2296 talloc_destroy(shadow_data->mem_ctx);
2297 reply_nterror(req, NT_STATUS_NO_MEMORY);
2303 /* num_volumes 4 bytes */
2304 SIVAL(pdata,0,shadow_data->num_volumes);
2307 /* num_labels 4 bytes */
2308 SIVAL(pdata,4,shadow_data->num_volumes);
2311 /* needed_data_count 4 bytes */
2312 SIVAL(pdata, 8, labels_data_count+4);
2316 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
2317 shadow_data->num_volumes, fsp_str_dbg(fsp)));
2318 if (labels && shadow_data->labels) {
2319 for (i=0;i<shadow_data->num_volumes;i++) {
2320 srvstr_push(pdata, req->flags2,
2321 cur_pdata, shadow_data->labels[i],
2322 2*sizeof(SHADOW_COPY_LABEL),
2323 STR_UNICODE|STR_TERMINATE);
2324 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
2325 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
2329 talloc_destroy(shadow_data->mem_ctx);
2331 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
2337 case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
2339 /* pretend this succeeded -
2341 * we have to send back a list with all files owned by this SID
2343 * but I have to check that --metze
2349 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
2351 if (!check_fsp_open(conn, req, fsp)) {
2355 if (data_count < 8) {
2356 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2360 sid_len = MIN(data_count-4,SID_MAX_SIZE);
2362 /* unknown 4 bytes: this is not the length of the sid :-( */
2363 /*unknown = IVAL(pdata,0);*/
2365 if (!sid_parse(pdata+4,sid_len,&sid)) {
2366 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2369 DEBUGADD(10, ("for SID: %s\n", sid_string_dbg(&sid)));
2371 if (!sid_to_uid(&sid, &uid)) {
2372 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2373 sid_string_dbg(&sid),
2374 (unsigned long)sid_len));
2378 /* we can take a look at the find source :-)
2380 * find ./ -uid $uid -name '*' is what we need here
2383 * and send 4bytes len and then NULL terminated unicode strings
2386 * but I don't know how to deal with the paged results
2387 * (maybe we can hang the result anywhere in the fsp struct)
2389 * we don't send all files at once
2390 * and at the next we should *not* start from the beginning,
2391 * so we have to cache the result
2396 /* this works for now... */
2397 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2400 case FSCTL_QUERY_ALLOCATED_RANGES:
2402 /* FIXME: This is just a dummy reply, telling that all of the
2403 * file is allocated. MKS cp needs that.
2404 * Adding the real allocated ranges via FIEMAP on Linux
2405 * and SEEK_DATA/SEEK_HOLE on Solaris is needed to make
2406 * this FSCTL correct for sparse files.
2409 uint64_t offset, length;
2411 if (!check_fsp_open(conn, req, fsp)) {
2415 if (data_count != 16) {
2416 DEBUG(0,("FSCTL_QUERY_ALLOCATED_RANGES: data_count(%u) != 16 is invalid!\n",
2418 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2422 if (max_data_count < 16) {
2423 DEBUG(0,("FSCTL_QUERY_ALLOCATED_RANGES: max_data_count(%u) < 16 is invalid!\n",
2425 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2429 offset = BVAL(pdata,0);
2430 length = BVAL(pdata,8);
2432 if (offset + length < offset) {
2433 /* No 64-bit integer wrap. */
2434 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2438 status = vfs_stat_fsp(fsp);
2439 if (!NT_STATUS_IS_OK(status)) {
2440 reply_nterror(req, status);
2444 if (offset > fsp->fsp_name->st.st_ex_size ||
2445 fsp->fsp_name->st.st_ex_size == 0 ||
2447 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2449 uint64_t end = offset + length;
2450 end = MIN(end, fsp->fsp_name->st.st_ex_size);
2453 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
2458 case FSCTL_IS_VOLUME_DIRTY:
2459 DEBUG(10,("FSCTL_IS_VOLUME_DIRTY: called on FID[0x%04X] "
2460 "(but not implemented)\n", (int)fidnum));
2462 * http://msdn.microsoft.com/en-us/library/cc232128%28PROT.10%29.aspx
2463 * says we have to respond with NT_STATUS_INVALID_PARAMETER
2465 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2468 /* Only print this once... */
2469 if (!logged_ioctl_message) {
2470 logged_ioctl_message = true;
2471 DEBUG(2,("call_nt_transact_ioctl(0x%x): "
2472 "Currently not implemented.\n",
2477 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2481 #ifdef HAVE_SYS_QUOTAS
2482 /****************************************************************************
2483 Reply to get user quota
2484 ****************************************************************************/
2486 static void call_nt_transact_get_user_quota(connection_struct *conn,
2487 struct smb_request *req,
2491 uint32 parameter_count,
2494 uint32 max_data_count)
2496 NTSTATUS nt_status = NT_STATUS_OK;
2497 char *params = *ppparams;
2498 char *pdata = *ppdata;
2500 int data_len=0,param_len=0;
2503 files_struct *fsp = NULL;
2507 bool start_enum = True;
2508 SMB_NTQUOTA_STRUCT qt;
2509 SMB_NTQUOTA_LIST *tmp_list;
2510 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2515 if (get_current_uid(conn) != 0) {
2516 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2517 "[%s]\n", lp_servicename(SNUM(conn)),
2518 conn->session_info->unix_name));
2519 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2524 * Ensure minimum number of parameters sent.
2527 if (parameter_count < 4) {
2528 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2529 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2533 /* maybe we can check the quota_fnum */
2534 fsp = file_fsp(req, SVAL(params,0));
2535 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2536 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2537 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2541 /* the NULL pointer checking for fsp->fake_file_handle->pd
2542 * is done by CHECK_NTQUOTA_HANDLE_OK()
2544 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->private_data;
2546 level = SVAL(params,2);
2548 /* unknown 12 bytes leading in params */
2551 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2552 /* seems that we should continue with the enum here --metze */
2554 if (qt_handle->quota_list!=NULL &&
2555 qt_handle->tmp_list==NULL) {
2558 free_ntquota_list(&(qt_handle->quota_list));
2560 /* Realloc the size of parameters and data we will return */
2562 params = nttrans_realloc(ppparams, param_len);
2563 if(params == NULL) {
2564 reply_nterror(req, NT_STATUS_NO_MEMORY);
2569 SIVAL(params,0,data_len);
2576 case TRANSACT_GET_USER_QUOTA_LIST_START:
2578 if (qt_handle->quota_list==NULL &&
2579 qt_handle->tmp_list==NULL) {
2583 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2584 reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
2588 /* Realloc the size of parameters and data we will return */
2590 params = nttrans_realloc(ppparams, param_len);
2591 if(params == NULL) {
2592 reply_nterror(req, NT_STATUS_NO_MEMORY);
2596 /* we should not trust the value in max_data_count*/
2597 max_data_count = MIN(max_data_count,2048);
2599 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2601 reply_nterror(req, NT_STATUS_NO_MEMORY);
2607 /* set params Size of returned Quota Data 4 bytes*/
2608 /* but set it later when we know it */
2610 /* for each entry push the data */
2613 qt_handle->tmp_list = qt_handle->quota_list;
2616 tmp_list = qt_handle->tmp_list;
2618 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2619 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2621 sid_len = ndr_size_dom_sid(
2622 &tmp_list->quotas->sid, 0);
2623 entry_len = 40 + sid_len;
2625 /* nextoffset entry 4 bytes */
2626 SIVAL(entry,0,entry_len);
2628 /* then the len of the SID 4 bytes */
2629 SIVAL(entry,4,sid_len);
2631 /* unknown data 8 bytes uint64_t */
2632 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-metze*/
2634 /* the used disk space 8 bytes uint64_t */
2635 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2637 /* the soft quotas 8 bytes uint64_t */
2638 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2640 /* the hard quotas 8 bytes uint64_t */
2641 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2643 /* and now the SID */
2644 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2647 qt_handle->tmp_list = tmp_list;
2649 /* overwrite the offset of the last entry */
2650 SIVAL(entry-entry_len,0,0);
2652 data_len = 4+qt_len;
2653 /* overwrite the params quota_data_len */
2654 SIVAL(params,0,data_len);
2658 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2660 /* unknown 4 bytes IVAL(pdata,0) */
2662 if (data_count < 8) {
2663 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2664 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2668 sid_len = IVAL(pdata,4);
2669 /* Ensure this is less than 1mb. */
2670 if (sid_len > (1024*1024)) {
2671 reply_nterror(req, NT_STATUS_NO_MEMORY);
2675 if (data_count < 8+sid_len) {
2676 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2677 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2681 data_len = 4+40+sid_len;
2683 if (max_data_count < data_len) {
2684 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2685 max_data_count, data_len));
2687 SIVAL(params,0,data_len);
2689 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2693 if (!sid_parse(pdata+8,sid_len,&sid)) {
2694 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2698 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2701 * we have to return zero's in all fields
2702 * instead of returning an error here
2707 /* Realloc the size of parameters and data we will return */
2709 params = nttrans_realloc(ppparams, param_len);
2710 if(params == NULL) {
2711 reply_nterror(req, NT_STATUS_NO_MEMORY);
2715 pdata = nttrans_realloc(ppdata, data_len);
2717 reply_nterror(req, NT_STATUS_NO_MEMORY);
2723 /* set params Size of returned Quota Data 4 bytes*/
2724 SIVAL(params,0,data_len);
2726 /* nextoffset entry 4 bytes */
2729 /* then the len of the SID 4 bytes */
2730 SIVAL(entry,4,sid_len);
2732 /* unknown data 8 bytes uint64_t */
2733 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-mezte*/
2735 /* the used disk space 8 bytes uint64_t */
2736 SBIG_UINT(entry,16,qt.usedspace);
2738 /* the soft quotas 8 bytes uint64_t */
2739 SBIG_UINT(entry,24,qt.softlim);
2741 /* the hard quotas 8 bytes uint64_t */
2742 SBIG_UINT(entry,32,qt.hardlim);
2744 /* and now the SID */
2745 sid_linearize(entry+40, sid_len, &sid);
2750 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2751 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2756 send_nt_replies(conn, req, nt_status, params, param_len,
2760 /****************************************************************************
2761 Reply to set user quota
2762 ****************************************************************************/
2764 static void call_nt_transact_set_user_quota(connection_struct *conn,
2765 struct smb_request *req,
2769 uint32 parameter_count,
2772 uint32 max_data_count)
2774 char *params = *ppparams;
2775 char *pdata = *ppdata;
2776 int data_len=0,param_len=0;
2777 SMB_NTQUOTA_STRUCT qt;
2780 files_struct *fsp = NULL;
2785 if (get_current_uid(conn) != 0) {
2786 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2787 "[%s]\n", lp_servicename(SNUM(conn)),
2788 conn->session_info->unix_name));
2789 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2794 * Ensure minimum number of parameters sent.
2797 if (parameter_count < 2) {
2798 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2799 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2803 /* maybe we can check the quota_fnum */
2804 fsp = file_fsp(req, SVAL(params,0));
2805 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2806 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2807 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2811 if (data_count < 40) {
2812 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2813 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2817 /* offset to next quota record.
2818 * 4 bytes IVAL(pdata,0)
2823 sid_len = IVAL(pdata,4);
2825 if (data_count < 40+sid_len || (40+sid_len < sid_len)) {
2826 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2827 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2831 /* unknown 8 bytes in pdata
2832 * maybe its the change time in NTTIME
2835 /* the used space 8 bytes (uint64_t)*/
2836 qt.usedspace = (uint64_t)IVAL(pdata,16);
2837 #ifdef LARGE_SMB_OFF_T
2838 qt.usedspace |= (((uint64_t)IVAL(pdata,20)) << 32);
2839 #else /* LARGE_SMB_OFF_T */
2840 if ((IVAL(pdata,20) != 0)&&
2841 ((qt.usedspace != 0xFFFFFFFF)||
2842 (IVAL(pdata,20)!=0xFFFFFFFF))) {
2843 /* more than 32 bits? */
2844 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2847 #endif /* LARGE_SMB_OFF_T */
2849 /* the soft quotas 8 bytes (uint64_t)*/
2850 qt.softlim = (uint64_t)IVAL(pdata,24);
2851 #ifdef LARGE_SMB_OFF_T
2852 qt.softlim |= (((uint64_t)IVAL(pdata,28)) << 32);
2853 #else /* LARGE_SMB_OFF_T */
2854 if ((IVAL(pdata,28) != 0)&&
2855 ((qt.softlim != 0xFFFFFFFF)||
2856 (IVAL(pdata,28)!=0xFFFFFFFF))) {
2857 /* more than 32 bits? */
2858 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2861 #endif /* LARGE_SMB_OFF_T */
2863 /* the hard quotas 8 bytes (uint64_t)*/
2864 qt.hardlim = (uint64_t)IVAL(pdata,32);
2865 #ifdef LARGE_SMB_OFF_T
2866 qt.hardlim |= (((uint64_t)IVAL(pdata,36)) << 32);
2867 #else /* LARGE_SMB_OFF_T */
2868 if ((IVAL(pdata,36) != 0)&&
2869 ((qt.hardlim != 0xFFFFFFFF)||
2870 (IVAL(pdata,36)!=0xFFFFFFFF))) {
2871 /* more than 32 bits? */
2872 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2875 #endif /* LARGE_SMB_OFF_T */
2877 if (!sid_parse(pdata+40,sid_len,&sid)) {
2878 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2882 DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid)));
2884 /* 44 unknown bytes left... */
2886 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2887 reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
2891 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2894 #endif /* HAVE_SYS_QUOTAS */
2896 static void handle_nttrans(connection_struct *conn,
2897 struct trans_state *state,
2898 struct smb_request *req)
2900 if (get_Protocol() >= PROTOCOL_NT1) {
2901 req->flags2 |= 0x40; /* IS_LONG_NAME */
2902 SSVAL(req->inbuf,smb_flg2,req->flags2);
2906 SMB_PERFCOUNT_SET_SUBOP(&req->pcd, state->call);
2908 /* Now we must call the relevant NT_TRANS function */
2909 switch(state->call) {
2910 case NT_TRANSACT_CREATE:
2912 START_PROFILE(NT_transact_create);
2913 call_nt_transact_create(
2915 &state->setup, state->setup_count,
2916 &state->param, state->total_param,
2917 &state->data, state->total_data,
2918 state->max_data_return);
2919 END_PROFILE(NT_transact_create);
2923 case NT_TRANSACT_IOCTL:
2925 START_PROFILE(NT_transact_ioctl);
2926 call_nt_transact_ioctl(
2928 &state->setup, state->setup_count,
2929 &state->param, state->total_param,
2930 &state->data, state->total_data,
2931 state->max_data_return);
2932 END_PROFILE(NT_transact_ioctl);
2936 case NT_TRANSACT_SET_SECURITY_DESC:
2938 START_PROFILE(NT_transact_set_security_desc);
2939 call_nt_transact_set_security_desc(
2941 &state->setup, state->setup_count,
2942 &state->param, state->total_param,
2943 &state->data, state->total_data,
2944 state->max_data_return);
2945 END_PROFILE(NT_transact_set_security_desc);
2949 case NT_TRANSACT_NOTIFY_CHANGE:
2951 START_PROFILE(NT_transact_notify_change);
2952 call_nt_transact_notify_change(
2954 &state->setup, state->setup_count,
2955 &state->param, state->total_param,
2956 &state->data, state->total_data,
2957 state->max_data_return,
2958 state->max_param_return);
2959 END_PROFILE(NT_transact_notify_change);
2963 case NT_TRANSACT_RENAME:
2965 START_PROFILE(NT_transact_rename);
2966 call_nt_transact_rename(
2968 &state->setup, state->setup_count,
2969 &state->param, state->total_param,
2970 &state->data, state->total_data,
2971 state->max_data_return);
2972 END_PROFILE(NT_transact_rename);
2976 case NT_TRANSACT_QUERY_SECURITY_DESC:
2978 START_PROFILE(NT_transact_query_security_desc);
2979 call_nt_transact_query_security_desc(
2981 &state->setup, state->setup_count,
2982 &state->param, state->total_param,
2983 &state->data, state->total_data,
2984 state->max_data_return);
2985 END_PROFILE(NT_transact_query_security_desc);
2989 #ifdef HAVE_SYS_QUOTAS
2990 case NT_TRANSACT_GET_USER_QUOTA:
2992 START_PROFILE(NT_transact_get_user_quota);
2993 call_nt_transact_get_user_quota(
2995 &state->setup, state->setup_count,
2996 &state->param, state->total_param,
2997 &state->data, state->total_data,
2998 state->max_data_return);
2999 END_PROFILE(NT_transact_get_user_quota);
3003 case NT_TRANSACT_SET_USER_QUOTA:
3005 START_PROFILE(NT_transact_set_user_quota);
3006 call_nt_transact_set_user_quota(
3008 &state->setup, state->setup_count,
3009 &state->param, state->total_param,
3010 &state->data, state->total_data,
3011 state->max_data_return);
3012 END_PROFILE(NT_transact_set_user_quota);
3015 #endif /* HAVE_SYS_QUOTAS */
3018 /* Error in request */
3019 DEBUG(0,("handle_nttrans: Unknown request %d in "
3020 "nttrans call\n", state->call));
3021 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
3027 /****************************************************************************
3028 Reply to a SMBNTtrans.
3029 ****************************************************************************/
3031 void reply_nttrans(struct smb_request *req)
3033 connection_struct *conn = req->conn;
3038 uint16 function_code;
3040 struct trans_state *state;
3042 START_PROFILE(SMBnttrans);
3044 if (req->wct < 19) {
3045 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3046 END_PROFILE(SMBnttrans);
3050 pscnt = IVAL(req->vwv+9, 1);
3051 psoff = IVAL(req->vwv+11, 1);
3052 dscnt = IVAL(req->vwv+13, 1);
3053 dsoff = IVAL(req->vwv+15, 1);
3054 function_code = SVAL(req->vwv+18, 0);
3056 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
3057 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
3058 END_PROFILE(SMBnttrans);
3062 result = allow_new_trans(conn->pending_trans, req->mid);
3063 if (!NT_STATUS_IS_OK(result)) {
3064 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
3065 reply_nterror(req, result);
3066 END_PROFILE(SMBnttrans);
3070 if ((state = TALLOC_P(conn, struct trans_state)) == NULL) {
3071 reply_nterror(req, NT_STATUS_NO_MEMORY);
3072 END_PROFILE(SMBnttrans);
3076 state->cmd = SMBnttrans;
3078 state->mid = req->mid;
3079 state->vuid = req->vuid;
3080 state->total_data = IVAL(req->vwv+3, 1);
3082 state->total_param = IVAL(req->vwv+1, 1);
3083 state->param = NULL;
3084 state->max_data_return = IVAL(req->vwv+7, 1);
3085 state->max_param_return = IVAL(req->vwv+5, 1);
3087 /* setup count is in *words* */
3088 state->setup_count = 2*CVAL(req->vwv+17, 1);
3089 state->setup = NULL;
3090 state->call = function_code;
3092 DEBUG(10, ("num_setup=%u, "
3093 "param_total=%u, this_param=%u, max_param=%u, "
3094 "data_total=%u, this_data=%u, max_data=%u, "
3095 "param_offset=%u, data_offset=%u\n",
3096 (unsigned)state->setup_count,
3097 (unsigned)state->total_param, (unsigned)pscnt,
3098 (unsigned)state->max_param_return,
3099 (unsigned)state->total_data, (unsigned)dscnt,
3100 (unsigned)state->max_data_return,
3101 (unsigned)psoff, (unsigned)dsoff));
3104 * All nttrans messages we handle have smb_wct == 19 +
3105 * state->setup_count. Ensure this is so as a sanity check.
3108 if(req->wct != 19 + (state->setup_count/2)) {
3109 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
3110 req->wct, 19 + (state->setup_count/2)));
3114 /* Don't allow more than 128mb for each value. */
3115 if ((state->total_data > (1024*1024*128)) ||
3116 (state->total_param > (1024*1024*128))) {
3117 reply_nterror(req, NT_STATUS_NO_MEMORY);
3118 END_PROFILE(SMBnttrans);
3122 if ((dscnt > state->total_data) || (pscnt > state->total_param))
3125 if (state->total_data) {
3127 if (trans_oob(state->total_data, 0, dscnt)
3128 || trans_oob(smb_len(req->inbuf), dsoff, dscnt)) {
3132 /* Can't use talloc here, the core routines do realloc on the
3133 * params and data. */
3134 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
3135 DEBUG(0,("reply_nttrans: data malloc fail for %u "
3136 "bytes !\n", (unsigned int)state->total_data));
3138 reply_nterror(req, NT_STATUS_NO_MEMORY);
3139 END_PROFILE(SMBnttrans);
3143 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
3146 if (state->total_param) {
3148 if (trans_oob(state->total_param, 0, pscnt)
3149 || trans_oob(smb_len(req->inbuf), psoff, pscnt)) {
3153 /* Can't use talloc here, the core routines do realloc on the
3154 * params and data. */
3155 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
3156 DEBUG(0,("reply_nttrans: param malloc fail for %u "
3157 "bytes !\n", (unsigned int)state->total_param));
3158 SAFE_FREE(state->data);
3160 reply_nterror(req, NT_STATUS_NO_MEMORY);
3161 END_PROFILE(SMBnttrans);
3165 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
3168 state->received_data = dscnt;
3169 state->received_param = pscnt;
3171 if(state->setup_count > 0) {
3172 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
3173 state->setup_count));
3176 * No overflow possible here, state->setup_count is an
3177 * unsigned int, being filled by a single byte from
3178 * CVAL(req->vwv+13, 0) above. The cast in the comparison
3179 * below is not necessary, it's here to clarify things. The
3180 * validity of req->vwv and req->wct has been checked in
3181 * init_smb_request already.
3183 if ((state->setup_count/2) + 19 > (unsigned int)req->wct) {
3187 state->setup = (uint16 *)TALLOC(state, state->setup_count);
3188 if (state->setup == NULL) {
3189 DEBUG(0,("reply_nttrans : Out of memory\n"));
3190 SAFE_FREE(state->data);
3191 SAFE_FREE(state->param);
3193 reply_nterror(req, NT_STATUS_NO_MEMORY);
3194 END_PROFILE(SMBnttrans);
3198 memcpy(state->setup, req->vwv+19, state->setup_count);
3199 dump_data(10, (uint8 *)state->setup, state->setup_count);
3202 if ((state->received_data == state->total_data) &&
3203 (state->received_param == state->total_param)) {
3204 handle_nttrans(conn, state, req);
3205 SAFE_FREE(state->param);
3206 SAFE_FREE(state->data);
3208 END_PROFILE(SMBnttrans);
3212 DLIST_ADD(conn->pending_trans, state);
3214 /* We need to send an interim response then receive the rest
3215 of the parameter/data bytes */
3216 reply_outbuf(req, 0, 0);
3217 show_msg((char *)req->outbuf);
3218 END_PROFILE(SMBnttrans);
3223 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
3224 SAFE_FREE(state->data);
3225 SAFE_FREE(state->param);
3227 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3228 END_PROFILE(SMBnttrans);
3232 /****************************************************************************
3233 Reply to a SMBnttranss
3234 ****************************************************************************/
3236 void reply_nttranss(struct smb_request *req)
3238 connection_struct *conn = req->conn;
3239 uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
3240 struct trans_state *state;
3242 START_PROFILE(SMBnttranss);
3244 show_msg((char *)req->inbuf);
3246 if (req->wct < 18) {
3247 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3248 END_PROFILE(SMBnttranss);
3252 for (state = conn->pending_trans; state != NULL;
3253 state = state->next) {
3254 if (state->mid == req->mid) {
3259 if ((state == NULL) || (state->cmd != SMBnttrans)) {
3260 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3261 END_PROFILE(SMBnttranss);
3265 /* Revise state->total_param and state->total_data in case they have
3266 changed downwards */
3267 if (IVAL(req->vwv+1, 1) < state->total_param) {
3268 state->total_param = IVAL(req->vwv+1, 1);
3270 if (IVAL(req->vwv+3, 1) < state->total_data) {
3271 state->total_data = IVAL(req->vwv+3, 1);
3274 pcnt = IVAL(req->vwv+5, 1);
3275 poff = IVAL(req->vwv+7, 1);
3276 pdisp = IVAL(req->vwv+9, 1);
3278 dcnt = IVAL(req->vwv+11, 1);
3279 doff = IVAL(req->vwv+13, 1);
3280 ddisp = IVAL(req->vwv+15, 1);
3282 state->received_param += pcnt;
3283 state->received_data += dcnt;
3285 if ((state->received_data > state->total_data) ||
3286 (state->received_param > state->total_param))
3290 if (trans_oob(state->total_param, pdisp, pcnt)
3291 || trans_oob(smb_len(req->inbuf), poff, pcnt)) {
3294 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,pcnt);
3298 if (trans_oob(state->total_data, ddisp, dcnt)
3299 || trans_oob(smb_len(req->inbuf), doff, dcnt)) {
3302 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
3305 if ((state->received_param < state->total_param) ||
3306 (state->received_data < state->total_data)) {
3307 END_PROFILE(SMBnttranss);
3311 handle_nttrans(conn, state, req);
3313 DLIST_REMOVE(conn->pending_trans, state);
3314 SAFE_FREE(state->data);
3315 SAFE_FREE(state->param);
3317 END_PROFILE(SMBnttranss);
3322 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
3323 DLIST_REMOVE(conn->pending_trans, state);
3324 SAFE_FREE(state->data);
3325 SAFE_FREE(state->param);
3327 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3328 END_PROFILE(SMBnttranss);