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 "smbd/globals.h"
24 extern const struct generic_mapping file_generic_mapping;
26 static char *nttrans_realloc(char **ptr, size_t size)
29 smb_panic("nttrans_realloc() called with NULL ptr");
32 *ptr = (char *)SMB_REALLOC(*ptr, size);
36 memset(*ptr,'\0',size);
40 /****************************************************************************
41 Send the required number of replies back.
42 We assume all fields other than the data fields are
43 set correctly for the type of call.
44 HACK ! Always assumes smb_setup field is zero.
45 ****************************************************************************/
47 void send_nt_replies(connection_struct *conn,
48 struct smb_request *req, NTSTATUS nt_error,
49 char *params, int paramsize,
50 char *pdata, int datasize)
52 int data_to_send = datasize;
53 int params_to_send = paramsize;
57 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
58 int alignment_offset = 3;
59 int data_alignment_offset = 0;
60 struct smbd_server_connection *sconn = smbd_server_conn;
61 int max_send = sconn->smb1.sessions.max_send;
64 * If there genuinely are no parameters or data to send just send
68 if(params_to_send == 0 && data_to_send == 0) {
69 reply_outbuf(req, 18, 0);
70 if (NT_STATUS_V(nt_error)) {
71 error_packet_set((char *)req->outbuf,
75 show_msg((char *)req->outbuf);
76 if (!srv_send_smb(smbd_server_fd(),
79 IS_CONN_ENCRYPTED(conn),
81 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
83 TALLOC_FREE(req->outbuf);
88 * When sending params and data ensure that both are nicely aligned.
89 * Only do this alignment when there is also data to send - else
90 * can cause NT redirector problems.
93 if (((params_to_send % 4) != 0) && (data_to_send != 0)) {
94 data_alignment_offset = 4 - (params_to_send % 4);
98 * Space is bufsize minus Netbios over TCP header minus SMB header.
99 * The alignment_offset is to align the param bytes on a four byte
100 * boundary (2 bytes for data len, one byte pad).
101 * NT needs this to work correctly.
104 useable_space = max_send - (smb_size
107 + data_alignment_offset);
109 if (useable_space < 0) {
110 char *msg = talloc_asprintf(
112 "send_nt_replies failed sanity useable_space = %d!!!",
114 DEBUG(0, ("%s\n", msg));
115 exit_server_cleanly(msg);
118 while (params_to_send || data_to_send) {
121 * Calculate whether we will totally or partially fill this packet.
124 total_sent_thistime = params_to_send + data_to_send;
127 * We can never send more than useable_space.
130 total_sent_thistime = MIN(total_sent_thistime, useable_space);
132 reply_outbuf(req, 18,
133 total_sent_thistime + alignment_offset
134 + data_alignment_offset);
137 * Set total params and data to be sent.
140 SIVAL(req->outbuf,smb_ntr_TotalParameterCount,paramsize);
141 SIVAL(req->outbuf,smb_ntr_TotalDataCount,datasize);
144 * Calculate how many parameters and data we can fit into
145 * this packet. Parameters get precedence.
148 params_sent_thistime = MIN(params_to_send,useable_space);
149 data_sent_thistime = useable_space - params_sent_thistime;
150 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
152 SIVAL(req->outbuf, smb_ntr_ParameterCount,
153 params_sent_thistime);
155 if(params_sent_thistime == 0) {
156 SIVAL(req->outbuf,smb_ntr_ParameterOffset,0);
157 SIVAL(req->outbuf,smb_ntr_ParameterDisplacement,0);
160 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
161 * parameter bytes, however the first 4 bytes of outbuf are
162 * the Netbios over TCP header. Thus use smb_base() to subtract
163 * them from the calculation.
166 SIVAL(req->outbuf,smb_ntr_ParameterOffset,
167 ((smb_buf(req->outbuf)+alignment_offset)
168 - smb_base(req->outbuf)));
170 * Absolute displacement of param bytes sent in this packet.
173 SIVAL(req->outbuf, smb_ntr_ParameterDisplacement,
178 * Deal with the data portion.
181 SIVAL(req->outbuf, smb_ntr_DataCount, data_sent_thistime);
183 if(data_sent_thistime == 0) {
184 SIVAL(req->outbuf,smb_ntr_DataOffset,0);
185 SIVAL(req->outbuf,smb_ntr_DataDisplacement, 0);
188 * The offset of the data bytes is the offset of the
189 * parameter bytes plus the number of parameters being sent this time.
192 SIVAL(req->outbuf, smb_ntr_DataOffset,
193 ((smb_buf(req->outbuf)+alignment_offset) -
194 smb_base(req->outbuf))
195 + params_sent_thistime + data_alignment_offset);
196 SIVAL(req->outbuf,smb_ntr_DataDisplacement, pd - pdata);
200 * Copy the param bytes into the packet.
203 if(params_sent_thistime) {
204 if (alignment_offset != 0) {
205 memset(smb_buf(req->outbuf), 0,
208 memcpy((smb_buf(req->outbuf)+alignment_offset), pp,
209 params_sent_thistime);
213 * Copy in the data bytes
216 if(data_sent_thistime) {
217 if (data_alignment_offset != 0) {
218 memset((smb_buf(req->outbuf)+alignment_offset+
219 params_sent_thistime), 0,
220 data_alignment_offset);
222 memcpy(smb_buf(req->outbuf)+alignment_offset
223 +params_sent_thistime+data_alignment_offset,
224 pd,data_sent_thistime);
227 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
228 params_sent_thistime, data_sent_thistime, useable_space));
229 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
230 params_to_send, data_to_send, paramsize, datasize));
232 if (NT_STATUS_V(nt_error)) {
233 error_packet_set((char *)req->outbuf,
238 /* Send the packet */
239 show_msg((char *)req->outbuf);
240 if (!srv_send_smb(smbd_server_fd(),
243 IS_CONN_ENCRYPTED(conn),
245 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
248 TALLOC_FREE(req->outbuf);
250 pp += params_sent_thistime;
251 pd += data_sent_thistime;
253 params_to_send -= params_sent_thistime;
254 data_to_send -= data_sent_thistime;
260 if(params_to_send < 0 || data_to_send < 0) {
261 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
262 params_to_send, data_to_send));
263 exit_server_cleanly("send_nt_replies: internal error");
268 /****************************************************************************
269 Reply to an NT create and X call on a pipe
270 ****************************************************************************/
272 static void nt_open_pipe(char *fname, connection_struct *conn,
273 struct smb_request *req, int *ppnum)
278 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
280 /* Strip \\ off the name. */
283 status = open_np_file(req, fname, &fsp);
284 if (!NT_STATUS_IS_OK(status)) {
285 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
286 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
290 reply_nterror(req, status);
298 /****************************************************************************
299 Reply to an NT create and X call for pipes.
300 ****************************************************************************/
302 static void do_ntcreate_pipe_open(connection_struct *conn,
303 struct smb_request *req)
308 uint32 flags = IVAL(req->vwv+3, 1);
309 TALLOC_CTX *ctx = talloc_tos();
311 srvstr_pull_req_talloc(ctx, req, &fname, req->buf, STR_TERMINATE);
314 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
318 nt_open_pipe(fname, conn, req, &pnum);
326 * Deal with pipe return.
329 if (flags & EXTENDED_RESPONSE_REQUIRED) {
330 /* This is very strange. We
331 * return 50 words, but only set
332 * the wcnt to 42 ? It's definately
333 * what happens on the wire....
335 reply_outbuf(req, 50, 0);
336 SCVAL(req->outbuf,smb_wct,42);
338 reply_outbuf(req, 34, 0);
341 p = (char *)req->outbuf + smb_vwv2;
345 SIVAL(p,0,FILE_WAS_OPENED);
348 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
351 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
353 SSVAL(p,2, 0x5FF); /* ? */
356 if (flags & EXTENDED_RESPONSE_REQUIRED) {
358 SIVAL(p,0,FILE_GENERIC_ALL);
360 * For pipes W2K3 seems to return
362 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
364 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
367 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
372 struct case_semantics_state {
373 connection_struct *conn;
376 bool short_case_preserve;
379 /****************************************************************************
380 Restore case semantics.
381 ****************************************************************************/
383 static int restore_case_semantics(struct case_semantics_state *state)
385 state->conn->case_sensitive = state->case_sensitive;
386 state->conn->case_preserve = state->case_preserve;
387 state->conn->short_case_preserve = state->short_case_preserve;
391 /****************************************************************************
393 ****************************************************************************/
395 static struct case_semantics_state *set_posix_case_semantics(TALLOC_CTX *mem_ctx,
396 connection_struct *conn)
398 struct case_semantics_state *result;
400 if (!(result = talloc(mem_ctx, struct case_semantics_state))) {
405 result->case_sensitive = conn->case_sensitive;
406 result->case_preserve = conn->case_preserve;
407 result->short_case_preserve = conn->short_case_preserve;
410 conn->case_sensitive = True;
411 conn->case_preserve = True;
412 conn->short_case_preserve = True;
414 talloc_set_destructor(result, restore_case_semantics);
419 /****************************************************************************
420 Reply to an NT create and X call.
421 ****************************************************************************/
423 void reply_ntcreate_and_X(struct smb_request *req)
425 connection_struct *conn = req->conn;
426 struct smb_filename *smb_fname = NULL;
430 uint32 file_attributes;
432 uint32 create_disposition;
433 uint32 create_options;
435 uint64_t allocation_size;
436 /* Breakout the oplock request bits so we can set the
437 reply bits separately. */
439 SMB_OFF_T file_len = 0;
441 files_struct *fsp = NULL;
443 struct timespec create_timespec;
444 struct timespec c_timespec;
445 struct timespec a_timespec;
446 struct timespec m_timespec;
447 struct timespec write_time_ts;
450 uint8_t oplock_granted = NO_OPLOCK_RETURN;
451 struct case_semantics_state *case_state = NULL;
452 TALLOC_CTX *ctx = talloc_tos();
454 START_PROFILE(SMBntcreateX);
457 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
461 flags = IVAL(req->vwv+3, 1);
462 access_mask = IVAL(req->vwv+7, 1);
463 file_attributes = IVAL(req->vwv+13, 1);
464 share_access = IVAL(req->vwv+15, 1);
465 create_disposition = IVAL(req->vwv+17, 1);
466 create_options = IVAL(req->vwv+19, 1);
467 root_dir_fid = (uint16)IVAL(req->vwv+5, 1);
469 allocation_size = (uint64_t)IVAL(req->vwv+9, 1);
470 #ifdef LARGE_SMB_OFF_T
471 allocation_size |= (((uint64_t)IVAL(req->vwv+11, 1)) << 32);
474 srvstr_get_path_req(ctx, req, &fname, (const char *)req->buf,
475 STR_TERMINATE, &status);
477 if (!NT_STATUS_IS_OK(status)) {
478 reply_nterror(req, status);
482 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
483 "file_attributes = 0x%x, share_access = 0x%x, "
484 "create_disposition = 0x%x create_options = 0x%x "
485 "root_dir_fid = 0x%x, fname = %s\n",
487 (unsigned int)access_mask,
488 (unsigned int)file_attributes,
489 (unsigned int)share_access,
490 (unsigned int)create_disposition,
491 (unsigned int)create_options,
492 (unsigned int)root_dir_fid,
496 * we need to remove ignored bits when they come directly from the client
497 * because we reuse some of them for internal stuff
499 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
502 * If it's an IPC, use the pipe handler.
506 if (lp_nt_pipe_support()) {
507 do_ntcreate_pipe_open(conn, req);
510 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
514 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
515 if (oplock_request) {
516 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
520 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
521 case_state = set_posix_case_semantics(ctx, conn);
523 reply_nterror(req, NT_STATUS_NO_MEMORY);
528 status = filename_convert(ctx,
530 req->flags2 & FLAGS2_DFS_PATHNAMES,
536 TALLOC_FREE(case_state);
538 if (!NT_STATUS_IS_OK(status)) {
539 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
541 NT_STATUS_PATH_NOT_COVERED,
545 reply_nterror(req, status);
550 * Bug #6898 - clients using Windows opens should
551 * never be able to set this attribute into the
554 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
556 status = SMB_VFS_CREATE_FILE(
559 root_dir_fid, /* root_dir_fid */
560 smb_fname, /* fname */
561 access_mask, /* access_mask */
562 share_access, /* share_access */
563 create_disposition, /* create_disposition*/
564 create_options, /* create_options */
565 file_attributes, /* file_attributes */
566 oplock_request, /* oplock_request */
567 allocation_size, /* allocation_size */
573 if (!NT_STATUS_IS_OK(status)) {
574 if (open_was_deferred(req->mid)) {
575 /* We have re-scheduled this call, no error. */
578 reply_openerror(req, status);
582 /* Ensure we're pointing at the correct stat struct. */
583 TALLOC_FREE(smb_fname);
584 smb_fname = fsp->fsp_name;
587 * If the caller set the extended oplock request bit
588 * and we granted one (by whatever means) - set the
589 * correct bit for extended oplock reply.
592 if (oplock_request &&
593 (lp_fake_oplocks(SNUM(conn))
594 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
597 * Exclusive oplock granted
600 if (flags & REQUEST_BATCH_OPLOCK) {
601 oplock_granted = BATCH_OPLOCK_RETURN;
603 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
605 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
606 oplock_granted = LEVEL_II_OPLOCK_RETURN;
608 oplock_granted = NO_OPLOCK_RETURN;
611 file_len = smb_fname->st.st_ex_size;
613 if (flags & EXTENDED_RESPONSE_REQUIRED) {
614 /* This is very strange. We
615 * return 50 words, but only set
616 * the wcnt to 42 ? It's definately
617 * what happens on the wire....
619 reply_outbuf(req, 50, 0);
620 SCVAL(req->outbuf,smb_wct,42);
622 reply_outbuf(req, 34, 0);
625 p = (char *)req->outbuf + smb_vwv2;
627 SCVAL(p, 0, oplock_granted);
630 SSVAL(p,0,fsp->fnum);
632 if ((create_disposition == FILE_SUPERSEDE)
633 && (info == FILE_WAS_OVERWRITTEN)) {
634 SIVAL(p,0,FILE_WAS_SUPERSEDED);
640 fattr = dos_mode(conn, smb_fname);
642 fattr = FILE_ATTRIBUTE_NORMAL;
645 /* Deal with other possible opens having a modified
647 ZERO_STRUCT(write_time_ts);
648 get_file_infos(fsp->file_id, NULL, &write_time_ts);
649 if (!null_timespec(write_time_ts)) {
650 update_stat_ex_mtime(&smb_fname->st, write_time_ts);
654 create_timespec = get_create_timespec(conn, fsp, smb_fname);
655 a_timespec = smb_fname->st.st_ex_atime;
656 m_timespec = smb_fname->st.st_ex_mtime;
657 c_timespec = get_change_timespec(conn, fsp, smb_fname);
659 if (lp_dos_filetime_resolution(SNUM(conn))) {
660 dos_filetime_timespec(&create_timespec);
661 dos_filetime_timespec(&a_timespec);
662 dos_filetime_timespec(&m_timespec);
663 dos_filetime_timespec(&c_timespec);
666 put_long_date_timespec(conn->ts_res, p, create_timespec); /* create time. */
668 put_long_date_timespec(conn->ts_res, p, a_timespec); /* access time */
670 put_long_date_timespec(conn->ts_res, p, m_timespec); /* write time */
672 put_long_date_timespec(conn->ts_res, p, c_timespec); /* change time */
674 SIVAL(p,0,fattr); /* File Attributes. */
676 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn,fsp,&smb_fname->st));
678 SOFF_T(p,0,file_len);
680 if (flags & EXTENDED_RESPONSE_REQUIRED) {
681 uint16_t file_status = (NO_EAS|NO_SUBSTREAMS|NO_REPARSETAG);
682 size_t num_names = 0;
683 unsigned int num_streams;
684 struct stream_struct *streams = NULL;
686 /* Do we have any EA's ? */
687 status = get_ea_names_from_file(ctx, conn, fsp,
688 smb_fname->base_name, NULL, &num_names);
689 if (NT_STATUS_IS_OK(status) && num_names) {
690 file_status &= ~NO_EAS;
692 status = SMB_VFS_STREAMINFO(conn, NULL, smb_fname->base_name, ctx,
693 &num_streams, &streams);
694 /* There is always one stream, ::$DATA. */
695 if (NT_STATUS_IS_OK(status) && num_streams > 1) {
696 file_status &= ~NO_SUBSTREAMS;
698 TALLOC_FREE(streams);
699 SSVAL(p,2,file_status);
702 SCVAL(p,0,fsp->is_directory ? 1 : 0);
704 if (flags & EXTENDED_RESPONSE_REQUIRED) {
707 if (fsp->is_directory ||
708 can_write_to_file(conn, smb_fname)) {
709 perms = FILE_GENERIC_ALL;
711 perms = FILE_GENERIC_READ|FILE_EXECUTE;
716 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n",
717 fsp->fnum, smb_fname_str_dbg(smb_fname)));
721 END_PROFILE(SMBntcreateX);
725 /****************************************************************************
726 Reply to a NT_TRANSACT_CREATE call to open a pipe.
727 ****************************************************************************/
729 static void do_nt_transact_create_pipe(connection_struct *conn,
730 struct smb_request *req,
731 uint16 **ppsetup, uint32 setup_count,
732 char **ppparams, uint32 parameter_count,
733 char **ppdata, uint32 data_count)
736 char *params = *ppparams;
742 TALLOC_CTX *ctx = talloc_tos();
745 * Ensure minimum number of parameters sent.
748 if(parameter_count < 54) {
749 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
750 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
754 flags = IVAL(params,0);
756 srvstr_get_path(ctx, params, req->flags2, &fname, params+53,
757 parameter_count-53, STR_TERMINATE,
759 if (!NT_STATUS_IS_OK(status)) {
760 reply_nterror(req, status);
764 nt_open_pipe(fname, conn, req, &pnum);
771 /* Realloc the size of parameters and data we will return */
772 if (flags & EXTENDED_RESPONSE_REQUIRED) {
773 /* Extended response is 32 more byyes. */
778 params = nttrans_realloc(ppparams, param_len);
780 reply_nterror(req, NT_STATUS_NO_MEMORY);
785 SCVAL(p,0,NO_OPLOCK_RETURN);
790 SIVAL(p,0,FILE_WAS_OPENED);
794 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
797 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
799 SSVAL(p,2, 0x5FF); /* ? */
802 if (flags & EXTENDED_RESPONSE_REQUIRED) {
804 SIVAL(p,0,FILE_GENERIC_ALL);
806 * For pipes W2K3 seems to return
808 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
810 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
813 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
815 /* Send the required number of replies */
816 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
821 /****************************************************************************
822 Internal fn to set security descriptors.
823 ****************************************************************************/
825 static NTSTATUS set_sd(files_struct *fsp, uint8 *data, uint32 sd_len,
826 uint32 security_info_sent)
828 SEC_DESC *psd = NULL;
832 return NT_STATUS_INVALID_PARAMETER;
835 if (!CAN_WRITE(fsp->conn)) {
836 return NT_STATUS_ACCESS_DENIED;
839 if (!lp_nt_acl_support(SNUM(fsp->conn))) {
843 status = unmarshall_sec_desc(talloc_tos(), data, sd_len, &psd);
845 if (!NT_STATUS_IS_OK(status)) {
849 if (psd->owner_sid == NULL) {
850 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
852 if (psd->group_sid == NULL) {
853 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
856 /* Ensure we have at least one thing set. */
857 if ((security_info_sent & (SECINFO_OWNER|SECINFO_GROUP|SECINFO_DACL|SECINFO_SACL)) == 0) {
858 if (security_info_sent & SECINFO_LABEL) {
859 /* Only consider SECINFO_LABEL if no other
860 bits are set. Just like W2K3 we don't
864 return NT_STATUS_INVALID_PARAMETER;
867 /* Ensure we have the rights to do this. */
868 if (security_info_sent & SECINFO_OWNER) {
869 if (!(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
870 return NT_STATUS_ACCESS_DENIED;
874 if (security_info_sent & SECINFO_GROUP) {
875 if (!(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
876 return NT_STATUS_ACCESS_DENIED;
880 if (security_info_sent & SECINFO_DACL) {
881 if (!(fsp->access_mask & SEC_STD_WRITE_DAC)) {
882 return NT_STATUS_ACCESS_DENIED;
884 /* Convert all the generic bits. */
886 security_acl_map_generic(psd->dacl, &file_generic_mapping);
890 if (security_info_sent & SECINFO_SACL) {
891 if (!(fsp->access_mask & SEC_FLAG_SYSTEM_SECURITY)) {
892 return NT_STATUS_ACCESS_DENIED;
894 /* Convert all the generic bits. */
896 security_acl_map_generic(psd->sacl, &file_generic_mapping);
900 if (DEBUGLEVEL >= 10) {
901 DEBUG(10,("set_sd for file %s\n", fsp_str_dbg(fsp)));
902 NDR_PRINT_DEBUG(security_descriptor, psd);
905 status = SMB_VFS_FSET_NT_ACL(fsp, security_info_sent, psd);
912 /****************************************************************************
913 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
914 ****************************************************************************/
916 struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
918 struct ea_list *ea_list_head = NULL;
925 while (offset + 4 <= data_size) {
926 size_t next_offset = IVAL(pdata,offset);
927 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
933 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
934 if (next_offset == 0) {
938 /* Integer wrap protection for the increment. */
939 if (offset + next_offset < offset) {
943 offset += next_offset;
945 /* Integer wrap protection for while loop. */
946 if (offset + 4 < offset) {
955 /****************************************************************************
956 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
957 ****************************************************************************/
959 static void call_nt_transact_create(connection_struct *conn,
960 struct smb_request *req,
961 uint16 **ppsetup, uint32 setup_count,
962 char **ppparams, uint32 parameter_count,
963 char **ppdata, uint32 data_count,
964 uint32 max_data_count)
966 struct smb_filename *smb_fname = NULL;
968 char *params = *ppparams;
969 char *data = *ppdata;
970 /* Breakout the oplock request bits so we can set the reply bits separately. */
972 SMB_OFF_T file_len = 0;
974 files_struct *fsp = NULL;
978 uint32 file_attributes;
980 uint32 create_disposition;
981 uint32 create_options;
983 struct security_descriptor *sd = NULL;
986 struct timespec create_timespec;
987 struct timespec c_timespec;
988 struct timespec a_timespec;
989 struct timespec m_timespec;
990 struct timespec write_time_ts;
991 struct ea_list *ea_list = NULL;
994 uint64_t allocation_size;
996 uint8_t oplock_granted;
997 struct case_semantics_state *case_state = NULL;
998 TALLOC_CTX *ctx = talloc_tos();
1000 DEBUG(5,("call_nt_transact_create\n"));
1003 * If it's an IPC, use the pipe handler.
1007 if (lp_nt_pipe_support()) {
1008 do_nt_transact_create_pipe(
1010 ppsetup, setup_count,
1011 ppparams, parameter_count,
1012 ppdata, data_count);
1015 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1020 * Ensure minimum number of parameters sent.
1023 if(parameter_count < 54) {
1024 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1025 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1029 flags = IVAL(params,0);
1030 access_mask = IVAL(params,8);
1031 file_attributes = IVAL(params,20);
1032 share_access = IVAL(params,24);
1033 create_disposition = IVAL(params,28);
1034 create_options = IVAL(params,32);
1035 sd_len = IVAL(params,36);
1036 ea_len = IVAL(params,40);
1037 root_dir_fid = (uint16)IVAL(params,4);
1038 allocation_size = (uint64_t)IVAL(params,12);
1039 #ifdef LARGE_SMB_OFF_T
1040 allocation_size |= (((uint64_t)IVAL(params,16)) << 32);
1044 * we need to remove ignored bits when they come directly from the client
1045 * because we reuse some of them for internal stuff
1047 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
1049 /* Ensure the data_len is correct for the sd and ea values given. */
1050 if ((ea_len + sd_len > data_count)
1051 || (ea_len > data_count) || (sd_len > data_count)
1052 || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
1053 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
1054 "%u, data_count = %u\n", (unsigned int)ea_len,
1055 (unsigned int)sd_len, (unsigned int)data_count));
1056 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1061 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
1064 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
1066 if (!NT_STATUS_IS_OK(status)) {
1067 DEBUG(10, ("call_nt_transact_create: "
1068 "unmarshall_sec_desc failed: %s\n",
1069 nt_errstr(status)));
1070 reply_nterror(req, status);
1076 if (!lp_ea_support(SNUM(conn))) {
1077 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
1078 "EA's not supported.\n",
1079 (unsigned int)ea_len));
1080 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
1085 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
1086 "too small (should be more than 10)\n",
1087 (unsigned int)ea_len ));
1088 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1092 /* We have already checked that ea_len <= data_count here. */
1093 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
1095 if (ea_list == NULL) {
1096 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1101 srvstr_get_path(ctx, params, req->flags2, &fname,
1102 params+53, parameter_count-53,
1103 STR_TERMINATE, &status);
1104 if (!NT_STATUS_IS_OK(status)) {
1105 reply_nterror(req, status);
1109 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1110 case_state = set_posix_case_semantics(ctx, conn);
1112 reply_nterror(req, NT_STATUS_NO_MEMORY);
1117 status = filename_convert(ctx,
1119 req->flags2 & FLAGS2_DFS_PATHNAMES,
1125 TALLOC_FREE(case_state);
1127 if (!NT_STATUS_IS_OK(status)) {
1128 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1129 reply_botherror(req,
1130 NT_STATUS_PATH_NOT_COVERED,
1131 ERRSRV, ERRbadpath);
1134 reply_nterror(req, status);
1138 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1139 if (oplock_request) {
1140 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
1145 * Bug #6898 - clients using Windows opens should
1146 * never be able to set this attribute into the
1149 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
1151 status = SMB_VFS_CREATE_FILE(
1154 root_dir_fid, /* root_dir_fid */
1155 smb_fname, /* fname */
1156 access_mask, /* access_mask */
1157 share_access, /* share_access */
1158 create_disposition, /* create_disposition*/
1159 create_options, /* create_options */
1160 file_attributes, /* file_attributes */
1161 oplock_request, /* oplock_request */
1162 allocation_size, /* allocation_size */
1164 ea_list, /* ea_list */
1168 if(!NT_STATUS_IS_OK(status)) {
1169 if (open_was_deferred(req->mid)) {
1170 /* We have re-scheduled this call, no error. */
1173 reply_openerror(req, status);
1177 /* Ensure we're pointing at the correct stat struct. */
1178 TALLOC_FREE(smb_fname);
1179 smb_fname = fsp->fsp_name;
1182 * If the caller set the extended oplock request bit
1183 * and we granted one (by whatever means) - set the
1184 * correct bit for extended oplock reply.
1187 if (oplock_request &&
1188 (lp_fake_oplocks(SNUM(conn))
1189 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
1192 * Exclusive oplock granted
1195 if (flags & REQUEST_BATCH_OPLOCK) {
1196 oplock_granted = BATCH_OPLOCK_RETURN;
1198 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
1200 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1201 oplock_granted = LEVEL_II_OPLOCK_RETURN;
1203 oplock_granted = NO_OPLOCK_RETURN;
1206 file_len = smb_fname->st.st_ex_size;
1208 /* Realloc the size of parameters and data we will return */
1209 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1210 /* Extended response is 32 more byyes. */
1215 params = nttrans_realloc(ppparams, param_len);
1216 if(params == NULL) {
1217 reply_nterror(req, NT_STATUS_NO_MEMORY);
1222 SCVAL(p, 0, oplock_granted);
1225 SSVAL(p,0,fsp->fnum);
1227 if ((create_disposition == FILE_SUPERSEDE)
1228 && (info == FILE_WAS_OVERWRITTEN)) {
1229 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1235 fattr = dos_mode(conn, smb_fname);
1237 fattr = FILE_ATTRIBUTE_NORMAL;
1240 /* Deal with other possible opens having a modified
1242 ZERO_STRUCT(write_time_ts);
1243 get_file_infos(fsp->file_id, NULL, &write_time_ts);
1244 if (!null_timespec(write_time_ts)) {
1245 update_stat_ex_mtime(&smb_fname->st, write_time_ts);
1249 create_timespec = get_create_timespec(conn, fsp, smb_fname);
1250 a_timespec = smb_fname->st.st_ex_atime;
1251 m_timespec = smb_fname->st.st_ex_mtime;
1252 c_timespec = get_change_timespec(conn, fsp, smb_fname);
1254 if (lp_dos_filetime_resolution(SNUM(conn))) {
1255 dos_filetime_timespec(&create_timespec);
1256 dos_filetime_timespec(&a_timespec);
1257 dos_filetime_timespec(&m_timespec);
1258 dos_filetime_timespec(&c_timespec);
1261 put_long_date_timespec(conn->ts_res, p, create_timespec); /* create time. */
1263 put_long_date_timespec(conn->ts_res, p, a_timespec); /* access time */
1265 put_long_date_timespec(conn->ts_res, p, m_timespec); /* write time */
1267 put_long_date_timespec(conn->ts_res, p, c_timespec); /* change time */
1269 SIVAL(p,0,fattr); /* File Attributes. */
1271 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn, fsp, &smb_fname->st));
1273 SOFF_T(p,0,file_len);
1275 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1279 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1281 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1284 if (fsp->is_directory ||
1285 can_write_to_file(conn, smb_fname)) {
1286 perms = FILE_GENERIC_ALL;
1288 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1293 DEBUG(5,("call_nt_transact_create: open name = %s\n",
1294 smb_fname_str_dbg(smb_fname)));
1296 /* Send the required number of replies */
1297 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1302 /****************************************************************************
1303 Reply to a NT CANCEL request.
1304 conn POINTER CAN BE NULL HERE !
1305 ****************************************************************************/
1307 void reply_ntcancel(struct smb_request *req)
1310 * Go through and cancel any pending change notifies.
1313 START_PROFILE(SMBntcancel);
1314 srv_cancel_sign_response(smbd_server_conn);
1315 remove_pending_change_notify_requests_by_mid(req->mid);
1316 remove_pending_lock_requests_by_mid(req->mid);
1318 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", req->mid));
1320 END_PROFILE(SMBntcancel);
1324 /****************************************************************************
1326 ****************************************************************************/
1328 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
1329 connection_struct *conn,
1330 struct smb_request *req,
1331 struct smb_filename *smb_fname_src,
1332 struct smb_filename *smb_fname_dst,
1335 files_struct *fsp1,*fsp2;
1339 NTSTATUS status = NT_STATUS_OK;
1342 if (!CAN_WRITE(conn)) {
1343 status = NT_STATUS_MEDIA_WRITE_PROTECTED;
1347 /* Source must already exist. */
1348 if (!VALID_STAT(smb_fname_src->st)) {
1349 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
1353 /* Ensure attributes match. */
1354 fattr = dos_mode(conn, smb_fname_src);
1355 if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
1356 status = NT_STATUS_NO_SUCH_FILE;
1360 /* Disallow if dst file already exists. */
1361 if (VALID_STAT(smb_fname_dst->st)) {
1362 status = NT_STATUS_OBJECT_NAME_COLLISION;
1366 /* No links from a directory. */
1367 if (S_ISDIR(smb_fname_src->st.st_ex_mode)) {
1368 status = NT_STATUS_FILE_IS_A_DIRECTORY;
1372 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1373 smb_fname_str_dbg(smb_fname_src),
1374 smb_fname_str_dbg(smb_fname_dst)));
1376 status = SMB_VFS_CREATE_FILE(
1379 0, /* root_dir_fid */
1380 smb_fname_src, /* fname */
1381 FILE_READ_DATA, /* access_mask */
1382 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1384 FILE_OPEN, /* create_disposition*/
1385 0, /* create_options */
1386 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
1387 NO_OPLOCK, /* oplock_request */
1388 0, /* allocation_size */
1394 if (!NT_STATUS_IS_OK(status)) {
1398 status = SMB_VFS_CREATE_FILE(
1401 0, /* root_dir_fid */
1402 smb_fname_dst, /* fname */
1403 FILE_WRITE_DATA, /* access_mask */
1404 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1406 FILE_CREATE, /* create_disposition*/
1407 0, /* create_options */
1408 fattr, /* file_attributes */
1409 NO_OPLOCK, /* oplock_request */
1410 0, /* allocation_size */
1416 if (!NT_STATUS_IS_OK(status)) {
1417 close_file(NULL, fsp1, ERROR_CLOSE);
1421 if (smb_fname_src->st.st_ex_size) {
1422 ret = vfs_transfer_file(fsp1, fsp2, smb_fname_src->st.st_ex_size);
1426 * As we are opening fsp1 read-only we only expect
1427 * an error on close on fsp2 if we are out of space.
1428 * Thus we don't look at the error return from the
1431 close_file(NULL, fsp1, NORMAL_CLOSE);
1433 /* Ensure the modtime is set correctly on the destination file. */
1434 set_close_write_time(fsp2, smb_fname_src->st.st_ex_mtime);
1436 status = close_file(NULL, fsp2, NORMAL_CLOSE);
1438 /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
1439 creates the file. This isn't the correct thing to do in the copy
1441 if (!parent_dirname(talloc_tos(), smb_fname_dst->base_name, &parent,
1443 status = NT_STATUS_NO_MEMORY;
1446 file_set_dosmode(conn, smb_fname_dst, fattr, parent, false);
1447 TALLOC_FREE(parent);
1449 if (ret < (SMB_OFF_T)smb_fname_src->st.st_ex_size) {
1450 status = NT_STATUS_DISK_FULL;
1454 if (!NT_STATUS_IS_OK(status)) {
1455 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1456 nt_errstr(status), smb_fname_str_dbg(smb_fname_src),
1457 smb_fname_str_dbg(smb_fname_dst)));
1463 /****************************************************************************
1464 Reply to a NT rename request.
1465 ****************************************************************************/
1467 void reply_ntrename(struct smb_request *req)
1469 connection_struct *conn = req->conn;
1470 struct smb_filename *smb_fname_old = NULL;
1471 struct smb_filename *smb_fname_new = NULL;
1472 char *oldname = NULL;
1473 char *newname = NULL;
1476 bool src_has_wcard = False;
1477 bool dest_has_wcard = False;
1479 uint32_t ucf_flags_src = 0;
1480 uint32_t ucf_flags_dst = 0;
1482 TALLOC_CTX *ctx = talloc_tos();
1484 START_PROFILE(SMBntrename);
1487 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1491 attrs = SVAL(req->vwv+0, 0);
1492 rename_type = SVAL(req->vwv+1, 0);
1494 p = (const char *)req->buf + 1;
1495 p += srvstr_get_path_req_wcard(ctx, req, &oldname, p, STR_TERMINATE,
1496 &status, &src_has_wcard);
1497 if (!NT_STATUS_IS_OK(status)) {
1498 reply_nterror(req, status);
1502 if (ms_has_wild(oldname)) {
1503 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1508 p += srvstr_get_path_req_wcard(ctx, req, &newname, p, STR_TERMINATE,
1509 &status, &dest_has_wcard);
1510 if (!NT_STATUS_IS_OK(status)) {
1511 reply_nterror(req, status);
1515 /* The newname must begin with a ':' if the oldname contains a ':'. */
1516 if (strchr_m(oldname, ':') && (newname[0] != ':')) {
1517 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1522 * If this is a rename operation, allow wildcards and save the
1523 * destination's last component.
1525 if (rename_type == RENAME_FLAG_RENAME) {
1526 ucf_flags_src = UCF_COND_ALLOW_WCARD_LCOMP;
1527 ucf_flags_dst = UCF_COND_ALLOW_WCARD_LCOMP | UCF_SAVE_LCOMP;
1530 /* rename_internals() calls unix_convert(), so don't call it here. */
1531 status = filename_convert(ctx, conn,
1532 req->flags2 & FLAGS2_DFS_PATHNAMES,
1537 if (!NT_STATUS_IS_OK(status)) {
1538 if (NT_STATUS_EQUAL(status,
1539 NT_STATUS_PATH_NOT_COVERED)) {
1540 reply_botherror(req,
1541 NT_STATUS_PATH_NOT_COVERED,
1542 ERRSRV, ERRbadpath);
1545 reply_nterror(req, status);
1549 status = filename_convert(ctx, conn,
1550 req->flags2 & FLAGS2_DFS_PATHNAMES,
1555 if (!NT_STATUS_IS_OK(status)) {
1556 if (NT_STATUS_EQUAL(status,
1557 NT_STATUS_PATH_NOT_COVERED)) {
1558 reply_botherror(req,
1559 NT_STATUS_PATH_NOT_COVERED,
1560 ERRSRV, ERRbadpath);
1563 reply_nterror(req, status);
1567 DEBUG(3,("reply_ntrename: %s -> %s\n",
1568 smb_fname_str_dbg(smb_fname_old),
1569 smb_fname_str_dbg(smb_fname_new)));
1571 switch(rename_type) {
1572 case RENAME_FLAG_RENAME:
1573 status = rename_internals(ctx, conn, req,
1574 smb_fname_old, smb_fname_new,
1575 attrs, False, src_has_wcard,
1579 case RENAME_FLAG_HARD_LINK:
1580 if (src_has_wcard || dest_has_wcard) {
1582 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1584 status = hardlink_internals(ctx, conn,
1589 case RENAME_FLAG_COPY:
1590 if (src_has_wcard || dest_has_wcard) {
1592 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1594 status = copy_internals(ctx, conn, req,
1600 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1601 status = NT_STATUS_INVALID_PARAMETER;
1604 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1608 if (!NT_STATUS_IS_OK(status)) {
1609 if (open_was_deferred(req->mid)) {
1610 /* We have re-scheduled this call. */
1614 reply_nterror(req, status);
1618 reply_outbuf(req, 0, 0);
1620 END_PROFILE(SMBntrename);
1624 /****************************************************************************
1625 Reply to a notify change - queue the request and
1626 don't allow a directory to be opened.
1627 ****************************************************************************/
1629 static void smbd_smb1_notify_reply(struct smb_request *req,
1630 NTSTATUS error_code,
1631 uint8_t *buf, size_t len)
1633 send_nt_replies(req->conn, req, error_code, (char *)buf, len, NULL, 0);
1636 static void call_nt_transact_notify_change(connection_struct *conn,
1637 struct smb_request *req,
1641 uint32 parameter_count,
1642 char **ppdata, uint32 data_count,
1643 uint32 max_data_count,
1644 uint32 max_param_count)
1646 uint16 *setup = *ppsetup;
1652 if(setup_count < 6) {
1653 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1657 fsp = file_fsp(req, SVAL(setup,4));
1658 filter = IVAL(setup, 0);
1659 recursive = (SVAL(setup, 6) != 0) ? True : False;
1661 DEBUG(3,("call_nt_transact_notify_change\n"));
1664 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1669 char *filter_string;
1671 if (!(filter_string = notify_filter_string(NULL, filter))) {
1672 reply_nterror(req,NT_STATUS_NO_MEMORY);
1676 DEBUG(3,("call_nt_transact_notify_change: notify change "
1677 "called on %s, filter = %s, recursive = %d\n",
1678 fsp_str_dbg(fsp), filter_string, recursive));
1680 TALLOC_FREE(filter_string);
1683 if((!fsp->is_directory) || (conn != fsp->conn)) {
1684 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1688 if (fsp->notify == NULL) {
1690 status = change_notify_create(fsp, filter, recursive);
1692 if (!NT_STATUS_IS_OK(status)) {
1693 DEBUG(10, ("change_notify_create returned %s\n",
1694 nt_errstr(status)));
1695 reply_nterror(req, status);
1700 if (fsp->notify->num_changes != 0) {
1703 * We've got changes pending, respond immediately
1707 * TODO: write a torture test to check the filtering behaviour
1711 change_notify_reply(fsp->conn, req,
1715 smbd_smb1_notify_reply);
1718 * change_notify_reply() above has independently sent its
1725 * No changes pending, queue the request
1728 status = change_notify_add_request(req,
1732 smbd_smb1_notify_reply);
1733 if (!NT_STATUS_IS_OK(status)) {
1734 reply_nterror(req, status);
1739 /****************************************************************************
1740 Reply to an NT transact rename command.
1741 ****************************************************************************/
1743 static void call_nt_transact_rename(connection_struct *conn,
1744 struct smb_request *req,
1745 uint16 **ppsetup, uint32 setup_count,
1746 char **ppparams, uint32 parameter_count,
1747 char **ppdata, uint32 data_count,
1748 uint32 max_data_count)
1750 char *params = *ppparams;
1751 char *new_name = NULL;
1752 files_struct *fsp = NULL;
1753 bool dest_has_wcard = False;
1755 TALLOC_CTX *ctx = talloc_tos();
1757 if(parameter_count < 5) {
1758 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1762 fsp = file_fsp(req, SVAL(params, 0));
1763 if (!check_fsp(conn, req, fsp)) {
1766 srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
1767 parameter_count - 4,
1768 STR_TERMINATE, &status, &dest_has_wcard);
1769 if (!NT_STATUS_IS_OK(status)) {
1770 reply_nterror(req, status);
1775 * W2K3 ignores this request as the RAW-RENAME test
1776 * demonstrates, so we do.
1778 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1780 DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1781 fsp_str_dbg(fsp), new_name));
1786 /******************************************************************************
1787 Fake up a completely empty SD.
1788 *******************************************************************************/
1790 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
1794 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1796 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1797 return NT_STATUS_NO_MEMORY;
1800 return NT_STATUS_OK;
1803 /****************************************************************************
1804 Reply to query a security descriptor.
1805 ****************************************************************************/
1807 static void call_nt_transact_query_security_desc(connection_struct *conn,
1808 struct smb_request *req,
1812 uint32 parameter_count,
1815 uint32 max_data_count)
1817 char *params = *ppparams;
1818 char *data = *ppdata;
1819 SEC_DESC *psd = NULL;
1821 uint32 security_info_wanted;
1822 files_struct *fsp = NULL;
1826 if(parameter_count < 8) {
1827 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1831 fsp = file_fsp(req, SVAL(params,0));
1833 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1837 security_info_wanted = IVAL(params,4);
1839 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, "
1840 "info_wanted = 0x%x\n", fsp_str_dbg(fsp),
1841 (unsigned int)security_info_wanted));
1843 params = nttrans_realloc(ppparams, 4);
1844 if(params == NULL) {
1845 reply_nterror(req, NT_STATUS_NO_MEMORY);
1850 * Get the permissions to return.
1853 if ((security_info_wanted & SECINFO_SACL) &&
1854 !(fsp->access_mask & SEC_FLAG_SYSTEM_SECURITY)) {
1855 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1859 if ((security_info_wanted & (SECINFO_DACL|SECINFO_OWNER|SECINFO_GROUP)) &&
1860 !(fsp->access_mask & SEC_STD_READ_CONTROL)) {
1861 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1865 if (security_info_wanted & (SECINFO_DACL|SECINFO_OWNER|
1866 SECINFO_GROUP|SECINFO_SACL)) {
1867 /* Don't return SECINFO_LABEL if anything else was
1868 requested. See bug #8458. */
1869 security_info_wanted &= ~SECINFO_LABEL;
1872 if (!lp_nt_acl_support(SNUM(conn))) {
1873 status = get_null_nt_acl(talloc_tos(), &psd);
1874 } else if (security_info_wanted & SECINFO_LABEL) {
1875 /* Like W2K3 return a null object. */
1876 status = get_null_nt_acl(talloc_tos(), &psd);
1878 status = SMB_VFS_FGET_NT_ACL(
1879 fsp, security_info_wanted, &psd);
1881 if (!NT_STATUS_IS_OK(status)) {
1882 reply_nterror(req, status);
1886 if (!(security_info_wanted & SECINFO_OWNER)) {
1887 psd->owner_sid = NULL;
1889 if (!(security_info_wanted & SECINFO_GROUP)) {
1890 psd->group_sid = NULL;
1892 if (!(security_info_wanted & SECINFO_DACL)) {
1893 psd->type &= ~SEC_DESC_DACL_PRESENT;
1896 if (!(security_info_wanted & SECINFO_SACL)) {
1897 psd->type &= ~SEC_DESC_SACL_PRESENT;
1901 /* If the SACL/DACL is NULL, but was requested, we mark that it is
1902 * present in the reply to match Windows behavior */
1903 if (psd->sacl == NULL &&
1904 security_info_wanted & SACL_SECURITY_INFORMATION)
1905 psd->type |= SEC_DESC_SACL_PRESENT;
1906 if (psd->dacl == NULL &&
1907 security_info_wanted & DACL_SECURITY_INFORMATION)
1908 psd->type |= SEC_DESC_DACL_PRESENT;
1910 if (security_info_wanted & SECINFO_LABEL) {
1911 /* Like W2K3 return a null object. */
1912 psd->owner_sid = NULL;
1913 psd->group_sid = NULL;
1916 psd->type &= ~(SEC_DESC_DACL_PRESENT|SEC_DESC_SACL_PRESENT);
1919 sd_size = ndr_size_security_descriptor(psd, NULL, 0);
1921 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
1923 if (DEBUGLEVEL >= 10) {
1924 DEBUG(10,("call_nt_transact_query_security_desc for file %s\n",
1926 NDR_PRINT_DEBUG(security_descriptor, psd);
1929 SIVAL(params,0,(uint32)sd_size);
1931 if (max_data_count < sd_size) {
1932 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
1933 params, 4, *ppdata, 0);
1938 * Allocate the data we will point this at.
1941 data = nttrans_realloc(ppdata, sd_size);
1943 reply_nterror(req, NT_STATUS_NO_MEMORY);
1947 status = marshall_sec_desc(talloc_tos(), psd,
1948 &blob.data, &blob.length);
1950 if (!NT_STATUS_IS_OK(status)) {
1951 reply_nterror(req, status);
1955 SMB_ASSERT(sd_size == blob.length);
1956 memcpy(data, blob.data, sd_size);
1958 send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
1963 /****************************************************************************
1964 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
1965 ****************************************************************************/
1967 static void call_nt_transact_set_security_desc(connection_struct *conn,
1968 struct smb_request *req,
1972 uint32 parameter_count,
1975 uint32 max_data_count)
1977 char *params= *ppparams;
1978 char *data = *ppdata;
1979 files_struct *fsp = NULL;
1980 uint32 security_info_sent = 0;
1983 if(parameter_count < 8) {
1984 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1988 if((fsp = file_fsp(req, SVAL(params,0))) == NULL) {
1989 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1993 if(!lp_nt_acl_support(SNUM(conn))) {
1997 security_info_sent = IVAL(params,4);
1999 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n",
2000 fsp_str_dbg(fsp), (unsigned int)security_info_sent));
2002 if (data_count == 0) {
2003 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2007 status = set_sd(fsp, (uint8 *)data, data_count, security_info_sent);
2009 if (!NT_STATUS_IS_OK(status)) {
2010 reply_nterror(req, status);
2015 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2019 /****************************************************************************
2021 ****************************************************************************/
2023 static void call_nt_transact_ioctl(connection_struct *conn,
2024 struct smb_request *req,
2025 uint16 **ppsetup, uint32 setup_count,
2026 char **ppparams, uint32 parameter_count,
2027 char **ppdata, uint32 data_count,
2028 uint32 max_data_count)
2035 char *pdata = *ppdata;
2037 if (setup_count != 8) {
2038 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
2039 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2043 function = IVAL(*ppsetup, 0);
2044 fidnum = SVAL(*ppsetup, 4);
2045 isFSctl = CVAL(*ppsetup, 6);
2046 compfilter = CVAL(*ppsetup, 7);
2048 DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
2049 function, fidnum, isFSctl, compfilter));
2051 fsp=file_fsp(req, fidnum);
2052 /* this check is done in each implemented function case for now
2053 because I don't want to break anything... --metze
2054 FSP_BELONGS_CONN(fsp,conn);*/
2056 SMB_PERFCOUNT_SET_IOCTL(&req->pcd, function);
2059 case FSCTL_SET_SPARSE:
2060 /* pretend this succeeded - tho strictly we should
2061 mark the file sparse (if the local fs supports it)
2062 so we can know if we need to pre-allocate or not */
2064 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
2065 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2068 case FSCTL_CREATE_OR_GET_OBJECT_ID:
2070 unsigned char objid[16];
2072 /* This should return the object-id on this file.
2073 * I think I'll make this be the inode+dev. JRA.
2076 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
2078 if (!fsp_belongs_conn(conn, req, fsp)) {
2083 pdata = nttrans_realloc(ppdata, data_count);
2084 if (pdata == NULL) {
2085 reply_nterror(req, NT_STATUS_NO_MEMORY);
2089 /* For backwards compatibility only store the dev/inode. */
2090 push_file_id_16(pdata, &fsp->file_id);
2091 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
2092 push_file_id_16(pdata+32, &fsp->file_id);
2093 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
2098 case FSCTL_GET_REPARSE_POINT:
2099 /* pretend this fail - my winXP does it like this
2103 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2104 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
2107 case FSCTL_SET_REPARSE_POINT:
2108 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
2112 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2113 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
2116 case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
2119 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
2120 * and return their volume names. If max_data_count is 16, then it is just
2121 * asking for the number of volumes and length of the combined names.
2123 * pdata is the data allocated by our caller, but that uses
2124 * total_data_count (which is 0 in our case) rather than max_data_count.
2125 * Allocate the correct amount and return the pointer to let
2126 * it be deallocated when we return.
2128 SHADOW_COPY_DATA *shadow_data = NULL;
2129 TALLOC_CTX *shadow_mem_ctx = NULL;
2130 bool labels = False;
2131 uint32 labels_data_count = 0;
2135 if (!fsp_belongs_conn(conn, req, fsp)) {
2139 if (max_data_count < 16) {
2140 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
2142 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2146 if (max_data_count > 16) {
2150 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
2151 if (shadow_mem_ctx == NULL) {
2152 DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
2153 reply_nterror(req, NT_STATUS_NO_MEMORY);
2157 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
2158 if (shadow_data == NULL) {
2159 DEBUG(0,("TALLOC_ZERO() failed!\n"));
2160 talloc_destroy(shadow_mem_ctx);
2161 reply_nterror(req, NT_STATUS_NO_MEMORY);
2165 shadow_data->mem_ctx = shadow_mem_ctx;
2168 * Call the VFS routine to actually do the work.
2170 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
2171 talloc_destroy(shadow_data->mem_ctx);
2172 if (errno == ENOSYS) {
2173 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
2174 conn->connectpath));
2175 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2178 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
2179 conn->connectpath));
2180 reply_nterror(req, NT_STATUS_UNSUCCESSFUL);
2185 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
2190 data_count = 12+labels_data_count+4;
2193 if (max_data_count<data_count) {
2194 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
2195 max_data_count,data_count));
2196 talloc_destroy(shadow_data->mem_ctx);
2197 reply_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
2201 pdata = nttrans_realloc(ppdata, data_count);
2202 if (pdata == NULL) {
2203 talloc_destroy(shadow_data->mem_ctx);
2204 reply_nterror(req, NT_STATUS_NO_MEMORY);
2210 /* num_volumes 4 bytes */
2211 SIVAL(pdata,0,shadow_data->num_volumes);
2214 /* num_labels 4 bytes */
2215 SIVAL(pdata,4,shadow_data->num_volumes);
2218 /* needed_data_count 4 bytes */
2219 SIVAL(pdata, 8, labels_data_count+4);
2223 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
2224 shadow_data->num_volumes, fsp_str_dbg(fsp)));
2225 if (labels && shadow_data->labels) {
2226 for (i=0;i<shadow_data->num_volumes;i++) {
2227 srvstr_push(pdata, req->flags2,
2228 cur_pdata, shadow_data->labels[i],
2229 2*sizeof(SHADOW_COPY_LABEL),
2230 STR_UNICODE|STR_TERMINATE);
2231 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
2232 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
2236 talloc_destroy(shadow_data->mem_ctx);
2238 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
2244 case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
2246 /* pretend this succeeded -
2248 * we have to send back a list with all files owned by this SID
2250 * but I have to check that --metze
2254 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
2256 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
2258 if (!fsp_belongs_conn(conn, req, fsp)) {
2262 /* unknown 4 bytes: this is not the length of the sid :-( */
2263 /*unknown = IVAL(pdata,0);*/
2265 if (!sid_parse(pdata+4,sid_len,&sid)) {
2266 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2270 DEBUGADD(10, ("for SID: %s\n", sid_string_dbg(&sid)));
2272 if (!sid_to_uid(&sid, &uid)) {
2273 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2274 sid_string_dbg(&sid),
2275 (unsigned long)sid_len));
2279 /* we can take a look at the find source :-)
2281 * find ./ -uid $uid -name '*' is what we need here
2284 * and send 4bytes len and then NULL terminated unicode strings
2287 * but I don't know how to deal with the paged results
2288 * (maybe we can hang the result anywhere in the fsp struct)
2290 * we don't send all files at once
2291 * and at the next we should *not* start from the beginning,
2292 * so we have to cache the result
2297 /* this works for now... */
2298 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2302 if (!logged_ioctl_message) {
2303 logged_ioctl_message = true; /* Only print this once... */
2304 DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
2309 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2313 #ifdef HAVE_SYS_QUOTAS
2314 /****************************************************************************
2315 Reply to get user quota
2316 ****************************************************************************/
2318 static void call_nt_transact_get_user_quota(connection_struct *conn,
2319 struct smb_request *req,
2323 uint32 parameter_count,
2326 uint32 max_data_count)
2328 NTSTATUS nt_status = NT_STATUS_OK;
2329 char *params = *ppparams;
2330 char *pdata = *ppdata;
2332 int data_len=0,param_len=0;
2335 files_struct *fsp = NULL;
2339 bool start_enum = True;
2340 SMB_NTQUOTA_STRUCT qt;
2341 SMB_NTQUOTA_LIST *tmp_list;
2342 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2347 if (conn->server_info->utok.uid != 0 && !conn->admin_user) {
2348 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2349 "[%s]\n", lp_servicename(SNUM(conn)),
2350 conn->server_info->unix_name));
2351 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2356 * Ensure minimum number of parameters sent.
2359 if (parameter_count < 4) {
2360 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2361 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2365 /* maybe we can check the quota_fnum */
2366 fsp = file_fsp(req, SVAL(params,0));
2367 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2368 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2369 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2373 /* the NULL pointer checking for fsp->fake_file_handle->pd
2374 * is done by CHECK_NTQUOTA_HANDLE_OK()
2376 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->private_data;
2378 level = SVAL(params,2);
2380 /* unknown 12 bytes leading in params */
2383 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2384 /* seems that we should continue with the enum here --metze */
2386 if (qt_handle->quota_list!=NULL &&
2387 qt_handle->tmp_list==NULL) {
2390 free_ntquota_list(&(qt_handle->quota_list));
2392 /* Realloc the size of parameters and data we will return */
2394 params = nttrans_realloc(ppparams, param_len);
2395 if(params == NULL) {
2396 reply_nterror(req, NT_STATUS_NO_MEMORY);
2401 SIVAL(params,0,data_len);
2408 case TRANSACT_GET_USER_QUOTA_LIST_START:
2410 if (qt_handle->quota_list==NULL &&
2411 qt_handle->tmp_list==NULL) {
2415 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2416 reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
2420 /* Realloc the size of parameters and data we will return */
2422 params = nttrans_realloc(ppparams, param_len);
2423 if(params == NULL) {
2424 reply_nterror(req, NT_STATUS_NO_MEMORY);
2428 /* we should not trust the value in max_data_count*/
2429 max_data_count = MIN(max_data_count,2048);
2431 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2433 reply_nterror(req, NT_STATUS_NO_MEMORY);
2439 /* set params Size of returned Quota Data 4 bytes*/
2440 /* but set it later when we know it */
2442 /* for each entry push the data */
2445 qt_handle->tmp_list = qt_handle->quota_list;
2448 tmp_list = qt_handle->tmp_list;
2450 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2451 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2453 sid_len = ndr_size_dom_sid(
2454 &tmp_list->quotas->sid, NULL, 0);
2455 entry_len = 40 + sid_len;
2457 /* nextoffset entry 4 bytes */
2458 SIVAL(entry,0,entry_len);
2460 /* then the len of the SID 4 bytes */
2461 SIVAL(entry,4,sid_len);
2463 /* unknown data 8 bytes uint64_t */
2464 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-metze*/
2466 /* the used disk space 8 bytes uint64_t */
2467 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2469 /* the soft quotas 8 bytes uint64_t */
2470 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2472 /* the hard quotas 8 bytes uint64_t */
2473 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2475 /* and now the SID */
2476 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2479 qt_handle->tmp_list = tmp_list;
2481 /* overwrite the offset of the last entry */
2482 SIVAL(entry-entry_len,0,0);
2484 data_len = 4+qt_len;
2485 /* overwrite the params quota_data_len */
2486 SIVAL(params,0,data_len);
2490 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2492 /* unknown 4 bytes IVAL(pdata,0) */
2494 if (data_count < 8) {
2495 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2496 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2500 sid_len = IVAL(pdata,4);
2501 /* Ensure this is less than 1mb. */
2502 if (sid_len > (1024*1024)) {
2503 reply_nterror(req, NT_STATUS_NO_MEMORY);
2507 if (data_count < 8+sid_len) {
2508 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2509 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2513 data_len = 4+40+sid_len;
2515 if (max_data_count < data_len) {
2516 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2517 max_data_count, data_len));
2519 SIVAL(params,0,data_len);
2521 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2525 if (!sid_parse(pdata+8,sid_len,&sid)) {
2526 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2530 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2533 * we have to return zero's in all fields
2534 * instead of returning an error here
2539 /* Realloc the size of parameters and data we will return */
2541 params = nttrans_realloc(ppparams, param_len);
2542 if(params == NULL) {
2543 reply_nterror(req, NT_STATUS_NO_MEMORY);
2547 pdata = nttrans_realloc(ppdata, data_len);
2549 reply_nterror(req, NT_STATUS_NO_MEMORY);
2555 /* set params Size of returned Quota Data 4 bytes*/
2556 SIVAL(params,0,data_len);
2558 /* nextoffset entry 4 bytes */
2561 /* then the len of the SID 4 bytes */
2562 SIVAL(entry,4,sid_len);
2564 /* unknown data 8 bytes uint64_t */
2565 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-mezte*/
2567 /* the used disk space 8 bytes uint64_t */
2568 SBIG_UINT(entry,16,qt.usedspace);
2570 /* the soft quotas 8 bytes uint64_t */
2571 SBIG_UINT(entry,24,qt.softlim);
2573 /* the hard quotas 8 bytes uint64_t */
2574 SBIG_UINT(entry,32,qt.hardlim);
2576 /* and now the SID */
2577 sid_linearize(entry+40, sid_len, &sid);
2582 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2583 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2588 send_nt_replies(conn, req, nt_status, params, param_len,
2592 /****************************************************************************
2593 Reply to set user quota
2594 ****************************************************************************/
2596 static void call_nt_transact_set_user_quota(connection_struct *conn,
2597 struct smb_request *req,
2601 uint32 parameter_count,
2604 uint32 max_data_count)
2606 char *params = *ppparams;
2607 char *pdata = *ppdata;
2608 int data_len=0,param_len=0;
2609 SMB_NTQUOTA_STRUCT qt;
2612 files_struct *fsp = NULL;
2617 if (conn->server_info->utok.uid != 0 && !conn->admin_user) {
2618 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2619 "[%s]\n", lp_servicename(SNUM(conn)),
2620 conn->server_info->unix_name));
2621 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2626 * Ensure minimum number of parameters sent.
2629 if (parameter_count < 2) {
2630 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2631 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2635 /* maybe we can check the quota_fnum */
2636 fsp = file_fsp(req, SVAL(params,0));
2637 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2638 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2639 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2643 if (data_count < 40) {
2644 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2645 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2649 /* offset to next quota record.
2650 * 4 bytes IVAL(pdata,0)
2655 sid_len = IVAL(pdata,4);
2657 if (data_count < 40+sid_len) {
2658 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2659 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2663 /* unknown 8 bytes in pdata
2664 * maybe its the change time in NTTIME
2667 /* the used space 8 bytes (uint64_t)*/
2668 qt.usedspace = (uint64_t)IVAL(pdata,16);
2669 #ifdef LARGE_SMB_OFF_T
2670 qt.usedspace |= (((uint64_t)IVAL(pdata,20)) << 32);
2671 #else /* LARGE_SMB_OFF_T */
2672 if ((IVAL(pdata,20) != 0)&&
2673 ((qt.usedspace != 0xFFFFFFFF)||
2674 (IVAL(pdata,20)!=0xFFFFFFFF))) {
2675 /* more than 32 bits? */
2676 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2679 #endif /* LARGE_SMB_OFF_T */
2681 /* the soft quotas 8 bytes (uint64_t)*/
2682 qt.softlim = (uint64_t)IVAL(pdata,24);
2683 #ifdef LARGE_SMB_OFF_T
2684 qt.softlim |= (((uint64_t)IVAL(pdata,28)) << 32);
2685 #else /* LARGE_SMB_OFF_T */
2686 if ((IVAL(pdata,28) != 0)&&
2687 ((qt.softlim != 0xFFFFFFFF)||
2688 (IVAL(pdata,28)!=0xFFFFFFFF))) {
2689 /* more than 32 bits? */
2690 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2693 #endif /* LARGE_SMB_OFF_T */
2695 /* the hard quotas 8 bytes (uint64_t)*/
2696 qt.hardlim = (uint64_t)IVAL(pdata,32);
2697 #ifdef LARGE_SMB_OFF_T
2698 qt.hardlim |= (((uint64_t)IVAL(pdata,36)) << 32);
2699 #else /* LARGE_SMB_OFF_T */
2700 if ((IVAL(pdata,36) != 0)&&
2701 ((qt.hardlim != 0xFFFFFFFF)||
2702 (IVAL(pdata,36)!=0xFFFFFFFF))) {
2703 /* more than 32 bits? */
2704 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2707 #endif /* LARGE_SMB_OFF_T */
2709 if (!sid_parse(pdata+40,sid_len,&sid)) {
2710 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2714 DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid)));
2716 /* 44 unknown bytes left... */
2718 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2719 reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
2723 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2726 #endif /* HAVE_SYS_QUOTAS */
2728 static void handle_nttrans(connection_struct *conn,
2729 struct trans_state *state,
2730 struct smb_request *req)
2732 if (get_Protocol() >= PROTOCOL_NT1) {
2733 req->flags2 |= 0x40; /* IS_LONG_NAME */
2734 SSVAL(req->inbuf,smb_flg2,req->flags2);
2738 SMB_PERFCOUNT_SET_SUBOP(&req->pcd, state->call);
2740 /* Now we must call the relevant NT_TRANS function */
2741 switch(state->call) {
2742 case NT_TRANSACT_CREATE:
2744 START_PROFILE(NT_transact_create);
2745 call_nt_transact_create(
2747 &state->setup, state->setup_count,
2748 &state->param, state->total_param,
2749 &state->data, state->total_data,
2750 state->max_data_return);
2751 END_PROFILE(NT_transact_create);
2755 case NT_TRANSACT_IOCTL:
2757 START_PROFILE(NT_transact_ioctl);
2758 call_nt_transact_ioctl(
2760 &state->setup, state->setup_count,
2761 &state->param, state->total_param,
2762 &state->data, state->total_data,
2763 state->max_data_return);
2764 END_PROFILE(NT_transact_ioctl);
2768 case NT_TRANSACT_SET_SECURITY_DESC:
2770 START_PROFILE(NT_transact_set_security_desc);
2771 call_nt_transact_set_security_desc(
2773 &state->setup, state->setup_count,
2774 &state->param, state->total_param,
2775 &state->data, state->total_data,
2776 state->max_data_return);
2777 END_PROFILE(NT_transact_set_security_desc);
2781 case NT_TRANSACT_NOTIFY_CHANGE:
2783 START_PROFILE(NT_transact_notify_change);
2784 call_nt_transact_notify_change(
2786 &state->setup, state->setup_count,
2787 &state->param, state->total_param,
2788 &state->data, state->total_data,
2789 state->max_data_return,
2790 state->max_param_return);
2791 END_PROFILE(NT_transact_notify_change);
2795 case NT_TRANSACT_RENAME:
2797 START_PROFILE(NT_transact_rename);
2798 call_nt_transact_rename(
2800 &state->setup, state->setup_count,
2801 &state->param, state->total_param,
2802 &state->data, state->total_data,
2803 state->max_data_return);
2804 END_PROFILE(NT_transact_rename);
2808 case NT_TRANSACT_QUERY_SECURITY_DESC:
2810 START_PROFILE(NT_transact_query_security_desc);
2811 call_nt_transact_query_security_desc(
2813 &state->setup, state->setup_count,
2814 &state->param, state->total_param,
2815 &state->data, state->total_data,
2816 state->max_data_return);
2817 END_PROFILE(NT_transact_query_security_desc);
2821 #ifdef HAVE_SYS_QUOTAS
2822 case NT_TRANSACT_GET_USER_QUOTA:
2824 START_PROFILE(NT_transact_get_user_quota);
2825 call_nt_transact_get_user_quota(
2827 &state->setup, state->setup_count,
2828 &state->param, state->total_param,
2829 &state->data, state->total_data,
2830 state->max_data_return);
2831 END_PROFILE(NT_transact_get_user_quota);
2835 case NT_TRANSACT_SET_USER_QUOTA:
2837 START_PROFILE(NT_transact_set_user_quota);
2838 call_nt_transact_set_user_quota(
2840 &state->setup, state->setup_count,
2841 &state->param, state->total_param,
2842 &state->data, state->total_data,
2843 state->max_data_return);
2844 END_PROFILE(NT_transact_set_user_quota);
2847 #endif /* HAVE_SYS_QUOTAS */
2850 /* Error in request */
2851 DEBUG(0,("handle_nttrans: Unknown request %d in "
2852 "nttrans call\n", state->call));
2853 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2859 /****************************************************************************
2860 Reply to a SMBNTtrans.
2861 ****************************************************************************/
2863 void reply_nttrans(struct smb_request *req)
2865 connection_struct *conn = req->conn;
2870 uint16 function_code;
2872 struct trans_state *state;
2874 START_PROFILE(SMBnttrans);
2876 if (req->wct < 19) {
2877 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2878 END_PROFILE(SMBnttrans);
2882 pscnt = IVAL(req->vwv+9, 1);
2883 psoff = IVAL(req->vwv+11, 1);
2884 dscnt = IVAL(req->vwv+13, 1);
2885 dsoff = IVAL(req->vwv+15, 1);
2886 function_code = SVAL(req->vwv+18, 0);
2888 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2889 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2890 END_PROFILE(SMBnttrans);
2894 result = allow_new_trans(conn->pending_trans, req->mid);
2895 if (!NT_STATUS_IS_OK(result)) {
2896 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
2897 reply_nterror(req, result);
2898 END_PROFILE(SMBnttrans);
2902 if ((state = TALLOC_P(conn, struct trans_state)) == NULL) {
2903 reply_nterror(req, NT_STATUS_NO_MEMORY);
2904 END_PROFILE(SMBnttrans);
2908 state->cmd = SMBnttrans;
2910 state->mid = req->mid;
2911 state->vuid = req->vuid;
2912 state->total_data = IVAL(req->vwv+3, 1);
2914 state->total_param = IVAL(req->vwv+1, 1);
2915 state->param = NULL;
2916 state->max_data_return = IVAL(req->vwv+7, 1);
2917 state->max_param_return = IVAL(req->vwv+5, 1);
2919 /* setup count is in *words* */
2920 state->setup_count = 2*CVAL(req->vwv+17, 1);
2921 state->setup = NULL;
2922 state->call = function_code;
2924 DEBUG(10, ("num_setup=%u, "
2925 "param_total=%u, this_param=%u, max_param=%u, "
2926 "data_total=%u, this_data=%u, max_data=%u, "
2927 "param_offset=%u, data_offset=%u\n",
2928 (unsigned)state->setup_count,
2929 (unsigned)state->total_param, (unsigned)pscnt,
2930 (unsigned)state->max_param_return,
2931 (unsigned)state->total_data, (unsigned)dscnt,
2932 (unsigned)state->max_data_return,
2933 (unsigned)psoff, (unsigned)dsoff));
2936 * All nttrans messages we handle have smb_wct == 19 +
2937 * state->setup_count. Ensure this is so as a sanity check.
2940 if(req->wct != 19 + (state->setup_count/2)) {
2941 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2942 req->wct, 19 + (state->setup_count/2)));
2946 /* Don't allow more than 128mb for each value. */
2947 if ((state->total_data > (1024*1024*128)) ||
2948 (state->total_param > (1024*1024*128))) {
2949 reply_nterror(req, NT_STATUS_NO_MEMORY);
2950 END_PROFILE(SMBnttrans);
2954 if ((dscnt > state->total_data) || (pscnt > state->total_param))
2957 if (state->total_data) {
2959 if (trans_oob(state->total_data, 0, dscnt)
2960 || trans_oob(smb_len(req->inbuf), dsoff, dscnt)) {
2964 /* Can't use talloc here, the core routines do realloc on the
2965 * params and data. */
2966 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
2967 DEBUG(0,("reply_nttrans: data malloc fail for %u "
2968 "bytes !\n", (unsigned int)state->total_data));
2970 reply_nterror(req, NT_STATUS_NO_MEMORY);
2971 END_PROFILE(SMBnttrans);
2975 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
2978 if (state->total_param) {
2980 if (trans_oob(state->total_param, 0, pscnt)
2981 || trans_oob(smb_len(req->inbuf), psoff, pscnt)) {
2985 /* Can't use talloc here, the core routines do realloc on the
2986 * params and data. */
2987 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
2988 DEBUG(0,("reply_nttrans: param malloc fail for %u "
2989 "bytes !\n", (unsigned int)state->total_param));
2990 SAFE_FREE(state->data);
2992 reply_nterror(req, NT_STATUS_NO_MEMORY);
2993 END_PROFILE(SMBnttrans);
2997 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
3000 state->received_data = dscnt;
3001 state->received_param = pscnt;
3003 if(state->setup_count > 0) {
3004 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
3005 state->setup_count));
3008 * No overflow possible here, state->setup_count is an
3009 * unsigned int, being filled by a single byte from
3010 * CVAL(req->vwv+13, 0) above. The cast in the comparison
3011 * below is not necessary, it's here to clarify things. The
3012 * validity of req->vwv and req->wct has been checked in
3013 * init_smb_request already.
3015 if ((state->setup_count/2) + 19 > (unsigned int)req->wct) {
3019 state->setup = (uint16 *)TALLOC(state, state->setup_count);
3020 if (state->setup == NULL) {
3021 DEBUG(0,("reply_nttrans : Out of memory\n"));
3022 SAFE_FREE(state->data);
3023 SAFE_FREE(state->param);
3025 reply_nterror(req, NT_STATUS_NO_MEMORY);
3026 END_PROFILE(SMBnttrans);
3030 memcpy(state->setup, req->vwv+19, state->setup_count);
3031 dump_data(10, (uint8 *)state->setup, state->setup_count);
3034 if ((state->received_data == state->total_data) &&
3035 (state->received_param == state->total_param)) {
3036 handle_nttrans(conn, state, req);
3037 SAFE_FREE(state->param);
3038 SAFE_FREE(state->data);
3040 END_PROFILE(SMBnttrans);
3044 DLIST_ADD(conn->pending_trans, state);
3046 /* We need to send an interim response then receive the rest
3047 of the parameter/data bytes */
3048 reply_outbuf(req, 0, 0);
3049 show_msg((char *)req->outbuf);
3050 END_PROFILE(SMBnttrans);
3055 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
3056 SAFE_FREE(state->data);
3057 SAFE_FREE(state->param);
3059 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3060 END_PROFILE(SMBnttrans);
3064 /****************************************************************************
3065 Reply to a SMBnttranss
3066 ****************************************************************************/
3068 void reply_nttranss(struct smb_request *req)
3070 connection_struct *conn = req->conn;
3071 uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
3072 struct trans_state *state;
3074 START_PROFILE(SMBnttranss);
3076 show_msg((char *)req->inbuf);
3078 /* Windows clients expect all replies to
3079 an NT transact secondary (SMBnttranss 0xA1)
3080 to have a command code of NT transact
3081 (SMBnttrans 0xA0). See bug #8989 for details. */
3082 req->cmd = SMBnttrans;
3084 if (req->wct < 18) {
3085 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3086 END_PROFILE(SMBnttranss);
3090 for (state = conn->pending_trans; state != NULL;
3091 state = state->next) {
3092 if (state->mid == req->mid) {
3097 if ((state == NULL) || (state->cmd != SMBnttrans)) {
3098 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3099 END_PROFILE(SMBnttranss);
3103 /* Revise state->total_param and state->total_data in case they have
3104 changed downwards */
3105 if (IVAL(req->vwv+1, 1) < state->total_param) {
3106 state->total_param = IVAL(req->vwv+1, 1);
3108 if (IVAL(req->vwv+3, 1) < state->total_data) {
3109 state->total_data = IVAL(req->vwv+3, 1);
3112 pcnt = IVAL(req->vwv+5, 1);
3113 poff = IVAL(req->vwv+7, 1);
3114 pdisp = IVAL(req->vwv+9, 1);
3116 dcnt = IVAL(req->vwv+11, 1);
3117 doff = IVAL(req->vwv+13, 1);
3118 ddisp = IVAL(req->vwv+15, 1);
3120 state->received_param += pcnt;
3121 state->received_data += dcnt;
3123 if ((state->received_data > state->total_data) ||
3124 (state->received_param > state->total_param))
3128 if (trans_oob(state->total_param, pdisp, pcnt)
3129 || trans_oob(smb_len(req->inbuf), poff, pcnt)) {
3132 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,pcnt);
3136 if (trans_oob(state->total_data, ddisp, dcnt)
3137 || trans_oob(smb_len(req->inbuf), doff, dcnt)) {
3140 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
3143 if ((state->received_param < state->total_param) ||
3144 (state->received_data < state->total_data)) {
3145 END_PROFILE(SMBnttranss);
3149 handle_nttrans(conn, state, req);
3151 DLIST_REMOVE(conn->pending_trans, state);
3152 SAFE_FREE(state->data);
3153 SAFE_FREE(state->param);
3155 END_PROFILE(SMBnttranss);
3160 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
3161 DLIST_REMOVE(conn->pending_trans, state);
3162 SAFE_FREE(state->data);
3163 SAFE_FREE(state->param);
3165 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3166 END_PROFILE(SMBnttranss);