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) {
937 offset += next_offset;
943 /****************************************************************************
944 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
945 ****************************************************************************/
947 static void call_nt_transact_create(connection_struct *conn,
948 struct smb_request *req,
949 uint16 **ppsetup, uint32 setup_count,
950 char **ppparams, uint32 parameter_count,
951 char **ppdata, uint32 data_count,
952 uint32 max_data_count)
954 struct smb_filename *smb_fname = NULL;
956 char *params = *ppparams;
957 char *data = *ppdata;
958 /* Breakout the oplock request bits so we can set the reply bits separately. */
960 SMB_OFF_T file_len = 0;
962 files_struct *fsp = NULL;
966 uint32 file_attributes;
968 uint32 create_disposition;
969 uint32 create_options;
971 struct security_descriptor *sd = NULL;
974 struct timespec create_timespec;
975 struct timespec c_timespec;
976 struct timespec a_timespec;
977 struct timespec m_timespec;
978 struct timespec write_time_ts;
979 struct ea_list *ea_list = NULL;
982 uint64_t allocation_size;
984 uint8_t oplock_granted;
985 struct case_semantics_state *case_state = NULL;
986 TALLOC_CTX *ctx = talloc_tos();
988 DEBUG(5,("call_nt_transact_create\n"));
991 * If it's an IPC, use the pipe handler.
995 if (lp_nt_pipe_support()) {
996 do_nt_transact_create_pipe(
998 ppsetup, setup_count,
999 ppparams, parameter_count,
1000 ppdata, data_count);
1003 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1008 * Ensure minimum number of parameters sent.
1011 if(parameter_count < 54) {
1012 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1013 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1017 flags = IVAL(params,0);
1018 access_mask = IVAL(params,8);
1019 file_attributes = IVAL(params,20);
1020 share_access = IVAL(params,24);
1021 create_disposition = IVAL(params,28);
1022 create_options = IVAL(params,32);
1023 sd_len = IVAL(params,36);
1024 ea_len = IVAL(params,40);
1025 root_dir_fid = (uint16)IVAL(params,4);
1026 allocation_size = (uint64_t)IVAL(params,12);
1027 #ifdef LARGE_SMB_OFF_T
1028 allocation_size |= (((uint64_t)IVAL(params,16)) << 32);
1032 * we need to remove ignored bits when they come directly from the client
1033 * because we reuse some of them for internal stuff
1035 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
1037 /* Ensure the data_len is correct for the sd and ea values given. */
1038 if ((ea_len + sd_len > data_count)
1039 || (ea_len > data_count) || (sd_len > data_count)
1040 || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
1041 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
1042 "%u, data_count = %u\n", (unsigned int)ea_len,
1043 (unsigned int)sd_len, (unsigned int)data_count));
1044 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1049 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
1052 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
1054 if (!NT_STATUS_IS_OK(status)) {
1055 DEBUG(10, ("call_nt_transact_create: "
1056 "unmarshall_sec_desc failed: %s\n",
1057 nt_errstr(status)));
1058 reply_nterror(req, status);
1064 if (!lp_ea_support(SNUM(conn))) {
1065 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
1066 "EA's not supported.\n",
1067 (unsigned int)ea_len));
1068 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
1073 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
1074 "too small (should be more than 10)\n",
1075 (unsigned int)ea_len ));
1076 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1080 /* We have already checked that ea_len <= data_count here. */
1081 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
1083 if (ea_list == NULL) {
1084 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1089 srvstr_get_path(ctx, params, req->flags2, &fname,
1090 params+53, parameter_count-53,
1091 STR_TERMINATE, &status);
1092 if (!NT_STATUS_IS_OK(status)) {
1093 reply_nterror(req, status);
1097 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1098 case_state = set_posix_case_semantics(ctx, conn);
1100 reply_nterror(req, NT_STATUS_NO_MEMORY);
1105 status = filename_convert(ctx,
1107 req->flags2 & FLAGS2_DFS_PATHNAMES,
1113 TALLOC_FREE(case_state);
1115 if (!NT_STATUS_IS_OK(status)) {
1116 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1117 reply_botherror(req,
1118 NT_STATUS_PATH_NOT_COVERED,
1119 ERRSRV, ERRbadpath);
1122 reply_nterror(req, status);
1126 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1127 if (oplock_request) {
1128 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
1133 * Bug #6898 - clients using Windows opens should
1134 * never be able to set this attribute into the
1137 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
1139 status = SMB_VFS_CREATE_FILE(
1142 root_dir_fid, /* root_dir_fid */
1143 smb_fname, /* fname */
1144 access_mask, /* access_mask */
1145 share_access, /* share_access */
1146 create_disposition, /* create_disposition*/
1147 create_options, /* create_options */
1148 file_attributes, /* file_attributes */
1149 oplock_request, /* oplock_request */
1150 allocation_size, /* allocation_size */
1152 ea_list, /* ea_list */
1156 if(!NT_STATUS_IS_OK(status)) {
1157 if (open_was_deferred(req->mid)) {
1158 /* We have re-scheduled this call, no error. */
1161 reply_openerror(req, status);
1165 /* Ensure we're pointing at the correct stat struct. */
1166 TALLOC_FREE(smb_fname);
1167 smb_fname = fsp->fsp_name;
1170 * If the caller set the extended oplock request bit
1171 * and we granted one (by whatever means) - set the
1172 * correct bit for extended oplock reply.
1175 if (oplock_request &&
1176 (lp_fake_oplocks(SNUM(conn))
1177 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
1180 * Exclusive oplock granted
1183 if (flags & REQUEST_BATCH_OPLOCK) {
1184 oplock_granted = BATCH_OPLOCK_RETURN;
1186 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
1188 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1189 oplock_granted = LEVEL_II_OPLOCK_RETURN;
1191 oplock_granted = NO_OPLOCK_RETURN;
1194 file_len = smb_fname->st.st_ex_size;
1196 /* Realloc the size of parameters and data we will return */
1197 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1198 /* Extended response is 32 more byyes. */
1203 params = nttrans_realloc(ppparams, param_len);
1204 if(params == NULL) {
1205 reply_nterror(req, NT_STATUS_NO_MEMORY);
1210 SCVAL(p, 0, oplock_granted);
1213 SSVAL(p,0,fsp->fnum);
1215 if ((create_disposition == FILE_SUPERSEDE)
1216 && (info == FILE_WAS_OVERWRITTEN)) {
1217 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1223 fattr = dos_mode(conn, smb_fname);
1225 fattr = FILE_ATTRIBUTE_NORMAL;
1228 /* Deal with other possible opens having a modified
1230 ZERO_STRUCT(write_time_ts);
1231 get_file_infos(fsp->file_id, NULL, &write_time_ts);
1232 if (!null_timespec(write_time_ts)) {
1233 update_stat_ex_mtime(&smb_fname->st, write_time_ts);
1237 create_timespec = get_create_timespec(conn, fsp, smb_fname);
1238 a_timespec = smb_fname->st.st_ex_atime;
1239 m_timespec = smb_fname->st.st_ex_mtime;
1240 c_timespec = get_change_timespec(conn, fsp, smb_fname);
1242 if (lp_dos_filetime_resolution(SNUM(conn))) {
1243 dos_filetime_timespec(&create_timespec);
1244 dos_filetime_timespec(&a_timespec);
1245 dos_filetime_timespec(&m_timespec);
1246 dos_filetime_timespec(&c_timespec);
1249 put_long_date_timespec(conn->ts_res, p, create_timespec); /* create time. */
1251 put_long_date_timespec(conn->ts_res, p, a_timespec); /* access time */
1253 put_long_date_timespec(conn->ts_res, p, m_timespec); /* write time */
1255 put_long_date_timespec(conn->ts_res, p, c_timespec); /* change time */
1257 SIVAL(p,0,fattr); /* File Attributes. */
1259 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn, fsp, &smb_fname->st));
1261 SOFF_T(p,0,file_len);
1263 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1267 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1269 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1272 if (fsp->is_directory ||
1273 can_write_to_file(conn, smb_fname)) {
1274 perms = FILE_GENERIC_ALL;
1276 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1281 DEBUG(5,("call_nt_transact_create: open name = %s\n",
1282 smb_fname_str_dbg(smb_fname)));
1284 /* Send the required number of replies */
1285 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1290 /****************************************************************************
1291 Reply to a NT CANCEL request.
1292 conn POINTER CAN BE NULL HERE !
1293 ****************************************************************************/
1295 void reply_ntcancel(struct smb_request *req)
1298 * Go through and cancel any pending change notifies.
1301 START_PROFILE(SMBntcancel);
1302 srv_cancel_sign_response(smbd_server_conn);
1303 remove_pending_change_notify_requests_by_mid(req->mid);
1304 remove_pending_lock_requests_by_mid(req->mid);
1306 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", req->mid));
1308 END_PROFILE(SMBntcancel);
1312 /****************************************************************************
1314 ****************************************************************************/
1316 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
1317 connection_struct *conn,
1318 struct smb_request *req,
1319 struct smb_filename *smb_fname_src,
1320 struct smb_filename *smb_fname_dst,
1323 files_struct *fsp1,*fsp2;
1327 NTSTATUS status = NT_STATUS_OK;
1330 if (!CAN_WRITE(conn)) {
1331 status = NT_STATUS_MEDIA_WRITE_PROTECTED;
1335 /* Source must already exist. */
1336 if (!VALID_STAT(smb_fname_src->st)) {
1337 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
1341 /* Ensure attributes match. */
1342 fattr = dos_mode(conn, smb_fname_src);
1343 if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
1344 status = NT_STATUS_NO_SUCH_FILE;
1348 /* Disallow if dst file already exists. */
1349 if (VALID_STAT(smb_fname_dst->st)) {
1350 status = NT_STATUS_OBJECT_NAME_COLLISION;
1354 /* No links from a directory. */
1355 if (S_ISDIR(smb_fname_src->st.st_ex_mode)) {
1356 status = NT_STATUS_FILE_IS_A_DIRECTORY;
1360 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1361 smb_fname_str_dbg(smb_fname_src),
1362 smb_fname_str_dbg(smb_fname_dst)));
1364 status = SMB_VFS_CREATE_FILE(
1367 0, /* root_dir_fid */
1368 smb_fname_src, /* fname */
1369 FILE_READ_DATA, /* access_mask */
1370 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1372 FILE_OPEN, /* create_disposition*/
1373 0, /* create_options */
1374 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
1375 NO_OPLOCK, /* oplock_request */
1376 0, /* allocation_size */
1382 if (!NT_STATUS_IS_OK(status)) {
1386 status = SMB_VFS_CREATE_FILE(
1389 0, /* root_dir_fid */
1390 smb_fname_dst, /* fname */
1391 FILE_WRITE_DATA, /* access_mask */
1392 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1394 FILE_CREATE, /* create_disposition*/
1395 0, /* create_options */
1396 fattr, /* file_attributes */
1397 NO_OPLOCK, /* oplock_request */
1398 0, /* allocation_size */
1404 if (!NT_STATUS_IS_OK(status)) {
1405 close_file(NULL, fsp1, ERROR_CLOSE);
1409 if (smb_fname_src->st.st_ex_size) {
1410 ret = vfs_transfer_file(fsp1, fsp2, smb_fname_src->st.st_ex_size);
1414 * As we are opening fsp1 read-only we only expect
1415 * an error on close on fsp2 if we are out of space.
1416 * Thus we don't look at the error return from the
1419 close_file(NULL, fsp1, NORMAL_CLOSE);
1421 /* Ensure the modtime is set correctly on the destination file. */
1422 set_close_write_time(fsp2, smb_fname_src->st.st_ex_mtime);
1424 status = close_file(NULL, fsp2, NORMAL_CLOSE);
1426 /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
1427 creates the file. This isn't the correct thing to do in the copy
1429 if (!parent_dirname(talloc_tos(), smb_fname_dst->base_name, &parent,
1431 status = NT_STATUS_NO_MEMORY;
1434 file_set_dosmode(conn, smb_fname_dst, fattr, parent, false);
1435 TALLOC_FREE(parent);
1437 if (ret < (SMB_OFF_T)smb_fname_src->st.st_ex_size) {
1438 status = NT_STATUS_DISK_FULL;
1442 if (!NT_STATUS_IS_OK(status)) {
1443 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1444 nt_errstr(status), smb_fname_str_dbg(smb_fname_src),
1445 smb_fname_str_dbg(smb_fname_dst)));
1451 /****************************************************************************
1452 Reply to a NT rename request.
1453 ****************************************************************************/
1455 void reply_ntrename(struct smb_request *req)
1457 connection_struct *conn = req->conn;
1458 struct smb_filename *smb_fname_old = NULL;
1459 struct smb_filename *smb_fname_new = NULL;
1460 char *oldname = NULL;
1461 char *newname = NULL;
1464 bool src_has_wcard = False;
1465 bool dest_has_wcard = False;
1467 uint32_t ucf_flags_src = 0;
1468 uint32_t ucf_flags_dst = 0;
1470 TALLOC_CTX *ctx = talloc_tos();
1472 START_PROFILE(SMBntrename);
1475 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1479 attrs = SVAL(req->vwv+0, 0);
1480 rename_type = SVAL(req->vwv+1, 0);
1482 p = (const char *)req->buf + 1;
1483 p += srvstr_get_path_req_wcard(ctx, req, &oldname, p, STR_TERMINATE,
1484 &status, &src_has_wcard);
1485 if (!NT_STATUS_IS_OK(status)) {
1486 reply_nterror(req, status);
1490 if (ms_has_wild(oldname)) {
1491 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1496 p += srvstr_get_path_req_wcard(ctx, req, &newname, p, STR_TERMINATE,
1497 &status, &dest_has_wcard);
1498 if (!NT_STATUS_IS_OK(status)) {
1499 reply_nterror(req, status);
1503 /* The newname must begin with a ':' if the oldname contains a ':'. */
1504 if (strchr_m(oldname, ':') && (newname[0] != ':')) {
1505 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1510 * If this is a rename operation, allow wildcards and save the
1511 * destination's last component.
1513 if (rename_type == RENAME_FLAG_RENAME) {
1514 ucf_flags_src = UCF_COND_ALLOW_WCARD_LCOMP;
1515 ucf_flags_dst = UCF_COND_ALLOW_WCARD_LCOMP | UCF_SAVE_LCOMP;
1518 /* rename_internals() calls unix_convert(), so don't call it here. */
1519 status = filename_convert(ctx, conn,
1520 req->flags2 & FLAGS2_DFS_PATHNAMES,
1525 if (!NT_STATUS_IS_OK(status)) {
1526 if (NT_STATUS_EQUAL(status,
1527 NT_STATUS_PATH_NOT_COVERED)) {
1528 reply_botherror(req,
1529 NT_STATUS_PATH_NOT_COVERED,
1530 ERRSRV, ERRbadpath);
1533 reply_nterror(req, status);
1537 status = filename_convert(ctx, conn,
1538 req->flags2 & FLAGS2_DFS_PATHNAMES,
1543 if (!NT_STATUS_IS_OK(status)) {
1544 if (NT_STATUS_EQUAL(status,
1545 NT_STATUS_PATH_NOT_COVERED)) {
1546 reply_botherror(req,
1547 NT_STATUS_PATH_NOT_COVERED,
1548 ERRSRV, ERRbadpath);
1551 reply_nterror(req, status);
1555 DEBUG(3,("reply_ntrename: %s -> %s\n",
1556 smb_fname_str_dbg(smb_fname_old),
1557 smb_fname_str_dbg(smb_fname_new)));
1559 switch(rename_type) {
1560 case RENAME_FLAG_RENAME:
1561 status = rename_internals(ctx, conn, req,
1562 smb_fname_old, smb_fname_new,
1563 attrs, False, src_has_wcard,
1567 case RENAME_FLAG_HARD_LINK:
1568 if (src_has_wcard || dest_has_wcard) {
1570 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1572 status = hardlink_internals(ctx, conn,
1577 case RENAME_FLAG_COPY:
1578 if (src_has_wcard || dest_has_wcard) {
1580 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1582 status = copy_internals(ctx, conn, req,
1588 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1589 status = NT_STATUS_INVALID_PARAMETER;
1592 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1596 if (!NT_STATUS_IS_OK(status)) {
1597 if (open_was_deferred(req->mid)) {
1598 /* We have re-scheduled this call. */
1602 reply_nterror(req, status);
1606 reply_outbuf(req, 0, 0);
1608 END_PROFILE(SMBntrename);
1612 /****************************************************************************
1613 Reply to a notify change - queue the request and
1614 don't allow a directory to be opened.
1615 ****************************************************************************/
1617 static void smbd_smb1_notify_reply(struct smb_request *req,
1618 NTSTATUS error_code,
1619 uint8_t *buf, size_t len)
1621 send_nt_replies(req->conn, req, error_code, (char *)buf, len, NULL, 0);
1624 static void call_nt_transact_notify_change(connection_struct *conn,
1625 struct smb_request *req,
1629 uint32 parameter_count,
1630 char **ppdata, uint32 data_count,
1631 uint32 max_data_count,
1632 uint32 max_param_count)
1634 uint16 *setup = *ppsetup;
1640 if(setup_count < 6) {
1641 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1645 fsp = file_fsp(req, SVAL(setup,4));
1646 filter = IVAL(setup, 0);
1647 recursive = (SVAL(setup, 6) != 0) ? True : False;
1649 DEBUG(3,("call_nt_transact_notify_change\n"));
1652 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1657 char *filter_string;
1659 if (!(filter_string = notify_filter_string(NULL, filter))) {
1660 reply_nterror(req,NT_STATUS_NO_MEMORY);
1664 DEBUG(3,("call_nt_transact_notify_change: notify change "
1665 "called on %s, filter = %s, recursive = %d\n",
1666 fsp_str_dbg(fsp), filter_string, recursive));
1668 TALLOC_FREE(filter_string);
1671 if((!fsp->is_directory) || (conn != fsp->conn)) {
1672 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1676 if (fsp->notify == NULL) {
1678 status = change_notify_create(fsp, filter, recursive);
1680 if (!NT_STATUS_IS_OK(status)) {
1681 DEBUG(10, ("change_notify_create returned %s\n",
1682 nt_errstr(status)));
1683 reply_nterror(req, status);
1688 if (fsp->notify->num_changes != 0) {
1691 * We've got changes pending, respond immediately
1695 * TODO: write a torture test to check the filtering behaviour
1699 change_notify_reply(fsp->conn, req,
1703 smbd_smb1_notify_reply);
1706 * change_notify_reply() above has independently sent its
1713 * No changes pending, queue the request
1716 status = change_notify_add_request(req,
1720 smbd_smb1_notify_reply);
1721 if (!NT_STATUS_IS_OK(status)) {
1722 reply_nterror(req, status);
1727 /****************************************************************************
1728 Reply to an NT transact rename command.
1729 ****************************************************************************/
1731 static void call_nt_transact_rename(connection_struct *conn,
1732 struct smb_request *req,
1733 uint16 **ppsetup, uint32 setup_count,
1734 char **ppparams, uint32 parameter_count,
1735 char **ppdata, uint32 data_count,
1736 uint32 max_data_count)
1738 char *params = *ppparams;
1739 char *new_name = NULL;
1740 files_struct *fsp = NULL;
1741 bool dest_has_wcard = False;
1743 TALLOC_CTX *ctx = talloc_tos();
1745 if(parameter_count < 5) {
1746 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1750 fsp = file_fsp(req, SVAL(params, 0));
1751 if (!check_fsp(conn, req, fsp)) {
1754 srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
1755 parameter_count - 4,
1756 STR_TERMINATE, &status, &dest_has_wcard);
1757 if (!NT_STATUS_IS_OK(status)) {
1758 reply_nterror(req, status);
1763 * W2K3 ignores this request as the RAW-RENAME test
1764 * demonstrates, so we do.
1766 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1768 DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1769 fsp_str_dbg(fsp), new_name));
1774 /******************************************************************************
1775 Fake up a completely empty SD.
1776 *******************************************************************************/
1778 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
1782 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1784 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1785 return NT_STATUS_NO_MEMORY;
1788 return NT_STATUS_OK;
1791 /****************************************************************************
1792 Reply to query a security descriptor.
1793 ****************************************************************************/
1795 static void call_nt_transact_query_security_desc(connection_struct *conn,
1796 struct smb_request *req,
1800 uint32 parameter_count,
1803 uint32 max_data_count)
1805 char *params = *ppparams;
1806 char *data = *ppdata;
1807 SEC_DESC *psd = NULL;
1809 uint32 security_info_wanted;
1810 files_struct *fsp = NULL;
1814 if(parameter_count < 8) {
1815 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1819 fsp = file_fsp(req, SVAL(params,0));
1821 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1825 security_info_wanted = IVAL(params,4);
1827 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, "
1828 "info_wanted = 0x%x\n", fsp_str_dbg(fsp),
1829 (unsigned int)security_info_wanted));
1831 params = nttrans_realloc(ppparams, 4);
1832 if(params == NULL) {
1833 reply_nterror(req, NT_STATUS_NO_MEMORY);
1838 * Get the permissions to return.
1841 if ((security_info_wanted & SECINFO_SACL) &&
1842 !(fsp->access_mask & SEC_FLAG_SYSTEM_SECURITY)) {
1843 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1847 if ((security_info_wanted & (SECINFO_DACL|SECINFO_OWNER|SECINFO_GROUP)) &&
1848 !(fsp->access_mask & SEC_STD_READ_CONTROL)) {
1849 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1853 if (security_info_wanted & (SECINFO_DACL|SECINFO_OWNER|
1854 SECINFO_GROUP|SECINFO_SACL)) {
1855 /* Don't return SECINFO_LABEL if anything else was
1856 requested. See bug #8458. */
1857 security_info_wanted &= ~SECINFO_LABEL;
1860 if (!lp_nt_acl_support(SNUM(conn))) {
1861 status = get_null_nt_acl(talloc_tos(), &psd);
1862 } else if (security_info_wanted & SECINFO_LABEL) {
1863 /* Like W2K3 return a null object. */
1864 status = get_null_nt_acl(talloc_tos(), &psd);
1866 status = SMB_VFS_FGET_NT_ACL(
1867 fsp, security_info_wanted, &psd);
1869 if (!NT_STATUS_IS_OK(status)) {
1870 reply_nterror(req, status);
1874 if (!(security_info_wanted & SECINFO_OWNER)) {
1875 psd->owner_sid = NULL;
1877 if (!(security_info_wanted & SECINFO_GROUP)) {
1878 psd->group_sid = NULL;
1880 if (!(security_info_wanted & SECINFO_DACL)) {
1881 psd->type &= ~SEC_DESC_DACL_PRESENT;
1884 if (!(security_info_wanted & SECINFO_SACL)) {
1885 psd->type &= ~SEC_DESC_SACL_PRESENT;
1889 /* If the SACL/DACL is NULL, but was requested, we mark that it is
1890 * present in the reply to match Windows behavior */
1891 if (psd->sacl == NULL &&
1892 security_info_wanted & SACL_SECURITY_INFORMATION)
1893 psd->type |= SEC_DESC_SACL_PRESENT;
1894 if (psd->dacl == NULL &&
1895 security_info_wanted & DACL_SECURITY_INFORMATION)
1896 psd->type |= SEC_DESC_DACL_PRESENT;
1898 if (security_info_wanted & SECINFO_LABEL) {
1899 /* Like W2K3 return a null object. */
1900 psd->owner_sid = NULL;
1901 psd->group_sid = NULL;
1904 psd->type &= ~(SEC_DESC_DACL_PRESENT|SEC_DESC_SACL_PRESENT);
1907 sd_size = ndr_size_security_descriptor(psd, NULL, 0);
1909 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
1911 if (DEBUGLEVEL >= 10) {
1912 DEBUG(10,("call_nt_transact_query_security_desc for file %s\n",
1914 NDR_PRINT_DEBUG(security_descriptor, psd);
1917 SIVAL(params,0,(uint32)sd_size);
1919 if (max_data_count < sd_size) {
1920 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
1921 params, 4, *ppdata, 0);
1926 * Allocate the data we will point this at.
1929 data = nttrans_realloc(ppdata, sd_size);
1931 reply_nterror(req, NT_STATUS_NO_MEMORY);
1935 status = marshall_sec_desc(talloc_tos(), psd,
1936 &blob.data, &blob.length);
1938 if (!NT_STATUS_IS_OK(status)) {
1939 reply_nterror(req, status);
1943 SMB_ASSERT(sd_size == blob.length);
1944 memcpy(data, blob.data, sd_size);
1946 send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
1951 /****************************************************************************
1952 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
1953 ****************************************************************************/
1955 static void call_nt_transact_set_security_desc(connection_struct *conn,
1956 struct smb_request *req,
1960 uint32 parameter_count,
1963 uint32 max_data_count)
1965 char *params= *ppparams;
1966 char *data = *ppdata;
1967 files_struct *fsp = NULL;
1968 uint32 security_info_sent = 0;
1971 if(parameter_count < 8) {
1972 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1976 if((fsp = file_fsp(req, SVAL(params,0))) == NULL) {
1977 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1981 if(!lp_nt_acl_support(SNUM(conn))) {
1985 security_info_sent = IVAL(params,4);
1987 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n",
1988 fsp_str_dbg(fsp), (unsigned int)security_info_sent));
1990 if (data_count == 0) {
1991 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1995 status = set_sd(fsp, (uint8 *)data, data_count, security_info_sent);
1997 if (!NT_STATUS_IS_OK(status)) {
1998 reply_nterror(req, status);
2003 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2007 /****************************************************************************
2009 ****************************************************************************/
2011 static void call_nt_transact_ioctl(connection_struct *conn,
2012 struct smb_request *req,
2013 uint16 **ppsetup, uint32 setup_count,
2014 char **ppparams, uint32 parameter_count,
2015 char **ppdata, uint32 data_count,
2016 uint32 max_data_count)
2023 char *pdata = *ppdata;
2025 if (setup_count != 8) {
2026 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
2027 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2031 function = IVAL(*ppsetup, 0);
2032 fidnum = SVAL(*ppsetup, 4);
2033 isFSctl = CVAL(*ppsetup, 6);
2034 compfilter = CVAL(*ppsetup, 7);
2036 DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
2037 function, fidnum, isFSctl, compfilter));
2039 fsp=file_fsp(req, fidnum);
2040 /* this check is done in each implemented function case for now
2041 because I don't want to break anything... --metze
2042 FSP_BELONGS_CONN(fsp,conn);*/
2044 SMB_PERFCOUNT_SET_IOCTL(&req->pcd, function);
2047 case FSCTL_SET_SPARSE:
2048 /* pretend this succeeded - tho strictly we should
2049 mark the file sparse (if the local fs supports it)
2050 so we can know if we need to pre-allocate or not */
2052 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
2053 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2056 case FSCTL_CREATE_OR_GET_OBJECT_ID:
2058 unsigned char objid[16];
2060 /* This should return the object-id on this file.
2061 * I think I'll make this be the inode+dev. JRA.
2064 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
2066 if (!fsp_belongs_conn(conn, req, fsp)) {
2071 pdata = nttrans_realloc(ppdata, data_count);
2072 if (pdata == NULL) {
2073 reply_nterror(req, NT_STATUS_NO_MEMORY);
2077 /* For backwards compatibility only store the dev/inode. */
2078 push_file_id_16(pdata, &fsp->file_id);
2079 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
2080 push_file_id_16(pdata+32, &fsp->file_id);
2081 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
2086 case FSCTL_GET_REPARSE_POINT:
2087 /* pretend this fail - my winXP does it like this
2091 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2092 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
2095 case FSCTL_SET_REPARSE_POINT:
2096 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
2100 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2101 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
2104 case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
2107 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
2108 * and return their volume names. If max_data_count is 16, then it is just
2109 * asking for the number of volumes and length of the combined names.
2111 * pdata is the data allocated by our caller, but that uses
2112 * total_data_count (which is 0 in our case) rather than max_data_count.
2113 * Allocate the correct amount and return the pointer to let
2114 * it be deallocated when we return.
2116 SHADOW_COPY_DATA *shadow_data = NULL;
2117 TALLOC_CTX *shadow_mem_ctx = NULL;
2118 bool labels = False;
2119 uint32 labels_data_count = 0;
2123 if (!fsp_belongs_conn(conn, req, fsp)) {
2127 if (max_data_count < 16) {
2128 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
2130 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2134 if (max_data_count > 16) {
2138 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
2139 if (shadow_mem_ctx == NULL) {
2140 DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
2141 reply_nterror(req, NT_STATUS_NO_MEMORY);
2145 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
2146 if (shadow_data == NULL) {
2147 DEBUG(0,("TALLOC_ZERO() failed!\n"));
2148 talloc_destroy(shadow_mem_ctx);
2149 reply_nterror(req, NT_STATUS_NO_MEMORY);
2153 shadow_data->mem_ctx = shadow_mem_ctx;
2156 * Call the VFS routine to actually do the work.
2158 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
2159 talloc_destroy(shadow_data->mem_ctx);
2160 if (errno == ENOSYS) {
2161 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
2162 conn->connectpath));
2163 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2166 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
2167 conn->connectpath));
2168 reply_nterror(req, NT_STATUS_UNSUCCESSFUL);
2173 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
2178 data_count = 12+labels_data_count+4;
2181 if (max_data_count<data_count) {
2182 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
2183 max_data_count,data_count));
2184 talloc_destroy(shadow_data->mem_ctx);
2185 reply_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
2189 pdata = nttrans_realloc(ppdata, data_count);
2190 if (pdata == NULL) {
2191 talloc_destroy(shadow_data->mem_ctx);
2192 reply_nterror(req, NT_STATUS_NO_MEMORY);
2198 /* num_volumes 4 bytes */
2199 SIVAL(pdata,0,shadow_data->num_volumes);
2202 /* num_labels 4 bytes */
2203 SIVAL(pdata,4,shadow_data->num_volumes);
2206 /* needed_data_count 4 bytes */
2207 SIVAL(pdata, 8, labels_data_count+4);
2211 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
2212 shadow_data->num_volumes, fsp_str_dbg(fsp)));
2213 if (labels && shadow_data->labels) {
2214 for (i=0;i<shadow_data->num_volumes;i++) {
2215 srvstr_push(pdata, req->flags2,
2216 cur_pdata, shadow_data->labels[i],
2217 2*sizeof(SHADOW_COPY_LABEL),
2218 STR_UNICODE|STR_TERMINATE);
2219 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
2220 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
2224 talloc_destroy(shadow_data->mem_ctx);
2226 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
2232 case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
2234 /* pretend this succeeded -
2236 * we have to send back a list with all files owned by this SID
2238 * but I have to check that --metze
2242 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
2244 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
2246 if (!fsp_belongs_conn(conn, req, fsp)) {
2250 /* unknown 4 bytes: this is not the length of the sid :-( */
2251 /*unknown = IVAL(pdata,0);*/
2253 if (!sid_parse(pdata+4,sid_len,&sid)) {
2254 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2258 DEBUGADD(10, ("for SID: %s\n", sid_string_dbg(&sid)));
2260 if (!sid_to_uid(&sid, &uid)) {
2261 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2262 sid_string_dbg(&sid),
2263 (unsigned long)sid_len));
2267 /* we can take a look at the find source :-)
2269 * find ./ -uid $uid -name '*' is what we need here
2272 * and send 4bytes len and then NULL terminated unicode strings
2275 * but I don't know how to deal with the paged results
2276 * (maybe we can hang the result anywhere in the fsp struct)
2278 * we don't send all files at once
2279 * and at the next we should *not* start from the beginning,
2280 * so we have to cache the result
2285 /* this works for now... */
2286 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2290 if (!logged_ioctl_message) {
2291 logged_ioctl_message = true; /* Only print this once... */
2292 DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
2297 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2301 #ifdef HAVE_SYS_QUOTAS
2302 /****************************************************************************
2303 Reply to get user quota
2304 ****************************************************************************/
2306 static void call_nt_transact_get_user_quota(connection_struct *conn,
2307 struct smb_request *req,
2311 uint32 parameter_count,
2314 uint32 max_data_count)
2316 NTSTATUS nt_status = NT_STATUS_OK;
2317 char *params = *ppparams;
2318 char *pdata = *ppdata;
2320 int data_len=0,param_len=0;
2323 files_struct *fsp = NULL;
2327 bool start_enum = True;
2328 SMB_NTQUOTA_STRUCT qt;
2329 SMB_NTQUOTA_LIST *tmp_list;
2330 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2335 if (conn->server_info->utok.uid != 0 && !conn->admin_user) {
2336 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2337 "[%s]\n", lp_servicename(SNUM(conn)),
2338 conn->server_info->unix_name));
2339 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2344 * Ensure minimum number of parameters sent.
2347 if (parameter_count < 4) {
2348 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2349 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2353 /* maybe we can check the quota_fnum */
2354 fsp = file_fsp(req, SVAL(params,0));
2355 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2356 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2357 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2361 /* the NULL pointer checking for fsp->fake_file_handle->pd
2362 * is done by CHECK_NTQUOTA_HANDLE_OK()
2364 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->private_data;
2366 level = SVAL(params,2);
2368 /* unknown 12 bytes leading in params */
2371 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2372 /* seems that we should continue with the enum here --metze */
2374 if (qt_handle->quota_list!=NULL &&
2375 qt_handle->tmp_list==NULL) {
2378 free_ntquota_list(&(qt_handle->quota_list));
2380 /* Realloc the size of parameters and data we will return */
2382 params = nttrans_realloc(ppparams, param_len);
2383 if(params == NULL) {
2384 reply_nterror(req, NT_STATUS_NO_MEMORY);
2389 SIVAL(params,0,data_len);
2396 case TRANSACT_GET_USER_QUOTA_LIST_START:
2398 if (qt_handle->quota_list==NULL &&
2399 qt_handle->tmp_list==NULL) {
2403 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2404 reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
2408 /* Realloc the size of parameters and data we will return */
2410 params = nttrans_realloc(ppparams, param_len);
2411 if(params == NULL) {
2412 reply_nterror(req, NT_STATUS_NO_MEMORY);
2416 /* we should not trust the value in max_data_count*/
2417 max_data_count = MIN(max_data_count,2048);
2419 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2421 reply_nterror(req, NT_STATUS_NO_MEMORY);
2427 /* set params Size of returned Quota Data 4 bytes*/
2428 /* but set it later when we know it */
2430 /* for each entry push the data */
2433 qt_handle->tmp_list = qt_handle->quota_list;
2436 tmp_list = qt_handle->tmp_list;
2438 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2439 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2441 sid_len = ndr_size_dom_sid(
2442 &tmp_list->quotas->sid, NULL, 0);
2443 entry_len = 40 + sid_len;
2445 /* nextoffset entry 4 bytes */
2446 SIVAL(entry,0,entry_len);
2448 /* then the len of the SID 4 bytes */
2449 SIVAL(entry,4,sid_len);
2451 /* unknown data 8 bytes uint64_t */
2452 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-metze*/
2454 /* the used disk space 8 bytes uint64_t */
2455 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2457 /* the soft quotas 8 bytes uint64_t */
2458 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2460 /* the hard quotas 8 bytes uint64_t */
2461 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2463 /* and now the SID */
2464 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2467 qt_handle->tmp_list = tmp_list;
2469 /* overwrite the offset of the last entry */
2470 SIVAL(entry-entry_len,0,0);
2472 data_len = 4+qt_len;
2473 /* overwrite the params quota_data_len */
2474 SIVAL(params,0,data_len);
2478 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2480 /* unknown 4 bytes IVAL(pdata,0) */
2482 if (data_count < 8) {
2483 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2484 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2488 sid_len = IVAL(pdata,4);
2489 /* Ensure this is less than 1mb. */
2490 if (sid_len > (1024*1024)) {
2491 reply_nterror(req, NT_STATUS_NO_MEMORY);
2495 if (data_count < 8+sid_len) {
2496 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2497 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2501 data_len = 4+40+sid_len;
2503 if (max_data_count < data_len) {
2504 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2505 max_data_count, data_len));
2507 SIVAL(params,0,data_len);
2509 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2513 if (!sid_parse(pdata+8,sid_len,&sid)) {
2514 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2518 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2521 * we have to return zero's in all fields
2522 * instead of returning an error here
2527 /* Realloc the size of parameters and data we will return */
2529 params = nttrans_realloc(ppparams, param_len);
2530 if(params == NULL) {
2531 reply_nterror(req, NT_STATUS_NO_MEMORY);
2535 pdata = nttrans_realloc(ppdata, data_len);
2537 reply_nterror(req, NT_STATUS_NO_MEMORY);
2543 /* set params Size of returned Quota Data 4 bytes*/
2544 SIVAL(params,0,data_len);
2546 /* nextoffset entry 4 bytes */
2549 /* then the len of the SID 4 bytes */
2550 SIVAL(entry,4,sid_len);
2552 /* unknown data 8 bytes uint64_t */
2553 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-mezte*/
2555 /* the used disk space 8 bytes uint64_t */
2556 SBIG_UINT(entry,16,qt.usedspace);
2558 /* the soft quotas 8 bytes uint64_t */
2559 SBIG_UINT(entry,24,qt.softlim);
2561 /* the hard quotas 8 bytes uint64_t */
2562 SBIG_UINT(entry,32,qt.hardlim);
2564 /* and now the SID */
2565 sid_linearize(entry+40, sid_len, &sid);
2570 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2571 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2576 send_nt_replies(conn, req, nt_status, params, param_len,
2580 /****************************************************************************
2581 Reply to set user quota
2582 ****************************************************************************/
2584 static void call_nt_transact_set_user_quota(connection_struct *conn,
2585 struct smb_request *req,
2589 uint32 parameter_count,
2592 uint32 max_data_count)
2594 char *params = *ppparams;
2595 char *pdata = *ppdata;
2596 int data_len=0,param_len=0;
2597 SMB_NTQUOTA_STRUCT qt;
2600 files_struct *fsp = NULL;
2605 if (conn->server_info->utok.uid != 0 && !conn->admin_user) {
2606 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2607 "[%s]\n", lp_servicename(SNUM(conn)),
2608 conn->server_info->unix_name));
2609 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2614 * Ensure minimum number of parameters sent.
2617 if (parameter_count < 2) {
2618 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2619 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2623 /* maybe we can check the quota_fnum */
2624 fsp = file_fsp(req, SVAL(params,0));
2625 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2626 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2627 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2631 if (data_count < 40) {
2632 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2633 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2637 /* offset to next quota record.
2638 * 4 bytes IVAL(pdata,0)
2643 sid_len = IVAL(pdata,4);
2645 if (data_count < 40+sid_len) {
2646 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2647 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2651 /* unknown 8 bytes in pdata
2652 * maybe its the change time in NTTIME
2655 /* the used space 8 bytes (uint64_t)*/
2656 qt.usedspace = (uint64_t)IVAL(pdata,16);
2657 #ifdef LARGE_SMB_OFF_T
2658 qt.usedspace |= (((uint64_t)IVAL(pdata,20)) << 32);
2659 #else /* LARGE_SMB_OFF_T */
2660 if ((IVAL(pdata,20) != 0)&&
2661 ((qt.usedspace != 0xFFFFFFFF)||
2662 (IVAL(pdata,20)!=0xFFFFFFFF))) {
2663 /* more than 32 bits? */
2664 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2667 #endif /* LARGE_SMB_OFF_T */
2669 /* the soft quotas 8 bytes (uint64_t)*/
2670 qt.softlim = (uint64_t)IVAL(pdata,24);
2671 #ifdef LARGE_SMB_OFF_T
2672 qt.softlim |= (((uint64_t)IVAL(pdata,28)) << 32);
2673 #else /* LARGE_SMB_OFF_T */
2674 if ((IVAL(pdata,28) != 0)&&
2675 ((qt.softlim != 0xFFFFFFFF)||
2676 (IVAL(pdata,28)!=0xFFFFFFFF))) {
2677 /* more than 32 bits? */
2678 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2681 #endif /* LARGE_SMB_OFF_T */
2683 /* the hard quotas 8 bytes (uint64_t)*/
2684 qt.hardlim = (uint64_t)IVAL(pdata,32);
2685 #ifdef LARGE_SMB_OFF_T
2686 qt.hardlim |= (((uint64_t)IVAL(pdata,36)) << 32);
2687 #else /* LARGE_SMB_OFF_T */
2688 if ((IVAL(pdata,36) != 0)&&
2689 ((qt.hardlim != 0xFFFFFFFF)||
2690 (IVAL(pdata,36)!=0xFFFFFFFF))) {
2691 /* more than 32 bits? */
2692 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2695 #endif /* LARGE_SMB_OFF_T */
2697 if (!sid_parse(pdata+40,sid_len,&sid)) {
2698 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2702 DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid)));
2704 /* 44 unknown bytes left... */
2706 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2707 reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
2711 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2714 #endif /* HAVE_SYS_QUOTAS */
2716 static void handle_nttrans(connection_struct *conn,
2717 struct trans_state *state,
2718 struct smb_request *req)
2720 if (get_Protocol() >= PROTOCOL_NT1) {
2721 req->flags2 |= 0x40; /* IS_LONG_NAME */
2722 SSVAL(req->inbuf,smb_flg2,req->flags2);
2726 SMB_PERFCOUNT_SET_SUBOP(&req->pcd, state->call);
2728 /* Now we must call the relevant NT_TRANS function */
2729 switch(state->call) {
2730 case NT_TRANSACT_CREATE:
2732 START_PROFILE(NT_transact_create);
2733 call_nt_transact_create(
2735 &state->setup, state->setup_count,
2736 &state->param, state->total_param,
2737 &state->data, state->total_data,
2738 state->max_data_return);
2739 END_PROFILE(NT_transact_create);
2743 case NT_TRANSACT_IOCTL:
2745 START_PROFILE(NT_transact_ioctl);
2746 call_nt_transact_ioctl(
2748 &state->setup, state->setup_count,
2749 &state->param, state->total_param,
2750 &state->data, state->total_data,
2751 state->max_data_return);
2752 END_PROFILE(NT_transact_ioctl);
2756 case NT_TRANSACT_SET_SECURITY_DESC:
2758 START_PROFILE(NT_transact_set_security_desc);
2759 call_nt_transact_set_security_desc(
2761 &state->setup, state->setup_count,
2762 &state->param, state->total_param,
2763 &state->data, state->total_data,
2764 state->max_data_return);
2765 END_PROFILE(NT_transact_set_security_desc);
2769 case NT_TRANSACT_NOTIFY_CHANGE:
2771 START_PROFILE(NT_transact_notify_change);
2772 call_nt_transact_notify_change(
2774 &state->setup, state->setup_count,
2775 &state->param, state->total_param,
2776 &state->data, state->total_data,
2777 state->max_data_return,
2778 state->max_param_return);
2779 END_PROFILE(NT_transact_notify_change);
2783 case NT_TRANSACT_RENAME:
2785 START_PROFILE(NT_transact_rename);
2786 call_nt_transact_rename(
2788 &state->setup, state->setup_count,
2789 &state->param, state->total_param,
2790 &state->data, state->total_data,
2791 state->max_data_return);
2792 END_PROFILE(NT_transact_rename);
2796 case NT_TRANSACT_QUERY_SECURITY_DESC:
2798 START_PROFILE(NT_transact_query_security_desc);
2799 call_nt_transact_query_security_desc(
2801 &state->setup, state->setup_count,
2802 &state->param, state->total_param,
2803 &state->data, state->total_data,
2804 state->max_data_return);
2805 END_PROFILE(NT_transact_query_security_desc);
2809 #ifdef HAVE_SYS_QUOTAS
2810 case NT_TRANSACT_GET_USER_QUOTA:
2812 START_PROFILE(NT_transact_get_user_quota);
2813 call_nt_transact_get_user_quota(
2815 &state->setup, state->setup_count,
2816 &state->param, state->total_param,
2817 &state->data, state->total_data,
2818 state->max_data_return);
2819 END_PROFILE(NT_transact_get_user_quota);
2823 case NT_TRANSACT_SET_USER_QUOTA:
2825 START_PROFILE(NT_transact_set_user_quota);
2826 call_nt_transact_set_user_quota(
2828 &state->setup, state->setup_count,
2829 &state->param, state->total_param,
2830 &state->data, state->total_data,
2831 state->max_data_return);
2832 END_PROFILE(NT_transact_set_user_quota);
2835 #endif /* HAVE_SYS_QUOTAS */
2838 /* Error in request */
2839 DEBUG(0,("handle_nttrans: Unknown request %d in "
2840 "nttrans call\n", state->call));
2841 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2847 /****************************************************************************
2848 Reply to a SMBNTtrans.
2849 ****************************************************************************/
2851 void reply_nttrans(struct smb_request *req)
2853 connection_struct *conn = req->conn;
2858 uint16 function_code;
2860 struct trans_state *state;
2862 START_PROFILE(SMBnttrans);
2864 if (req->wct < 19) {
2865 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2866 END_PROFILE(SMBnttrans);
2870 pscnt = IVAL(req->vwv+9, 1);
2871 psoff = IVAL(req->vwv+11, 1);
2872 dscnt = IVAL(req->vwv+13, 1);
2873 dsoff = IVAL(req->vwv+15, 1);
2874 function_code = SVAL(req->vwv+18, 0);
2876 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2877 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2878 END_PROFILE(SMBnttrans);
2882 result = allow_new_trans(conn->pending_trans, req->mid);
2883 if (!NT_STATUS_IS_OK(result)) {
2884 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
2885 reply_nterror(req, result);
2886 END_PROFILE(SMBnttrans);
2890 if ((state = TALLOC_P(conn, struct trans_state)) == NULL) {
2891 reply_nterror(req, NT_STATUS_NO_MEMORY);
2892 END_PROFILE(SMBnttrans);
2896 state->cmd = SMBnttrans;
2898 state->mid = req->mid;
2899 state->vuid = req->vuid;
2900 state->total_data = IVAL(req->vwv+3, 1);
2902 state->total_param = IVAL(req->vwv+1, 1);
2903 state->param = NULL;
2904 state->max_data_return = IVAL(req->vwv+7, 1);
2905 state->max_param_return = IVAL(req->vwv+5, 1);
2907 /* setup count is in *words* */
2908 state->setup_count = 2*CVAL(req->vwv+17, 1);
2909 state->setup = NULL;
2910 state->call = function_code;
2912 DEBUG(10, ("num_setup=%u, "
2913 "param_total=%u, this_param=%u, max_param=%u, "
2914 "data_total=%u, this_data=%u, max_data=%u, "
2915 "param_offset=%u, data_offset=%u\n",
2916 (unsigned)state->setup_count,
2917 (unsigned)state->total_param, (unsigned)pscnt,
2918 (unsigned)state->max_param_return,
2919 (unsigned)state->total_data, (unsigned)dscnt,
2920 (unsigned)state->max_data_return,
2921 (unsigned)psoff, (unsigned)dsoff));
2924 * All nttrans messages we handle have smb_wct == 19 +
2925 * state->setup_count. Ensure this is so as a sanity check.
2928 if(req->wct != 19 + (state->setup_count/2)) {
2929 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2930 req->wct, 19 + (state->setup_count/2)));
2934 /* Don't allow more than 128mb for each value. */
2935 if ((state->total_data > (1024*1024*128)) ||
2936 (state->total_param > (1024*1024*128))) {
2937 reply_nterror(req, NT_STATUS_NO_MEMORY);
2938 END_PROFILE(SMBnttrans);
2942 if ((dscnt > state->total_data) || (pscnt > state->total_param))
2945 if (state->total_data) {
2947 if (trans_oob(state->total_data, 0, dscnt)
2948 || trans_oob(smb_len(req->inbuf), dsoff, dscnt)) {
2952 /* Can't use talloc here, the core routines do realloc on the
2953 * params and data. */
2954 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
2955 DEBUG(0,("reply_nttrans: data malloc fail for %u "
2956 "bytes !\n", (unsigned int)state->total_data));
2958 reply_nterror(req, NT_STATUS_NO_MEMORY);
2959 END_PROFILE(SMBnttrans);
2963 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
2966 if (state->total_param) {
2968 if (trans_oob(state->total_param, 0, pscnt)
2969 || trans_oob(smb_len(req->inbuf), psoff, pscnt)) {
2973 /* Can't use talloc here, the core routines do realloc on the
2974 * params and data. */
2975 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
2976 DEBUG(0,("reply_nttrans: param malloc fail for %u "
2977 "bytes !\n", (unsigned int)state->total_param));
2978 SAFE_FREE(state->data);
2980 reply_nterror(req, NT_STATUS_NO_MEMORY);
2981 END_PROFILE(SMBnttrans);
2985 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
2988 state->received_data = dscnt;
2989 state->received_param = pscnt;
2991 if(state->setup_count > 0) {
2992 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2993 state->setup_count));
2996 * No overflow possible here, state->setup_count is an
2997 * unsigned int, being filled by a single byte from
2998 * CVAL(req->vwv+13, 0) above. The cast in the comparison
2999 * below is not necessary, it's here to clarify things. The
3000 * validity of req->vwv and req->wct has been checked in
3001 * init_smb_request already.
3003 if ((state->setup_count/2) + 19 > (unsigned int)req->wct) {
3007 state->setup = (uint16 *)TALLOC(state, state->setup_count);
3008 if (state->setup == NULL) {
3009 DEBUG(0,("reply_nttrans : Out of memory\n"));
3010 SAFE_FREE(state->data);
3011 SAFE_FREE(state->param);
3013 reply_nterror(req, NT_STATUS_NO_MEMORY);
3014 END_PROFILE(SMBnttrans);
3018 memcpy(state->setup, req->vwv+19, state->setup_count);
3019 dump_data(10, (uint8 *)state->setup, state->setup_count);
3022 if ((state->received_data == state->total_data) &&
3023 (state->received_param == state->total_param)) {
3024 handle_nttrans(conn, state, req);
3025 SAFE_FREE(state->param);
3026 SAFE_FREE(state->data);
3028 END_PROFILE(SMBnttrans);
3032 DLIST_ADD(conn->pending_trans, state);
3034 /* We need to send an interim response then receive the rest
3035 of the parameter/data bytes */
3036 reply_outbuf(req, 0, 0);
3037 show_msg((char *)req->outbuf);
3038 END_PROFILE(SMBnttrans);
3043 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
3044 SAFE_FREE(state->data);
3045 SAFE_FREE(state->param);
3047 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3048 END_PROFILE(SMBnttrans);
3052 /****************************************************************************
3053 Reply to a SMBnttranss
3054 ****************************************************************************/
3056 void reply_nttranss(struct smb_request *req)
3058 connection_struct *conn = req->conn;
3059 uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
3060 struct trans_state *state;
3062 START_PROFILE(SMBnttranss);
3064 show_msg((char *)req->inbuf);
3066 /* Windows clients expect all replies to
3067 an NT transact secondary (SMBnttranss 0xA1)
3068 to have a command code of NT transact
3069 (SMBnttrans 0xA0). See bug #8989 for details. */
3070 req->cmd = SMBnttrans;
3072 if (req->wct < 18) {
3073 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3074 END_PROFILE(SMBnttranss);
3078 for (state = conn->pending_trans; state != NULL;
3079 state = state->next) {
3080 if (state->mid == req->mid) {
3085 if ((state == NULL) || (state->cmd != SMBnttrans)) {
3086 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3087 END_PROFILE(SMBnttranss);
3091 /* Revise state->total_param and state->total_data in case they have
3092 changed downwards */
3093 if (IVAL(req->vwv+1, 1) < state->total_param) {
3094 state->total_param = IVAL(req->vwv+1, 1);
3096 if (IVAL(req->vwv+3, 1) < state->total_data) {
3097 state->total_data = IVAL(req->vwv+3, 1);
3100 pcnt = IVAL(req->vwv+5, 1);
3101 poff = IVAL(req->vwv+7, 1);
3102 pdisp = IVAL(req->vwv+9, 1);
3104 dcnt = IVAL(req->vwv+11, 1);
3105 doff = IVAL(req->vwv+13, 1);
3106 ddisp = IVAL(req->vwv+15, 1);
3108 state->received_param += pcnt;
3109 state->received_data += dcnt;
3111 if ((state->received_data > state->total_data) ||
3112 (state->received_param > state->total_param))
3116 if (trans_oob(state->total_param, pdisp, pcnt)
3117 || trans_oob(smb_len(req->inbuf), poff, pcnt)) {
3120 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,pcnt);
3124 if (trans_oob(state->total_data, ddisp, dcnt)
3125 || trans_oob(smb_len(req->inbuf), doff, dcnt)) {
3128 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
3131 if ((state->received_param < state->total_param) ||
3132 (state->received_data < state->total_data)) {
3133 END_PROFILE(SMBnttranss);
3137 handle_nttrans(conn, state, req);
3139 DLIST_REMOVE(conn->pending_trans, state);
3140 SAFE_FREE(state->data);
3141 SAFE_FREE(state->param);
3143 END_PROFILE(SMBnttranss);
3148 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
3149 DLIST_REMOVE(conn->pending_trans, state);
3150 SAFE_FREE(state->data);
3151 SAFE_FREE(state->param);
3153 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3154 END_PROFILE(SMBnttranss);