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 * We might have had SMBnttranss in req->inbuf, fix that.
139 SCVAL(req->outbuf, smb_com, SMBnttrans);
142 * Set total params and data to be sent.
145 SIVAL(req->outbuf,smb_ntr_TotalParameterCount,paramsize);
146 SIVAL(req->outbuf,smb_ntr_TotalDataCount,datasize);
149 * Calculate how many parameters and data we can fit into
150 * this packet. Parameters get precedence.
153 params_sent_thistime = MIN(params_to_send,useable_space);
154 data_sent_thistime = useable_space - params_sent_thistime;
155 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
157 SIVAL(req->outbuf, smb_ntr_ParameterCount,
158 params_sent_thistime);
160 if(params_sent_thistime == 0) {
161 SIVAL(req->outbuf,smb_ntr_ParameterOffset,0);
162 SIVAL(req->outbuf,smb_ntr_ParameterDisplacement,0);
165 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
166 * parameter bytes, however the first 4 bytes of outbuf are
167 * the Netbios over TCP header. Thus use smb_base() to subtract
168 * them from the calculation.
171 SIVAL(req->outbuf,smb_ntr_ParameterOffset,
172 ((smb_buf(req->outbuf)+alignment_offset)
173 - smb_base(req->outbuf)));
175 * Absolute displacement of param bytes sent in this packet.
178 SIVAL(req->outbuf, smb_ntr_ParameterDisplacement,
183 * Deal with the data portion.
186 SIVAL(req->outbuf, smb_ntr_DataCount, data_sent_thistime);
188 if(data_sent_thistime == 0) {
189 SIVAL(req->outbuf,smb_ntr_DataOffset,0);
190 SIVAL(req->outbuf,smb_ntr_DataDisplacement, 0);
193 * The offset of the data bytes is the offset of the
194 * parameter bytes plus the number of parameters being sent this time.
197 SIVAL(req->outbuf, smb_ntr_DataOffset,
198 ((smb_buf(req->outbuf)+alignment_offset) -
199 smb_base(req->outbuf))
200 + params_sent_thistime + data_alignment_offset);
201 SIVAL(req->outbuf,smb_ntr_DataDisplacement, pd - pdata);
205 * Copy the param bytes into the packet.
208 if(params_sent_thistime) {
209 if (alignment_offset != 0) {
210 memset(smb_buf(req->outbuf), 0,
213 memcpy((smb_buf(req->outbuf)+alignment_offset), pp,
214 params_sent_thistime);
218 * Copy in the data bytes
221 if(data_sent_thistime) {
222 if (data_alignment_offset != 0) {
223 memset((smb_buf(req->outbuf)+alignment_offset+
224 params_sent_thistime), 0,
225 data_alignment_offset);
227 memcpy(smb_buf(req->outbuf)+alignment_offset
228 +params_sent_thistime+data_alignment_offset,
229 pd,data_sent_thistime);
232 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
233 params_sent_thistime, data_sent_thistime, useable_space));
234 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
235 params_to_send, data_to_send, paramsize, datasize));
237 if (NT_STATUS_V(nt_error)) {
238 error_packet_set((char *)req->outbuf,
243 /* Send the packet */
244 show_msg((char *)req->outbuf);
245 if (!srv_send_smb(smbd_server_fd(),
248 IS_CONN_ENCRYPTED(conn),
250 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
253 TALLOC_FREE(req->outbuf);
255 pp += params_sent_thistime;
256 pd += data_sent_thistime;
258 params_to_send -= params_sent_thistime;
259 data_to_send -= data_sent_thistime;
265 if(params_to_send < 0 || data_to_send < 0) {
266 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
267 params_to_send, data_to_send));
268 exit_server_cleanly("send_nt_replies: internal error");
273 /****************************************************************************
274 Reply to an NT create and X call on a pipe
275 ****************************************************************************/
277 static void nt_open_pipe(char *fname, connection_struct *conn,
278 struct smb_request *req, int *ppnum)
283 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
285 /* Strip \\ off the name. */
288 status = open_np_file(req, fname, &fsp);
289 if (!NT_STATUS_IS_OK(status)) {
290 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
291 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
295 reply_nterror(req, status);
303 /****************************************************************************
304 Reply to an NT create and X call for pipes.
305 ****************************************************************************/
307 static void do_ntcreate_pipe_open(connection_struct *conn,
308 struct smb_request *req)
313 uint32 flags = IVAL(req->vwv+3, 1);
314 TALLOC_CTX *ctx = talloc_tos();
316 srvstr_pull_req_talloc(ctx, req, &fname, req->buf, STR_TERMINATE);
319 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
323 nt_open_pipe(fname, conn, req, &pnum);
331 * Deal with pipe return.
334 if (flags & EXTENDED_RESPONSE_REQUIRED) {
335 /* This is very strange. We
336 * return 50 words, but only set
337 * the wcnt to 42 ? It's definately
338 * what happens on the wire....
340 reply_outbuf(req, 50, 0);
341 SCVAL(req->outbuf,smb_wct,42);
343 reply_outbuf(req, 34, 0);
346 p = (char *)req->outbuf + smb_vwv2;
350 SIVAL(p,0,FILE_WAS_OPENED);
353 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
356 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
358 SSVAL(p,2, 0x5FF); /* ? */
361 if (flags & EXTENDED_RESPONSE_REQUIRED) {
363 SIVAL(p,0,FILE_GENERIC_ALL);
365 * For pipes W2K3 seems to return
367 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
369 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
372 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
377 struct case_semantics_state {
378 connection_struct *conn;
381 bool short_case_preserve;
384 /****************************************************************************
385 Restore case semantics.
386 ****************************************************************************/
388 static int restore_case_semantics(struct case_semantics_state *state)
390 state->conn->case_sensitive = state->case_sensitive;
391 state->conn->case_preserve = state->case_preserve;
392 state->conn->short_case_preserve = state->short_case_preserve;
396 /****************************************************************************
398 ****************************************************************************/
400 static struct case_semantics_state *set_posix_case_semantics(TALLOC_CTX *mem_ctx,
401 connection_struct *conn)
403 struct case_semantics_state *result;
405 if (!(result = talloc(mem_ctx, struct case_semantics_state))) {
410 result->case_sensitive = conn->case_sensitive;
411 result->case_preserve = conn->case_preserve;
412 result->short_case_preserve = conn->short_case_preserve;
415 conn->case_sensitive = True;
416 conn->case_preserve = True;
417 conn->short_case_preserve = True;
419 talloc_set_destructor(result, restore_case_semantics);
424 /****************************************************************************
425 Reply to an NT create and X call.
426 ****************************************************************************/
428 void reply_ntcreate_and_X(struct smb_request *req)
430 connection_struct *conn = req->conn;
431 struct smb_filename *smb_fname = NULL;
435 uint32 file_attributes;
437 uint32 create_disposition;
438 uint32 create_options;
440 uint64_t allocation_size;
441 /* Breakout the oplock request bits so we can set the
442 reply bits separately. */
444 SMB_OFF_T file_len = 0;
446 files_struct *fsp = NULL;
448 struct timespec create_timespec;
449 struct timespec c_timespec;
450 struct timespec a_timespec;
451 struct timespec m_timespec;
452 struct timespec write_time_ts;
455 uint8_t oplock_granted = NO_OPLOCK_RETURN;
456 struct case_semantics_state *case_state = NULL;
457 TALLOC_CTX *ctx = talloc_tos();
459 START_PROFILE(SMBntcreateX);
462 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
466 flags = IVAL(req->vwv+3, 1);
467 access_mask = IVAL(req->vwv+7, 1);
468 file_attributes = IVAL(req->vwv+13, 1);
469 share_access = IVAL(req->vwv+15, 1);
470 create_disposition = IVAL(req->vwv+17, 1);
471 create_options = IVAL(req->vwv+19, 1);
472 root_dir_fid = (uint16)IVAL(req->vwv+5, 1);
474 allocation_size = (uint64_t)IVAL(req->vwv+9, 1);
475 #ifdef LARGE_SMB_OFF_T
476 allocation_size |= (((uint64_t)IVAL(req->vwv+11, 1)) << 32);
479 srvstr_get_path_req(ctx, req, &fname, (const char *)req->buf,
480 STR_TERMINATE, &status);
482 if (!NT_STATUS_IS_OK(status)) {
483 reply_nterror(req, status);
487 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
488 "file_attributes = 0x%x, share_access = 0x%x, "
489 "create_disposition = 0x%x create_options = 0x%x "
490 "root_dir_fid = 0x%x, fname = %s\n",
492 (unsigned int)access_mask,
493 (unsigned int)file_attributes,
494 (unsigned int)share_access,
495 (unsigned int)create_disposition,
496 (unsigned int)create_options,
497 (unsigned int)root_dir_fid,
501 * we need to remove ignored bits when they come directly from the client
502 * because we reuse some of them for internal stuff
504 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
507 * If it's an IPC, use the pipe handler.
511 if (lp_nt_pipe_support()) {
512 do_ntcreate_pipe_open(conn, req);
515 reply_doserror(req, ERRDOS, ERRnoaccess);
519 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
520 if (oplock_request) {
521 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
525 case_state = set_posix_case_semantics(ctx, conn);
527 reply_nterror(req, NT_STATUS_NO_MEMORY);
531 status = filename_convert(ctx,
533 req->flags2 & FLAGS2_DFS_PATHNAMES,
539 TALLOC_FREE(case_state);
541 if (!NT_STATUS_IS_OK(status)) {
542 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
544 NT_STATUS_PATH_NOT_COVERED,
548 reply_nterror(req, status);
553 * Bug #6898 - clients using Windows opens should
554 * never be able to set this attribute into the
557 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
559 status = SMB_VFS_CREATE_FILE(
562 root_dir_fid, /* root_dir_fid */
563 smb_fname, /* fname */
564 access_mask, /* access_mask */
565 share_access, /* share_access */
566 create_disposition, /* create_disposition*/
567 create_options, /* create_options */
568 file_attributes, /* file_attributes */
569 oplock_request, /* oplock_request */
570 allocation_size, /* allocation_size */
576 if (!NT_STATUS_IS_OK(status)) {
577 if (open_was_deferred(req->mid)) {
578 /* We have re-scheduled this call, no error. */
581 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
582 reply_botherror(req, status, ERRDOS, ERRfilexists);
585 reply_nterror(req, status);
590 /* Ensure we're pointing at the correct stat struct. */
591 TALLOC_FREE(smb_fname);
592 smb_fname = fsp->fsp_name;
595 * If the caller set the extended oplock request bit
596 * and we granted one (by whatever means) - set the
597 * correct bit for extended oplock reply.
600 if (oplock_request &&
601 (lp_fake_oplocks(SNUM(conn))
602 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
605 * Exclusive oplock granted
608 if (flags & REQUEST_BATCH_OPLOCK) {
609 oplock_granted = BATCH_OPLOCK_RETURN;
611 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
613 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
614 oplock_granted = LEVEL_II_OPLOCK_RETURN;
616 oplock_granted = NO_OPLOCK_RETURN;
619 file_len = smb_fname->st.st_ex_size;
621 if (flags & EXTENDED_RESPONSE_REQUIRED) {
622 /* This is very strange. We
623 * return 50 words, but only set
624 * the wcnt to 42 ? It's definately
625 * what happens on the wire....
627 reply_outbuf(req, 50, 0);
628 SCVAL(req->outbuf,smb_wct,42);
630 reply_outbuf(req, 34, 0);
633 p = (char *)req->outbuf + smb_vwv2;
635 SCVAL(p, 0, oplock_granted);
638 SSVAL(p,0,fsp->fnum);
640 if ((create_disposition == FILE_SUPERSEDE)
641 && (info == FILE_WAS_OVERWRITTEN)) {
642 SIVAL(p,0,FILE_WAS_SUPERSEDED);
648 fattr = dos_mode(conn, smb_fname);
650 fattr = FILE_ATTRIBUTE_NORMAL;
653 /* Deal with other possible opens having a modified
655 ZERO_STRUCT(write_time_ts);
656 get_file_infos(fsp->file_id, NULL, &write_time_ts);
657 if (!null_timespec(write_time_ts)) {
658 update_stat_ex_mtime(&smb_fname->st, write_time_ts);
662 create_timespec = get_create_timespec(conn, fsp, smb_fname);
663 a_timespec = smb_fname->st.st_ex_atime;
664 m_timespec = smb_fname->st.st_ex_mtime;
665 c_timespec = get_change_timespec(conn, fsp, smb_fname);
667 if (lp_dos_filetime_resolution(SNUM(conn))) {
668 dos_filetime_timespec(&create_timespec);
669 dos_filetime_timespec(&a_timespec);
670 dos_filetime_timespec(&m_timespec);
671 dos_filetime_timespec(&c_timespec);
674 put_long_date_timespec(conn->ts_res, p, create_timespec); /* create time. */
676 put_long_date_timespec(conn->ts_res, p, a_timespec); /* access time */
678 put_long_date_timespec(conn->ts_res, p, m_timespec); /* write time */
680 put_long_date_timespec(conn->ts_res, p, c_timespec); /* change time */
682 SIVAL(p,0,fattr); /* File Attributes. */
684 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn,fsp,&smb_fname->st));
686 SOFF_T(p,0,file_len);
688 if (flags & EXTENDED_RESPONSE_REQUIRED) {
689 uint16_t file_status = (NO_EAS|NO_SUBSTREAMS|NO_REPARSETAG);
690 size_t num_names = 0;
691 unsigned int num_streams;
692 struct stream_struct *streams = NULL;
694 /* Do we have any EA's ? */
695 status = get_ea_names_from_file(ctx, conn, fsp,
696 smb_fname->base_name, NULL, &num_names);
697 if (NT_STATUS_IS_OK(status) && num_names) {
698 file_status &= ~NO_EAS;
700 status = SMB_VFS_STREAMINFO(conn, NULL, smb_fname->base_name, ctx,
701 &num_streams, &streams);
702 /* There is always one stream, ::$DATA. */
703 if (NT_STATUS_IS_OK(status) && num_streams > 1) {
704 file_status &= ~NO_SUBSTREAMS;
706 TALLOC_FREE(streams);
707 SSVAL(p,2,file_status);
710 SCVAL(p,0,fsp->is_directory ? 1 : 0);
712 if (flags & EXTENDED_RESPONSE_REQUIRED) {
715 if (fsp->is_directory ||
716 can_write_to_file(conn, smb_fname)) {
717 perms = FILE_GENERIC_ALL;
719 perms = FILE_GENERIC_READ|FILE_EXECUTE;
724 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n",
725 fsp->fnum, smb_fname_str_dbg(smb_fname)));
729 END_PROFILE(SMBntcreateX);
733 /****************************************************************************
734 Reply to a NT_TRANSACT_CREATE call to open a pipe.
735 ****************************************************************************/
737 static void do_nt_transact_create_pipe(connection_struct *conn,
738 struct smb_request *req,
739 uint16 **ppsetup, uint32 setup_count,
740 char **ppparams, uint32 parameter_count,
741 char **ppdata, uint32 data_count)
744 char *params = *ppparams;
750 TALLOC_CTX *ctx = talloc_tos();
753 * Ensure minimum number of parameters sent.
756 if(parameter_count < 54) {
757 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
758 reply_doserror(req, ERRDOS, ERRnoaccess);
762 flags = IVAL(params,0);
764 srvstr_get_path(ctx, params, req->flags2, &fname, params+53,
765 parameter_count-53, STR_TERMINATE,
767 if (!NT_STATUS_IS_OK(status)) {
768 reply_nterror(req, status);
772 nt_open_pipe(fname, conn, req, &pnum);
779 /* Realloc the size of parameters and data we will return */
780 if (flags & EXTENDED_RESPONSE_REQUIRED) {
781 /* Extended response is 32 more byyes. */
786 params = nttrans_realloc(ppparams, param_len);
788 reply_doserror(req, ERRDOS, ERRnomem);
793 SCVAL(p,0,NO_OPLOCK_RETURN);
798 SIVAL(p,0,FILE_WAS_OPENED);
802 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
805 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
807 SSVAL(p,2, 0x5FF); /* ? */
810 if (flags & EXTENDED_RESPONSE_REQUIRED) {
812 SIVAL(p,0,FILE_GENERIC_ALL);
814 * For pipes W2K3 seems to return
816 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
818 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
821 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
823 /* Send the required number of replies */
824 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
829 /****************************************************************************
830 Internal fn to set security descriptors.
831 ****************************************************************************/
833 static NTSTATUS set_sd(files_struct *fsp, uint8 *data, uint32 sd_len,
834 uint32 security_info_sent)
836 SEC_DESC *psd = NULL;
839 if (sd_len == 0 || !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 /* Convert all the generic bits. */
857 security_acl_map_generic(psd->dacl, &file_generic_mapping);
858 security_acl_map_generic(psd->sacl, &file_generic_mapping);
860 if (DEBUGLEVEL >= 10) {
861 DEBUG(10,("set_sd for file %s\n", fsp_str_dbg(fsp)));
862 NDR_PRINT_DEBUG(security_descriptor, psd);
865 status = SMB_VFS_FSET_NT_ACL(fsp, security_info_sent, psd);
872 /****************************************************************************
873 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
874 ****************************************************************************/
876 struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
878 struct ea_list *ea_list_head = NULL;
885 while (offset + 4 <= data_size) {
886 size_t next_offset = IVAL(pdata,offset);
887 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
893 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
894 if (next_offset == 0) {
897 offset += next_offset;
903 /****************************************************************************
904 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
905 ****************************************************************************/
907 static void call_nt_transact_create(connection_struct *conn,
908 struct smb_request *req,
909 uint16 **ppsetup, uint32 setup_count,
910 char **ppparams, uint32 parameter_count,
911 char **ppdata, uint32 data_count,
912 uint32 max_data_count)
914 struct smb_filename *smb_fname = NULL;
916 char *params = *ppparams;
917 char *data = *ppdata;
918 /* Breakout the oplock request bits so we can set the reply bits separately. */
920 SMB_OFF_T file_len = 0;
922 files_struct *fsp = NULL;
926 uint32 file_attributes;
928 uint32 create_disposition;
929 uint32 create_options;
931 struct security_descriptor *sd = NULL;
934 struct timespec create_timespec;
935 struct timespec c_timespec;
936 struct timespec a_timespec;
937 struct timespec m_timespec;
938 struct timespec write_time_ts;
939 struct ea_list *ea_list = NULL;
942 uint64_t allocation_size;
944 uint8_t oplock_granted;
945 struct case_semantics_state *case_state = NULL;
946 TALLOC_CTX *ctx = talloc_tos();
948 DEBUG(5,("call_nt_transact_create\n"));
951 * If it's an IPC, use the pipe handler.
955 if (lp_nt_pipe_support()) {
956 do_nt_transact_create_pipe(
958 ppsetup, setup_count,
959 ppparams, parameter_count,
963 reply_doserror(req, ERRDOS, ERRnoaccess);
968 * Ensure minimum number of parameters sent.
971 if(parameter_count < 54) {
972 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
973 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
977 flags = IVAL(params,0);
978 access_mask = IVAL(params,8);
979 file_attributes = IVAL(params,20);
980 share_access = IVAL(params,24);
981 create_disposition = IVAL(params,28);
982 create_options = IVAL(params,32);
983 sd_len = IVAL(params,36);
984 ea_len = IVAL(params,40);
985 root_dir_fid = (uint16)IVAL(params,4);
986 allocation_size = (uint64_t)IVAL(params,12);
987 #ifdef LARGE_SMB_OFF_T
988 allocation_size |= (((uint64_t)IVAL(params,16)) << 32);
992 * we need to remove ignored bits when they come directly from the client
993 * because we reuse some of them for internal stuff
995 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
997 /* Ensure the data_len is correct for the sd and ea values given. */
998 if ((ea_len + sd_len > data_count)
999 || (ea_len > data_count) || (sd_len > data_count)
1000 || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
1001 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
1002 "%u, data_count = %u\n", (unsigned int)ea_len,
1003 (unsigned int)sd_len, (unsigned int)data_count));
1004 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1009 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
1012 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
1014 if (!NT_STATUS_IS_OK(status)) {
1015 DEBUG(10, ("call_nt_transact_create: "
1016 "unmarshall_sec_desc failed: %s\n",
1017 nt_errstr(status)));
1018 reply_nterror(req, status);
1024 if (!lp_ea_support(SNUM(conn))) {
1025 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
1026 "EA's not supported.\n",
1027 (unsigned int)ea_len));
1028 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
1033 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
1034 "too small (should be more than 10)\n",
1035 (unsigned int)ea_len ));
1036 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1040 /* We have already checked that ea_len <= data_count here. */
1041 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
1043 if (ea_list == NULL) {
1044 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1049 srvstr_get_path(ctx, params, req->flags2, &fname,
1050 params+53, parameter_count-53,
1051 STR_TERMINATE, &status);
1052 if (!NT_STATUS_IS_OK(status)) {
1053 reply_nterror(req, status);
1057 case_state = set_posix_case_semantics(ctx, conn);
1059 reply_nterror(req, NT_STATUS_NO_MEMORY);
1063 status = filename_convert(ctx,
1065 req->flags2 & FLAGS2_DFS_PATHNAMES,
1071 TALLOC_FREE(case_state);
1073 if (!NT_STATUS_IS_OK(status)) {
1074 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1075 reply_botherror(req,
1076 NT_STATUS_PATH_NOT_COVERED,
1077 ERRSRV, ERRbadpath);
1080 reply_nterror(req, status);
1084 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1085 if (oplock_request) {
1086 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
1091 * Bug #6898 - clients using Windows opens should
1092 * never be able to set this attribute into the
1095 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
1097 status = SMB_VFS_CREATE_FILE(
1100 root_dir_fid, /* root_dir_fid */
1101 smb_fname, /* fname */
1102 access_mask, /* access_mask */
1103 share_access, /* share_access */
1104 create_disposition, /* create_disposition*/
1105 create_options, /* create_options */
1106 file_attributes, /* file_attributes */
1107 oplock_request, /* oplock_request */
1108 allocation_size, /* allocation_size */
1110 ea_list, /* ea_list */
1114 if(!NT_STATUS_IS_OK(status)) {
1115 if (open_was_deferred(req->mid)) {
1116 /* We have re-scheduled this call, no error. */
1119 reply_openerror(req, status);
1123 /* Ensure we're pointing at the correct stat struct. */
1124 TALLOC_FREE(smb_fname);
1125 smb_fname = fsp->fsp_name;
1128 * If the caller set the extended oplock request bit
1129 * and we granted one (by whatever means) - set the
1130 * correct bit for extended oplock reply.
1133 if (oplock_request &&
1134 (lp_fake_oplocks(SNUM(conn))
1135 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
1138 * Exclusive oplock granted
1141 if (flags & REQUEST_BATCH_OPLOCK) {
1142 oplock_granted = BATCH_OPLOCK_RETURN;
1144 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
1146 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1147 oplock_granted = LEVEL_II_OPLOCK_RETURN;
1149 oplock_granted = NO_OPLOCK_RETURN;
1152 file_len = smb_fname->st.st_ex_size;
1154 /* Realloc the size of parameters and data we will return */
1155 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1156 /* Extended response is 32 more byyes. */
1161 params = nttrans_realloc(ppparams, param_len);
1162 if(params == NULL) {
1163 reply_doserror(req, ERRDOS, ERRnomem);
1168 SCVAL(p, 0, oplock_granted);
1171 SSVAL(p,0,fsp->fnum);
1173 if ((create_disposition == FILE_SUPERSEDE)
1174 && (info == FILE_WAS_OVERWRITTEN)) {
1175 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1181 fattr = dos_mode(conn, smb_fname);
1183 fattr = FILE_ATTRIBUTE_NORMAL;
1186 /* Deal with other possible opens having a modified
1188 ZERO_STRUCT(write_time_ts);
1189 get_file_infos(fsp->file_id, NULL, &write_time_ts);
1190 if (!null_timespec(write_time_ts)) {
1191 update_stat_ex_mtime(&smb_fname->st, write_time_ts);
1195 create_timespec = get_create_timespec(conn, fsp, smb_fname);
1196 a_timespec = smb_fname->st.st_ex_atime;
1197 m_timespec = smb_fname->st.st_ex_mtime;
1198 c_timespec = get_change_timespec(conn, fsp, smb_fname);
1200 if (lp_dos_filetime_resolution(SNUM(conn))) {
1201 dos_filetime_timespec(&create_timespec);
1202 dos_filetime_timespec(&a_timespec);
1203 dos_filetime_timespec(&m_timespec);
1204 dos_filetime_timespec(&c_timespec);
1207 put_long_date_timespec(conn->ts_res, p, create_timespec); /* create time. */
1209 put_long_date_timespec(conn->ts_res, p, a_timespec); /* access time */
1211 put_long_date_timespec(conn->ts_res, p, m_timespec); /* write time */
1213 put_long_date_timespec(conn->ts_res, p, c_timespec); /* change time */
1215 SIVAL(p,0,fattr); /* File Attributes. */
1217 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn, fsp, &smb_fname->st));
1219 SOFF_T(p,0,file_len);
1221 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1225 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1227 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1230 if (fsp->is_directory ||
1231 can_write_to_file(conn, smb_fname)) {
1232 perms = FILE_GENERIC_ALL;
1234 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1239 DEBUG(5,("call_nt_transact_create: open name = %s\n",
1240 smb_fname_str_dbg(smb_fname)));
1242 /* Send the required number of replies */
1243 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1248 /****************************************************************************
1249 Reply to a NT CANCEL request.
1250 conn POINTER CAN BE NULL HERE !
1251 ****************************************************************************/
1253 void reply_ntcancel(struct smb_request *req)
1256 * Go through and cancel any pending change notifies.
1259 START_PROFILE(SMBntcancel);
1260 srv_cancel_sign_response(smbd_server_conn);
1261 remove_pending_change_notify_requests_by_mid(req->mid);
1262 remove_pending_lock_requests_by_mid(req->mid);
1264 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", req->mid));
1266 END_PROFILE(SMBntcancel);
1270 /****************************************************************************
1272 ****************************************************************************/
1274 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
1275 connection_struct *conn,
1276 struct smb_request *req,
1277 struct smb_filename *smb_fname_src,
1278 struct smb_filename *smb_fname_dst,
1281 files_struct *fsp1,*fsp2;
1285 NTSTATUS status = NT_STATUS_OK;
1288 if (!CAN_WRITE(conn)) {
1289 status = NT_STATUS_MEDIA_WRITE_PROTECTED;
1293 /* Source must already exist. */
1294 if (!VALID_STAT(smb_fname_src->st)) {
1295 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
1299 /* Ensure attributes match. */
1300 fattr = dos_mode(conn, smb_fname_src);
1301 if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
1302 status = NT_STATUS_NO_SUCH_FILE;
1306 /* Disallow if dst file already exists. */
1307 if (VALID_STAT(smb_fname_dst->st)) {
1308 status = NT_STATUS_OBJECT_NAME_COLLISION;
1312 /* No links from a directory. */
1313 if (S_ISDIR(smb_fname_src->st.st_ex_mode)) {
1314 status = NT_STATUS_FILE_IS_A_DIRECTORY;
1318 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1319 smb_fname_str_dbg(smb_fname_src),
1320 smb_fname_str_dbg(smb_fname_dst)));
1322 status = SMB_VFS_CREATE_FILE(
1325 0, /* root_dir_fid */
1326 smb_fname_src, /* fname */
1327 FILE_READ_DATA, /* access_mask */
1328 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1330 FILE_OPEN, /* create_disposition*/
1331 0, /* create_options */
1332 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
1333 NO_OPLOCK, /* oplock_request */
1334 0, /* allocation_size */
1340 if (!NT_STATUS_IS_OK(status)) {
1344 status = SMB_VFS_CREATE_FILE(
1347 0, /* root_dir_fid */
1348 smb_fname_dst, /* fname */
1349 FILE_WRITE_DATA, /* access_mask */
1350 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1352 FILE_CREATE, /* create_disposition*/
1353 0, /* create_options */
1354 fattr, /* file_attributes */
1355 NO_OPLOCK, /* oplock_request */
1356 0, /* allocation_size */
1362 if (!NT_STATUS_IS_OK(status)) {
1363 close_file(NULL, fsp1, ERROR_CLOSE);
1367 if (smb_fname_src->st.st_ex_size) {
1368 ret = vfs_transfer_file(fsp1, fsp2, smb_fname_src->st.st_ex_size);
1372 * As we are opening fsp1 read-only we only expect
1373 * an error on close on fsp2 if we are out of space.
1374 * Thus we don't look at the error return from the
1377 close_file(NULL, fsp1, NORMAL_CLOSE);
1379 /* Ensure the modtime is set correctly on the destination file. */
1380 set_close_write_time(fsp2, smb_fname_src->st.st_ex_mtime);
1382 status = close_file(NULL, fsp2, NORMAL_CLOSE);
1384 /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
1385 creates the file. This isn't the correct thing to do in the copy
1387 if (!parent_dirname(talloc_tos(), smb_fname_dst->base_name, &parent,
1389 status = NT_STATUS_NO_MEMORY;
1392 file_set_dosmode(conn, smb_fname_dst, fattr, parent, false);
1393 TALLOC_FREE(parent);
1395 if (ret < (SMB_OFF_T)smb_fname_src->st.st_ex_size) {
1396 status = NT_STATUS_DISK_FULL;
1400 if (!NT_STATUS_IS_OK(status)) {
1401 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1402 nt_errstr(status), smb_fname_str_dbg(smb_fname_src),
1403 smb_fname_str_dbg(smb_fname_dst)));
1409 /****************************************************************************
1410 Reply to a NT rename request.
1411 ****************************************************************************/
1413 void reply_ntrename(struct smb_request *req)
1415 connection_struct *conn = req->conn;
1416 struct smb_filename *smb_fname_old = NULL;
1417 struct smb_filename *smb_fname_new = NULL;
1418 char *oldname = NULL;
1419 char *newname = NULL;
1422 bool src_has_wcard = False;
1423 bool dest_has_wcard = False;
1425 uint32_t ucf_flags_src = 0;
1426 uint32_t ucf_flags_dst = 0;
1428 TALLOC_CTX *ctx = talloc_tos();
1430 START_PROFILE(SMBntrename);
1433 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1437 attrs = SVAL(req->vwv+0, 0);
1438 rename_type = SVAL(req->vwv+1, 0);
1440 p = (const char *)req->buf + 1;
1441 p += srvstr_get_path_req_wcard(ctx, req, &oldname, p, STR_TERMINATE,
1442 &status, &src_has_wcard);
1443 if (!NT_STATUS_IS_OK(status)) {
1444 reply_nterror(req, status);
1448 if (ms_has_wild(oldname)) {
1449 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1454 p += srvstr_get_path_req_wcard(ctx, req, &newname, p, STR_TERMINATE,
1455 &status, &dest_has_wcard);
1456 if (!NT_STATUS_IS_OK(status)) {
1457 reply_nterror(req, status);
1461 /* The newname must begin with a ':' if the oldname contains a ':'. */
1462 if (strchr_m(oldname, ':') && (newname[0] != ':')) {
1463 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1468 * If this is a rename operation, allow wildcards and save the
1469 * destination's last component.
1471 if (rename_type == RENAME_FLAG_RENAME) {
1472 ucf_flags_src = UCF_COND_ALLOW_WCARD_LCOMP;
1473 ucf_flags_dst = UCF_COND_ALLOW_WCARD_LCOMP | UCF_SAVE_LCOMP;
1476 /* rename_internals() calls unix_convert(), so don't call it here. */
1477 status = filename_convert(ctx, conn,
1478 req->flags2 & FLAGS2_DFS_PATHNAMES,
1483 if (!NT_STATUS_IS_OK(status)) {
1484 if (NT_STATUS_EQUAL(status,
1485 NT_STATUS_PATH_NOT_COVERED)) {
1486 reply_botherror(req,
1487 NT_STATUS_PATH_NOT_COVERED,
1488 ERRSRV, ERRbadpath);
1491 reply_nterror(req, status);
1495 status = filename_convert(ctx, conn,
1496 req->flags2 & FLAGS2_DFS_PATHNAMES,
1501 if (!NT_STATUS_IS_OK(status)) {
1502 if (NT_STATUS_EQUAL(status,
1503 NT_STATUS_PATH_NOT_COVERED)) {
1504 reply_botherror(req,
1505 NT_STATUS_PATH_NOT_COVERED,
1506 ERRSRV, ERRbadpath);
1509 reply_nterror(req, status);
1513 DEBUG(3,("reply_ntrename: %s -> %s\n",
1514 smb_fname_str_dbg(smb_fname_old),
1515 smb_fname_str_dbg(smb_fname_new)));
1517 switch(rename_type) {
1518 case RENAME_FLAG_RENAME:
1519 status = rename_internals(ctx, conn, req,
1520 smb_fname_old, smb_fname_new,
1521 attrs, False, src_has_wcard,
1525 case RENAME_FLAG_HARD_LINK:
1526 if (src_has_wcard || dest_has_wcard) {
1528 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1530 status = hardlink_internals(ctx, conn,
1535 case RENAME_FLAG_COPY:
1536 if (src_has_wcard || dest_has_wcard) {
1538 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1540 status = copy_internals(ctx, conn, req,
1546 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1547 status = NT_STATUS_INVALID_PARAMETER;
1550 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1554 if (!NT_STATUS_IS_OK(status)) {
1555 if (open_was_deferred(req->mid)) {
1556 /* We have re-scheduled this call. */
1560 reply_nterror(req, status);
1564 reply_outbuf(req, 0, 0);
1566 END_PROFILE(SMBntrename);
1570 /****************************************************************************
1571 Reply to a notify change - queue the request and
1572 don't allow a directory to be opened.
1573 ****************************************************************************/
1575 static void smbd_smb1_notify_reply(struct smb_request *req,
1576 NTSTATUS error_code,
1577 uint8_t *buf, size_t len)
1579 send_nt_replies(req->conn, req, error_code, (char *)buf, len, NULL, 0);
1582 static void call_nt_transact_notify_change(connection_struct *conn,
1583 struct smb_request *req,
1587 uint32 parameter_count,
1588 char **ppdata, uint32 data_count,
1589 uint32 max_data_count,
1590 uint32 max_param_count)
1592 uint16 *setup = *ppsetup;
1598 if(setup_count < 6) {
1599 reply_doserror(req, ERRDOS, ERRbadfunc);
1603 fsp = file_fsp(req, SVAL(setup,4));
1604 filter = IVAL(setup, 0);
1605 recursive = (SVAL(setup, 6) != 0) ? True : False;
1607 DEBUG(3,("call_nt_transact_notify_change\n"));
1610 reply_doserror(req, ERRDOS, ERRbadfid);
1615 char *filter_string;
1617 if (!(filter_string = notify_filter_string(NULL, filter))) {
1618 reply_nterror(req,NT_STATUS_NO_MEMORY);
1622 DEBUG(3,("call_nt_transact_notify_change: notify change "
1623 "called on %s, filter = %s, recursive = %d\n",
1624 fsp_str_dbg(fsp), filter_string, recursive));
1626 TALLOC_FREE(filter_string);
1629 if((!fsp->is_directory) || (conn != fsp->conn)) {
1630 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1634 if (fsp->notify == NULL) {
1636 status = change_notify_create(fsp, filter, recursive);
1638 if (!NT_STATUS_IS_OK(status)) {
1639 DEBUG(10, ("change_notify_create returned %s\n",
1640 nt_errstr(status)));
1641 reply_nterror(req, status);
1646 if (fsp->notify->num_changes != 0) {
1649 * We've got changes pending, respond immediately
1653 * TODO: write a torture test to check the filtering behaviour
1657 change_notify_reply(fsp->conn, req,
1661 smbd_smb1_notify_reply);
1664 * change_notify_reply() above has independently sent its
1671 * No changes pending, queue the request
1674 status = change_notify_add_request(req,
1678 smbd_smb1_notify_reply);
1679 if (!NT_STATUS_IS_OK(status)) {
1680 reply_nterror(req, status);
1685 /****************************************************************************
1686 Reply to an NT transact rename command.
1687 ****************************************************************************/
1689 static void call_nt_transact_rename(connection_struct *conn,
1690 struct smb_request *req,
1691 uint16 **ppsetup, uint32 setup_count,
1692 char **ppparams, uint32 parameter_count,
1693 char **ppdata, uint32 data_count,
1694 uint32 max_data_count)
1696 char *params = *ppparams;
1697 char *new_name = NULL;
1698 files_struct *fsp = NULL;
1699 bool dest_has_wcard = False;
1701 TALLOC_CTX *ctx = talloc_tos();
1703 if(parameter_count < 5) {
1704 reply_doserror(req, ERRDOS, ERRbadfunc);
1708 fsp = file_fsp(req, SVAL(params, 0));
1709 if (!check_fsp(conn, req, fsp)) {
1712 srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
1713 parameter_count - 4,
1714 STR_TERMINATE, &status, &dest_has_wcard);
1715 if (!NT_STATUS_IS_OK(status)) {
1716 reply_nterror(req, status);
1721 * W2K3 ignores this request as the RAW-RENAME test
1722 * demonstrates, so we do.
1724 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1726 DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1727 fsp_str_dbg(fsp), new_name));
1732 /******************************************************************************
1733 Fake up a completely empty SD.
1734 *******************************************************************************/
1736 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
1740 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1742 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1743 return NT_STATUS_NO_MEMORY;
1746 return NT_STATUS_OK;
1749 /****************************************************************************
1750 Reply to query a security descriptor.
1751 ****************************************************************************/
1753 static void call_nt_transact_query_security_desc(connection_struct *conn,
1754 struct smb_request *req,
1758 uint32 parameter_count,
1761 uint32 max_data_count)
1763 char *params = *ppparams;
1764 char *data = *ppdata;
1765 SEC_DESC *psd = NULL;
1767 uint32 security_info_wanted;
1768 files_struct *fsp = NULL;
1772 if(parameter_count < 8) {
1773 reply_doserror(req, ERRDOS, ERRbadfunc);
1777 fsp = file_fsp(req, SVAL(params,0));
1779 reply_doserror(req, ERRDOS, ERRbadfid);
1783 security_info_wanted = IVAL(params,4);
1785 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, "
1786 "info_wanted = 0x%x\n", fsp_str_dbg(fsp),
1787 (unsigned int)security_info_wanted));
1789 params = nttrans_realloc(ppparams, 4);
1790 if(params == NULL) {
1791 reply_doserror(req, ERRDOS, ERRnomem);
1796 * Get the permissions to return.
1799 if (!lp_nt_acl_support(SNUM(conn))) {
1800 status = get_null_nt_acl(talloc_tos(), &psd);
1802 status = SMB_VFS_FGET_NT_ACL(
1803 fsp, security_info_wanted, &psd);
1805 if (!NT_STATUS_IS_OK(status)) {
1806 reply_nterror(req, status);
1810 /* If the SACL/DACL is NULL, but was requested, we mark that it is
1811 * present in the reply to match Windows behavior */
1812 if (psd->sacl == NULL &&
1813 security_info_wanted & SACL_SECURITY_INFORMATION)
1814 psd->type |= SEC_DESC_SACL_PRESENT;
1815 if (psd->dacl == NULL &&
1816 security_info_wanted & DACL_SECURITY_INFORMATION)
1817 psd->type |= SEC_DESC_DACL_PRESENT;
1819 sd_size = ndr_size_security_descriptor(psd, NULL, 0);
1821 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
1823 if (DEBUGLEVEL >= 10) {
1824 DEBUG(10,("call_nt_transact_query_security_desc for file %s\n",
1826 NDR_PRINT_DEBUG(security_descriptor, psd);
1829 SIVAL(params,0,(uint32)sd_size);
1831 if (max_data_count < sd_size) {
1832 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
1833 params, 4, *ppdata, 0);
1838 * Allocate the data we will point this at.
1841 data = nttrans_realloc(ppdata, sd_size);
1843 reply_doserror(req, ERRDOS, ERRnomem);
1847 status = marshall_sec_desc(talloc_tos(), psd,
1848 &blob.data, &blob.length);
1850 if (!NT_STATUS_IS_OK(status)) {
1851 reply_nterror(req, status);
1855 SMB_ASSERT(sd_size == blob.length);
1856 memcpy(data, blob.data, sd_size);
1858 send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
1863 /****************************************************************************
1864 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
1865 ****************************************************************************/
1867 static void call_nt_transact_set_security_desc(connection_struct *conn,
1868 struct smb_request *req,
1872 uint32 parameter_count,
1875 uint32 max_data_count)
1877 char *params= *ppparams;
1878 char *data = *ppdata;
1879 files_struct *fsp = NULL;
1880 uint32 security_info_sent = 0;
1883 if(parameter_count < 8) {
1884 reply_doserror(req, ERRDOS, ERRbadfunc);
1888 if((fsp = file_fsp(req, SVAL(params,0))) == NULL) {
1889 reply_doserror(req, ERRDOS, ERRbadfid);
1893 if(!lp_nt_acl_support(SNUM(conn))) {
1897 security_info_sent = IVAL(params,4);
1899 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n",
1900 fsp_str_dbg(fsp), (unsigned int)security_info_sent));
1902 if (data_count == 0) {
1903 reply_doserror(req, ERRDOS, ERRnoaccess);
1907 status = set_sd(fsp, (uint8 *)data, data_count, security_info_sent);
1909 if (!NT_STATUS_IS_OK(status)) {
1910 reply_nterror(req, status);
1915 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1919 /****************************************************************************
1921 ****************************************************************************/
1923 static void call_nt_transact_ioctl(connection_struct *conn,
1924 struct smb_request *req,
1925 uint16 **ppsetup, uint32 setup_count,
1926 char **ppparams, uint32 parameter_count,
1927 char **ppdata, uint32 data_count,
1928 uint32 max_data_count)
1935 char *pdata = *ppdata;
1937 if (setup_count != 8) {
1938 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
1939 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1943 function = IVAL(*ppsetup, 0);
1944 fidnum = SVAL(*ppsetup, 4);
1945 isFSctl = CVAL(*ppsetup, 6);
1946 compfilter = CVAL(*ppsetup, 7);
1948 DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
1949 function, fidnum, isFSctl, compfilter));
1951 fsp=file_fsp(req, fidnum);
1952 /* this check is done in each implemented function case for now
1953 because I don't want to break anything... --metze
1954 FSP_BELONGS_CONN(fsp,conn);*/
1956 SMB_PERFCOUNT_SET_IOCTL(&req->pcd, function);
1959 case FSCTL_SET_SPARSE:
1960 /* pretend this succeeded - tho strictly we should
1961 mark the file sparse (if the local fs supports it)
1962 so we can know if we need to pre-allocate or not */
1964 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
1965 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1968 case FSCTL_CREATE_OR_GET_OBJECT_ID:
1970 unsigned char objid[16];
1972 /* This should return the object-id on this file.
1973 * I think I'll make this be the inode+dev. JRA.
1976 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
1978 if (!fsp_belongs_conn(conn, req, fsp)) {
1983 pdata = nttrans_realloc(ppdata, data_count);
1984 if (pdata == NULL) {
1985 reply_nterror(req, NT_STATUS_NO_MEMORY);
1989 /* For backwards compatibility only store the dev/inode. */
1990 push_file_id_16(pdata, &fsp->file_id);
1991 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
1992 push_file_id_16(pdata+32, &fsp->file_id);
1993 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
1998 case FSCTL_GET_REPARSE_POINT:
1999 /* pretend this fail - my winXP does it like this
2003 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2004 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
2007 case FSCTL_SET_REPARSE_POINT:
2008 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
2012 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2013 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
2016 case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
2019 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
2020 * and return their volume names. If max_data_count is 16, then it is just
2021 * asking for the number of volumes and length of the combined names.
2023 * pdata is the data allocated by our caller, but that uses
2024 * total_data_count (which is 0 in our case) rather than max_data_count.
2025 * Allocate the correct amount and return the pointer to let
2026 * it be deallocated when we return.
2028 SHADOW_COPY_DATA *shadow_data = NULL;
2029 TALLOC_CTX *shadow_mem_ctx = NULL;
2030 bool labels = False;
2031 uint32 labels_data_count = 0;
2035 if (!fsp_belongs_conn(conn, req, fsp)) {
2039 if (max_data_count < 16) {
2040 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
2042 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2046 if (max_data_count > 16) {
2050 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
2051 if (shadow_mem_ctx == NULL) {
2052 DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
2053 reply_nterror(req, NT_STATUS_NO_MEMORY);
2057 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
2058 if (shadow_data == NULL) {
2059 DEBUG(0,("TALLOC_ZERO() failed!\n"));
2060 talloc_destroy(shadow_mem_ctx);
2061 reply_nterror(req, NT_STATUS_NO_MEMORY);
2065 shadow_data->mem_ctx = shadow_mem_ctx;
2068 * Call the VFS routine to actually do the work.
2070 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
2071 talloc_destroy(shadow_data->mem_ctx);
2072 if (errno == ENOSYS) {
2073 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
2074 conn->connectpath));
2075 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2078 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
2079 conn->connectpath));
2080 reply_nterror(req, NT_STATUS_UNSUCCESSFUL);
2085 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
2090 data_count = 12+labels_data_count+4;
2093 if (max_data_count<data_count) {
2094 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
2095 max_data_count,data_count));
2096 talloc_destroy(shadow_data->mem_ctx);
2097 reply_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
2101 pdata = nttrans_realloc(ppdata, data_count);
2102 if (pdata == NULL) {
2103 talloc_destroy(shadow_data->mem_ctx);
2104 reply_nterror(req, NT_STATUS_NO_MEMORY);
2110 /* num_volumes 4 bytes */
2111 SIVAL(pdata,0,shadow_data->num_volumes);
2114 /* num_labels 4 bytes */
2115 SIVAL(pdata,4,shadow_data->num_volumes);
2118 /* needed_data_count 4 bytes */
2119 SIVAL(pdata, 8, labels_data_count+4);
2123 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
2124 shadow_data->num_volumes, fsp_str_dbg(fsp)));
2125 if (labels && shadow_data->labels) {
2126 for (i=0;i<shadow_data->num_volumes;i++) {
2127 srvstr_push(pdata, req->flags2,
2128 cur_pdata, shadow_data->labels[i],
2129 2*sizeof(SHADOW_COPY_LABEL),
2130 STR_UNICODE|STR_TERMINATE);
2131 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
2132 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
2136 talloc_destroy(shadow_data->mem_ctx);
2138 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
2144 case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
2146 /* pretend this succeeded -
2148 * we have to send back a list with all files owned by this SID
2150 * but I have to check that --metze
2154 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
2156 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
2158 if (!fsp_belongs_conn(conn, req, fsp)) {
2162 /* unknown 4 bytes: this is not the length of the sid :-( */
2163 /*unknown = IVAL(pdata,0);*/
2165 sid_parse(pdata+4,sid_len,&sid);
2166 DEBUGADD(10, ("for SID: %s\n", sid_string_dbg(&sid)));
2168 if (!sid_to_uid(&sid, &uid)) {
2169 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2170 sid_string_dbg(&sid),
2171 (unsigned long)sid_len));
2175 /* we can take a look at the find source :-)
2177 * find ./ -uid $uid -name '*' is what we need here
2180 * and send 4bytes len and then NULL terminated unicode strings
2183 * but I don't know how to deal with the paged results
2184 * (maybe we can hang the result anywhere in the fsp struct)
2186 * we don't send all files at once
2187 * and at the next we should *not* start from the beginning,
2188 * so we have to cache the result
2193 /* this works for now... */
2194 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2198 if (!logged_ioctl_message) {
2199 logged_ioctl_message = true; /* Only print this once... */
2200 DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
2205 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2209 #ifdef HAVE_SYS_QUOTAS
2210 /****************************************************************************
2211 Reply to get user quota
2212 ****************************************************************************/
2214 static void call_nt_transact_get_user_quota(connection_struct *conn,
2215 struct smb_request *req,
2219 uint32 parameter_count,
2222 uint32 max_data_count)
2224 NTSTATUS nt_status = NT_STATUS_OK;
2225 char *params = *ppparams;
2226 char *pdata = *ppdata;
2228 int data_len=0,param_len=0;
2231 files_struct *fsp = NULL;
2235 bool start_enum = True;
2236 SMB_NTQUOTA_STRUCT qt;
2237 SMB_NTQUOTA_LIST *tmp_list;
2238 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2243 if (conn->server_info->utok.uid != 0) {
2244 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2245 "[%s]\n", lp_servicename(SNUM(conn)),
2246 conn->server_info->unix_name));
2247 reply_doserror(req, ERRDOS, ERRnoaccess);
2252 * Ensure minimum number of parameters sent.
2255 if (parameter_count < 4) {
2256 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2257 reply_doserror(req, ERRDOS, ERRinvalidparam);
2261 /* maybe we can check the quota_fnum */
2262 fsp = file_fsp(req, SVAL(params,0));
2263 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2264 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2265 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2269 /* the NULL pointer checking for fsp->fake_file_handle->pd
2270 * is done by CHECK_NTQUOTA_HANDLE_OK()
2272 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->private_data;
2274 level = SVAL(params,2);
2276 /* unknown 12 bytes leading in params */
2279 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2280 /* seems that we should continue with the enum here --metze */
2282 if (qt_handle->quota_list!=NULL &&
2283 qt_handle->tmp_list==NULL) {
2286 free_ntquota_list(&(qt_handle->quota_list));
2288 /* Realloc the size of parameters and data we will return */
2290 params = nttrans_realloc(ppparams, param_len);
2291 if(params == NULL) {
2292 reply_doserror(req, ERRDOS, ERRnomem);
2297 SIVAL(params,0,data_len);
2304 case TRANSACT_GET_USER_QUOTA_LIST_START:
2306 if (qt_handle->quota_list==NULL &&
2307 qt_handle->tmp_list==NULL) {
2311 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2312 reply_doserror(req, ERRSRV, ERRerror);
2316 /* Realloc the size of parameters and data we will return */
2318 params = nttrans_realloc(ppparams, param_len);
2319 if(params == NULL) {
2320 reply_doserror(req, ERRDOS, ERRnomem);
2324 /* we should not trust the value in max_data_count*/
2325 max_data_count = MIN(max_data_count,2048);
2327 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2329 reply_doserror(req, ERRDOS, ERRnomem);
2335 /* set params Size of returned Quota Data 4 bytes*/
2336 /* but set it later when we know it */
2338 /* for each entry push the data */
2341 qt_handle->tmp_list = qt_handle->quota_list;
2344 tmp_list = qt_handle->tmp_list;
2346 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2347 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2349 sid_len = ndr_size_dom_sid(
2350 &tmp_list->quotas->sid, NULL, 0);
2351 entry_len = 40 + sid_len;
2353 /* nextoffset entry 4 bytes */
2354 SIVAL(entry,0,entry_len);
2356 /* then the len of the SID 4 bytes */
2357 SIVAL(entry,4,sid_len);
2359 /* unknown data 8 bytes uint64_t */
2360 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-metze*/
2362 /* the used disk space 8 bytes uint64_t */
2363 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2365 /* the soft quotas 8 bytes uint64_t */
2366 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2368 /* the hard quotas 8 bytes uint64_t */
2369 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2371 /* and now the SID */
2372 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2375 qt_handle->tmp_list = tmp_list;
2377 /* overwrite the offset of the last entry */
2378 SIVAL(entry-entry_len,0,0);
2380 data_len = 4+qt_len;
2381 /* overwrite the params quota_data_len */
2382 SIVAL(params,0,data_len);
2386 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2388 /* unknown 4 bytes IVAL(pdata,0) */
2390 if (data_count < 8) {
2391 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2392 reply_doserror(req, ERRDOS, ERRunknownlevel);
2396 sid_len = IVAL(pdata,4);
2397 /* Ensure this is less than 1mb. */
2398 if (sid_len > (1024*1024)) {
2399 reply_doserror(req, ERRDOS, ERRnomem);
2403 if (data_count < 8+sid_len) {
2404 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2405 reply_doserror(req, ERRDOS, ERRunknownlevel);
2409 data_len = 4+40+sid_len;
2411 if (max_data_count < data_len) {
2412 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2413 max_data_count, data_len));
2415 SIVAL(params,0,data_len);
2417 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2421 sid_parse(pdata+8,sid_len,&sid);
2423 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2426 * we have to return zero's in all fields
2427 * instead of returning an error here
2432 /* Realloc the size of parameters and data we will return */
2434 params = nttrans_realloc(ppparams, param_len);
2435 if(params == NULL) {
2436 reply_doserror(req, ERRDOS, ERRnomem);
2440 pdata = nttrans_realloc(ppdata, data_len);
2442 reply_doserror(req, ERRDOS, ERRnomem);
2448 /* set params Size of returned Quota Data 4 bytes*/
2449 SIVAL(params,0,data_len);
2451 /* nextoffset entry 4 bytes */
2454 /* then the len of the SID 4 bytes */
2455 SIVAL(entry,4,sid_len);
2457 /* unknown data 8 bytes uint64_t */
2458 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-mezte*/
2460 /* the used disk space 8 bytes uint64_t */
2461 SBIG_UINT(entry,16,qt.usedspace);
2463 /* the soft quotas 8 bytes uint64_t */
2464 SBIG_UINT(entry,24,qt.softlim);
2466 /* the hard quotas 8 bytes uint64_t */
2467 SBIG_UINT(entry,32,qt.hardlim);
2469 /* and now the SID */
2470 sid_linearize(entry+40, sid_len, &sid);
2475 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2476 reply_doserror(req, ERRSRV, ERRerror);
2481 send_nt_replies(conn, req, nt_status, params, param_len,
2485 /****************************************************************************
2486 Reply to set user quota
2487 ****************************************************************************/
2489 static void call_nt_transact_set_user_quota(connection_struct *conn,
2490 struct smb_request *req,
2494 uint32 parameter_count,
2497 uint32 max_data_count)
2499 char *params = *ppparams;
2500 char *pdata = *ppdata;
2501 int data_len=0,param_len=0;
2502 SMB_NTQUOTA_STRUCT qt;
2505 files_struct *fsp = NULL;
2510 if (conn->server_info->utok.uid != 0) {
2511 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2512 "[%s]\n", lp_servicename(SNUM(conn)),
2513 conn->server_info->unix_name));
2514 reply_doserror(req, ERRDOS, ERRnoaccess);
2519 * Ensure minimum number of parameters sent.
2522 if (parameter_count < 2) {
2523 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2524 reply_doserror(req, ERRDOS, ERRinvalidparam);
2528 /* maybe we can check the quota_fnum */
2529 fsp = file_fsp(req, SVAL(params,0));
2530 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2531 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2532 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2536 if (data_count < 40) {
2537 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2538 reply_doserror(req, ERRDOS, ERRunknownlevel);
2542 /* offset to next quota record.
2543 * 4 bytes IVAL(pdata,0)
2548 sid_len = IVAL(pdata,4);
2550 if (data_count < 40+sid_len) {
2551 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2552 reply_doserror(req, ERRDOS, ERRunknownlevel);
2556 /* unknown 8 bytes in pdata
2557 * maybe its the change time in NTTIME
2560 /* the used space 8 bytes (uint64_t)*/
2561 qt.usedspace = (uint64_t)IVAL(pdata,16);
2562 #ifdef LARGE_SMB_OFF_T
2563 qt.usedspace |= (((uint64_t)IVAL(pdata,20)) << 32);
2564 #else /* LARGE_SMB_OFF_T */
2565 if ((IVAL(pdata,20) != 0)&&
2566 ((qt.usedspace != 0xFFFFFFFF)||
2567 (IVAL(pdata,20)!=0xFFFFFFFF))) {
2568 /* more than 32 bits? */
2569 reply_doserror(req, ERRDOS, ERRunknownlevel);
2572 #endif /* LARGE_SMB_OFF_T */
2574 /* the soft quotas 8 bytes (uint64_t)*/
2575 qt.softlim = (uint64_t)IVAL(pdata,24);
2576 #ifdef LARGE_SMB_OFF_T
2577 qt.softlim |= (((uint64_t)IVAL(pdata,28)) << 32);
2578 #else /* LARGE_SMB_OFF_T */
2579 if ((IVAL(pdata,28) != 0)&&
2580 ((qt.softlim != 0xFFFFFFFF)||
2581 (IVAL(pdata,28)!=0xFFFFFFFF))) {
2582 /* more than 32 bits? */
2583 reply_doserror(req, ERRDOS, ERRunknownlevel);
2586 #endif /* LARGE_SMB_OFF_T */
2588 /* the hard quotas 8 bytes (uint64_t)*/
2589 qt.hardlim = (uint64_t)IVAL(pdata,32);
2590 #ifdef LARGE_SMB_OFF_T
2591 qt.hardlim |= (((uint64_t)IVAL(pdata,36)) << 32);
2592 #else /* LARGE_SMB_OFF_T */
2593 if ((IVAL(pdata,36) != 0)&&
2594 ((qt.hardlim != 0xFFFFFFFF)||
2595 (IVAL(pdata,36)!=0xFFFFFFFF))) {
2596 /* more than 32 bits? */
2597 reply_doserror(req, ERRDOS, ERRunknownlevel);
2600 #endif /* LARGE_SMB_OFF_T */
2602 sid_parse(pdata+40,sid_len,&sid);
2603 DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid)));
2605 /* 44 unknown bytes left... */
2607 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2608 reply_doserror(req, ERRSRV, ERRerror);
2612 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2615 #endif /* HAVE_SYS_QUOTAS */
2617 static void handle_nttrans(connection_struct *conn,
2618 struct trans_state *state,
2619 struct smb_request *req)
2621 if (get_Protocol() >= PROTOCOL_NT1) {
2622 req->flags2 |= 0x40; /* IS_LONG_NAME */
2623 SSVAL(req->inbuf,smb_flg2,req->flags2);
2627 SMB_PERFCOUNT_SET_SUBOP(&req->pcd, state->call);
2629 /* Now we must call the relevant NT_TRANS function */
2630 switch(state->call) {
2631 case NT_TRANSACT_CREATE:
2633 START_PROFILE(NT_transact_create);
2634 call_nt_transact_create(
2636 &state->setup, state->setup_count,
2637 &state->param, state->total_param,
2638 &state->data, state->total_data,
2639 state->max_data_return);
2640 END_PROFILE(NT_transact_create);
2644 case NT_TRANSACT_IOCTL:
2646 START_PROFILE(NT_transact_ioctl);
2647 call_nt_transact_ioctl(
2649 &state->setup, state->setup_count,
2650 &state->param, state->total_param,
2651 &state->data, state->total_data,
2652 state->max_data_return);
2653 END_PROFILE(NT_transact_ioctl);
2657 case NT_TRANSACT_SET_SECURITY_DESC:
2659 START_PROFILE(NT_transact_set_security_desc);
2660 call_nt_transact_set_security_desc(
2662 &state->setup, state->setup_count,
2663 &state->param, state->total_param,
2664 &state->data, state->total_data,
2665 state->max_data_return);
2666 END_PROFILE(NT_transact_set_security_desc);
2670 case NT_TRANSACT_NOTIFY_CHANGE:
2672 START_PROFILE(NT_transact_notify_change);
2673 call_nt_transact_notify_change(
2675 &state->setup, state->setup_count,
2676 &state->param, state->total_param,
2677 &state->data, state->total_data,
2678 state->max_data_return,
2679 state->max_param_return);
2680 END_PROFILE(NT_transact_notify_change);
2684 case NT_TRANSACT_RENAME:
2686 START_PROFILE(NT_transact_rename);
2687 call_nt_transact_rename(
2689 &state->setup, state->setup_count,
2690 &state->param, state->total_param,
2691 &state->data, state->total_data,
2692 state->max_data_return);
2693 END_PROFILE(NT_transact_rename);
2697 case NT_TRANSACT_QUERY_SECURITY_DESC:
2699 START_PROFILE(NT_transact_query_security_desc);
2700 call_nt_transact_query_security_desc(
2702 &state->setup, state->setup_count,
2703 &state->param, state->total_param,
2704 &state->data, state->total_data,
2705 state->max_data_return);
2706 END_PROFILE(NT_transact_query_security_desc);
2710 #ifdef HAVE_SYS_QUOTAS
2711 case NT_TRANSACT_GET_USER_QUOTA:
2713 START_PROFILE(NT_transact_get_user_quota);
2714 call_nt_transact_get_user_quota(
2716 &state->setup, state->setup_count,
2717 &state->param, state->total_param,
2718 &state->data, state->total_data,
2719 state->max_data_return);
2720 END_PROFILE(NT_transact_get_user_quota);
2724 case NT_TRANSACT_SET_USER_QUOTA:
2726 START_PROFILE(NT_transact_set_user_quota);
2727 call_nt_transact_set_user_quota(
2729 &state->setup, state->setup_count,
2730 &state->param, state->total_param,
2731 &state->data, state->total_data,
2732 state->max_data_return);
2733 END_PROFILE(NT_transact_set_user_quota);
2736 #endif /* HAVE_SYS_QUOTAS */
2739 /* Error in request */
2740 DEBUG(0,("handle_nttrans: Unknown request %d in "
2741 "nttrans call\n", state->call));
2742 reply_doserror(req, ERRSRV, ERRerror);
2748 /****************************************************************************
2749 Reply to a SMBNTtrans.
2750 ****************************************************************************/
2752 void reply_nttrans(struct smb_request *req)
2754 connection_struct *conn = req->conn;
2759 uint16 function_code;
2761 struct trans_state *state;
2763 START_PROFILE(SMBnttrans);
2765 if (req->wct < 19) {
2766 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2767 END_PROFILE(SMBnttrans);
2771 pscnt = IVAL(req->vwv+9, 1);
2772 psoff = IVAL(req->vwv+11, 1);
2773 dscnt = IVAL(req->vwv+13, 1);
2774 dsoff = IVAL(req->vwv+15, 1);
2775 function_code = SVAL(req->vwv+18, 0);
2777 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2778 reply_doserror(req, ERRSRV, ERRaccess);
2779 END_PROFILE(SMBnttrans);
2783 result = allow_new_trans(conn->pending_trans, req->mid);
2784 if (!NT_STATUS_IS_OK(result)) {
2785 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
2786 reply_nterror(req, result);
2787 END_PROFILE(SMBnttrans);
2791 if ((state = TALLOC_P(conn, struct trans_state)) == NULL) {
2792 reply_doserror(req, ERRSRV, ERRaccess);
2793 END_PROFILE(SMBnttrans);
2797 state->cmd = SMBnttrans;
2799 state->mid = req->mid;
2800 state->vuid = req->vuid;
2801 state->total_data = IVAL(req->vwv+3, 1);
2803 state->total_param = IVAL(req->vwv+1, 1);
2804 state->param = NULL;
2805 state->max_data_return = IVAL(req->vwv+7, 1);
2806 state->max_param_return = IVAL(req->vwv+5, 1);
2808 /* setup count is in *words* */
2809 state->setup_count = 2*CVAL(req->vwv+17, 1);
2810 state->setup = NULL;
2811 state->call = function_code;
2813 DEBUG(10, ("num_setup=%u, "
2814 "param_total=%u, this_param=%u, max_param=%u, "
2815 "data_total=%u, this_data=%u, max_data=%u, "
2816 "param_offset=%u, data_offset=%u\n",
2817 (unsigned)state->setup_count,
2818 (unsigned)state->total_param, (unsigned)pscnt,
2819 (unsigned)state->max_param_return,
2820 (unsigned)state->total_data, (unsigned)dscnt,
2821 (unsigned)state->max_data_return,
2822 (unsigned)psoff, (unsigned)dsoff));
2825 * All nttrans messages we handle have smb_wct == 19 +
2826 * state->setup_count. Ensure this is so as a sanity check.
2829 if(req->wct != 19 + (state->setup_count/2)) {
2830 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2831 req->wct, 19 + (state->setup_count/2)));
2835 /* Don't allow more than 128mb for each value. */
2836 if ((state->total_data > (1024*1024*128)) ||
2837 (state->total_param > (1024*1024*128))) {
2838 reply_doserror(req, ERRDOS, ERRnomem);
2839 END_PROFILE(SMBnttrans);
2843 if ((dscnt > state->total_data) || (pscnt > state->total_param))
2846 if (state->total_data) {
2848 if (trans_oob(state->total_data, 0, dscnt)
2849 || trans_oob(smb_len(req->inbuf), dsoff, dscnt)) {
2853 /* Can't use talloc here, the core routines do realloc on the
2854 * params and data. */
2855 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
2856 DEBUG(0,("reply_nttrans: data malloc fail for %u "
2857 "bytes !\n", (unsigned int)state->total_data));
2859 reply_doserror(req, ERRDOS, ERRnomem);
2860 END_PROFILE(SMBnttrans);
2864 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
2867 if (state->total_param) {
2869 if (trans_oob(state->total_param, 0, pscnt)
2870 || trans_oob(smb_len(req->inbuf), psoff, pscnt)) {
2874 /* Can't use talloc here, the core routines do realloc on the
2875 * params and data. */
2876 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
2877 DEBUG(0,("reply_nttrans: param malloc fail for %u "
2878 "bytes !\n", (unsigned int)state->total_param));
2879 SAFE_FREE(state->data);
2881 reply_doserror(req, ERRDOS, ERRnomem);
2882 END_PROFILE(SMBnttrans);
2886 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
2889 state->received_data = dscnt;
2890 state->received_param = pscnt;
2892 if(state->setup_count > 0) {
2893 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2894 state->setup_count));
2897 * No overflow possible here, state->setup_count is an
2898 * unsigned int, being filled by a single byte from
2899 * CVAL(req->vwv+13, 0) above. The cast in the comparison
2900 * below is not necessary, it's here to clarify things. The
2901 * validity of req->vwv and req->wct has been checked in
2902 * init_smb_request already.
2904 if ((state->setup_count/2) + 19 > (unsigned int)req->wct) {
2908 state->setup = (uint16 *)TALLOC(state, state->setup_count);
2909 if (state->setup == NULL) {
2910 DEBUG(0,("reply_nttrans : Out of memory\n"));
2911 SAFE_FREE(state->data);
2912 SAFE_FREE(state->param);
2914 reply_doserror(req, ERRDOS, ERRnomem);
2915 END_PROFILE(SMBnttrans);
2919 memcpy(state->setup, req->vwv+19, state->setup_count);
2920 dump_data(10, (uint8 *)state->setup, state->setup_count);
2923 if ((state->received_data == state->total_data) &&
2924 (state->received_param == state->total_param)) {
2925 handle_nttrans(conn, state, req);
2926 SAFE_FREE(state->param);
2927 SAFE_FREE(state->data);
2929 END_PROFILE(SMBnttrans);
2933 DLIST_ADD(conn->pending_trans, state);
2935 /* We need to send an interim response then receive the rest
2936 of the parameter/data bytes */
2937 reply_outbuf(req, 0, 0);
2938 show_msg((char *)req->outbuf);
2939 END_PROFILE(SMBnttrans);
2944 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
2945 SAFE_FREE(state->data);
2946 SAFE_FREE(state->param);
2948 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2949 END_PROFILE(SMBnttrans);
2953 /****************************************************************************
2954 Reply to a SMBnttranss
2955 ****************************************************************************/
2957 void reply_nttranss(struct smb_request *req)
2959 connection_struct *conn = req->conn;
2960 uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
2961 struct trans_state *state;
2963 START_PROFILE(SMBnttranss);
2965 show_msg((char *)req->inbuf);
2967 if (req->wct < 18) {
2968 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2969 END_PROFILE(SMBnttranss);
2973 for (state = conn->pending_trans; state != NULL;
2974 state = state->next) {
2975 if (state->mid == req->mid) {
2980 if ((state == NULL) || (state->cmd != SMBnttrans)) {
2981 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2982 END_PROFILE(SMBnttranss);
2986 /* Revise state->total_param and state->total_data in case they have
2987 changed downwards */
2988 if (IVAL(req->vwv+1, 1) < state->total_param) {
2989 state->total_param = IVAL(req->vwv+1, 1);
2991 if (IVAL(req->vwv+3, 1) < state->total_data) {
2992 state->total_data = IVAL(req->vwv+3, 1);
2995 pcnt = IVAL(req->vwv+5, 1);
2996 poff = IVAL(req->vwv+7, 1);
2997 pdisp = IVAL(req->vwv+9, 1);
2999 dcnt = IVAL(req->vwv+11, 1);
3000 doff = IVAL(req->vwv+13, 1);
3001 ddisp = IVAL(req->vwv+15, 1);
3003 state->received_param += pcnt;
3004 state->received_data += dcnt;
3006 if ((state->received_data > state->total_data) ||
3007 (state->received_param > state->total_param))
3011 if (trans_oob(state->total_param, pdisp, pcnt)
3012 || trans_oob(smb_len(req->inbuf), poff, pcnt)) {
3015 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,pcnt);
3019 if (trans_oob(state->total_data, ddisp, dcnt)
3020 || trans_oob(smb_len(req->inbuf), doff, dcnt)) {
3023 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
3026 if ((state->received_param < state->total_param) ||
3027 (state->received_data < state->total_data)) {
3028 END_PROFILE(SMBnttranss);
3032 handle_nttrans(conn, state, req);
3034 DLIST_REMOVE(conn->pending_trans, state);
3035 SAFE_FREE(state->data);
3036 SAFE_FREE(state->param);
3038 END_PROFILE(SMBnttranss);
3043 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
3044 DLIST_REMOVE(conn->pending_trans, state);
3045 SAFE_FREE(state->data);
3046 SAFE_FREE(state->param);
3048 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3049 END_PROFILE(SMBnttranss);