2 Unix SMB/CIFS implementation.
3 SMB NT transaction handling
4 Copyright (C) Jeremy Allison 1994-2007
5 Copyright (C) Stefan (metze) Metzmacher 2003
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "system/filesys.h"
23 #include "smbd/smbd.h"
24 #include "smbd/globals.h"
25 #include "fake_file.h"
26 #include "../libcli/security/security.h"
27 #include "../librpc/gen_ndr/ndr_security.h"
28 #include "passdb/lookup_sid.h"
30 extern const struct generic_mapping file_generic_mapping;
32 static char *nttrans_realloc(char **ptr, size_t size)
35 smb_panic("nttrans_realloc() called with NULL ptr");
38 *ptr = (char *)SMB_REALLOC(*ptr, size);
42 memset(*ptr,'\0',size);
46 /****************************************************************************
47 Send the required number of replies back.
48 We assume all fields other than the data fields are
49 set correctly for the type of call.
50 HACK ! Always assumes smb_setup field is zero.
51 ****************************************************************************/
53 void send_nt_replies(connection_struct *conn,
54 struct smb_request *req, NTSTATUS nt_error,
55 char *params, int paramsize,
56 char *pdata, int datasize)
58 int data_to_send = datasize;
59 int params_to_send = paramsize;
63 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
64 int alignment_offset = 1;
65 int data_alignment_offset = 0;
66 struct smbd_server_connection *sconn = req->sconn;
67 int max_send = sconn->smb1.sessions.max_send;
70 * If there genuinely are no parameters or data to send just send
74 if(params_to_send == 0 && data_to_send == 0) {
75 reply_outbuf(req, 18, 0);
76 if (NT_STATUS_V(nt_error)) {
77 error_packet_set((char *)req->outbuf,
81 show_msg((char *)req->outbuf);
82 if (!srv_send_smb(sconn,
85 IS_CONN_ENCRYPTED(conn),
87 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
89 TALLOC_FREE(req->outbuf);
94 * When sending params and data ensure that both are nicely aligned.
95 * Only do this alignment when there is also data to send - else
96 * can cause NT redirector problems.
99 if (((params_to_send % 4) != 0) && (data_to_send != 0)) {
100 data_alignment_offset = 4 - (params_to_send % 4);
104 * Space is bufsize minus Netbios over TCP header minus SMB header.
105 * The alignment_offset is to align the param bytes on a four byte
106 * boundary (2 bytes for data len, one byte pad).
107 * NT needs this to work correctly.
110 useable_space = max_send - (smb_size
113 + data_alignment_offset);
115 if (useable_space < 0) {
116 char *msg = talloc_asprintf(
118 "send_nt_replies failed sanity useable_space = %d!!!",
120 DEBUG(0, ("%s\n", msg));
121 exit_server_cleanly(msg);
124 while (params_to_send || data_to_send) {
127 * Calculate whether we will totally or partially fill this packet.
130 total_sent_thistime = params_to_send + data_to_send;
133 * We can never send more than useable_space.
136 total_sent_thistime = MIN(total_sent_thistime, useable_space);
138 reply_outbuf(req, 18,
139 total_sent_thistime + alignment_offset
140 + data_alignment_offset);
143 * We might have had SMBnttranss in req->inbuf, fix that.
145 SCVAL(req->outbuf, smb_com, SMBnttrans);
148 * Set total params and data to be sent.
151 SIVAL(req->outbuf,smb_ntr_TotalParameterCount,paramsize);
152 SIVAL(req->outbuf,smb_ntr_TotalDataCount,datasize);
155 * Calculate how many parameters and data we can fit into
156 * this packet. Parameters get precedence.
159 params_sent_thistime = MIN(params_to_send,useable_space);
160 data_sent_thistime = useable_space - params_sent_thistime;
161 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
163 SIVAL(req->outbuf, smb_ntr_ParameterCount,
164 params_sent_thistime);
166 if(params_sent_thistime == 0) {
167 SIVAL(req->outbuf,smb_ntr_ParameterOffset,0);
168 SIVAL(req->outbuf,smb_ntr_ParameterDisplacement,0);
171 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
172 * parameter bytes, however the first 4 bytes of outbuf are
173 * the Netbios over TCP header. Thus use smb_base() to subtract
174 * them from the calculation.
177 SIVAL(req->outbuf,smb_ntr_ParameterOffset,
178 ((smb_buf(req->outbuf)+alignment_offset)
179 - smb_base(req->outbuf)));
181 * Absolute displacement of param bytes sent in this packet.
184 SIVAL(req->outbuf, smb_ntr_ParameterDisplacement,
189 * Deal with the data portion.
192 SIVAL(req->outbuf, smb_ntr_DataCount, data_sent_thistime);
194 if(data_sent_thistime == 0) {
195 SIVAL(req->outbuf,smb_ntr_DataOffset,0);
196 SIVAL(req->outbuf,smb_ntr_DataDisplacement, 0);
199 * The offset of the data bytes is the offset of the
200 * parameter bytes plus the number of parameters being sent this time.
203 SIVAL(req->outbuf, smb_ntr_DataOffset,
204 ((smb_buf(req->outbuf)+alignment_offset) -
205 smb_base(req->outbuf))
206 + params_sent_thistime + data_alignment_offset);
207 SIVAL(req->outbuf,smb_ntr_DataDisplacement, pd - pdata);
211 * Copy the param bytes into the packet.
214 if(params_sent_thistime) {
215 if (alignment_offset != 0) {
216 memset(smb_buf(req->outbuf), 0,
219 memcpy((smb_buf(req->outbuf)+alignment_offset), pp,
220 params_sent_thistime);
224 * Copy in the data bytes
227 if(data_sent_thistime) {
228 if (data_alignment_offset != 0) {
229 memset((smb_buf(req->outbuf)+alignment_offset+
230 params_sent_thistime), 0,
231 data_alignment_offset);
233 memcpy(smb_buf(req->outbuf)+alignment_offset
234 +params_sent_thistime+data_alignment_offset,
235 pd,data_sent_thistime);
238 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
239 params_sent_thistime, data_sent_thistime, useable_space));
240 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
241 params_to_send, data_to_send, paramsize, datasize));
243 if (NT_STATUS_V(nt_error)) {
244 error_packet_set((char *)req->outbuf,
249 /* Send the packet */
250 show_msg((char *)req->outbuf);
251 if (!srv_send_smb(sconn,
254 IS_CONN_ENCRYPTED(conn),
256 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
259 TALLOC_FREE(req->outbuf);
261 pp += params_sent_thistime;
262 pd += data_sent_thistime;
264 params_to_send -= params_sent_thistime;
265 data_to_send -= data_sent_thistime;
271 if(params_to_send < 0 || data_to_send < 0) {
272 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
273 params_to_send, data_to_send));
274 exit_server_cleanly("send_nt_replies: internal error");
279 /****************************************************************************
280 Reply to an NT create and X call on a pipe
281 ****************************************************************************/
283 static void nt_open_pipe(char *fname, connection_struct *conn,
284 struct smb_request *req, int *ppnum)
289 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
291 /* Strip \\ off the name. */
294 status = open_np_file(req, fname, &fsp);
295 if (!NT_STATUS_IS_OK(status)) {
296 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
297 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
301 reply_nterror(req, status);
309 /****************************************************************************
310 Reply to an NT create and X call for pipes.
311 ****************************************************************************/
313 static void do_ntcreate_pipe_open(connection_struct *conn,
314 struct smb_request *req)
319 uint32 flags = IVAL(req->vwv+3, 1);
320 TALLOC_CTX *ctx = talloc_tos();
322 srvstr_pull_req_talloc(ctx, req, &fname, req->buf, STR_TERMINATE);
325 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
329 nt_open_pipe(fname, conn, req, &pnum);
337 * Deal with pipe return.
340 if (flags & EXTENDED_RESPONSE_REQUIRED) {
341 /* This is very strange. We
342 * return 50 words, but only set
343 * the wcnt to 42 ? It's definately
344 * what happens on the wire....
346 reply_outbuf(req, 50, 0);
347 SCVAL(req->outbuf,smb_wct,42);
349 reply_outbuf(req, 34, 0);
352 p = (char *)req->outbuf + smb_vwv2;
356 SIVAL(p,0,FILE_WAS_OPENED);
359 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
362 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
364 SSVAL(p,2, 0x5FF); /* ? */
367 if (flags & EXTENDED_RESPONSE_REQUIRED) {
369 SIVAL(p,0,FILE_GENERIC_ALL);
371 * For pipes W2K3 seems to return
373 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
375 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
378 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
383 struct case_semantics_state {
384 connection_struct *conn;
387 bool short_case_preserve;
390 /****************************************************************************
391 Restore case semantics.
392 ****************************************************************************/
394 static int restore_case_semantics(struct case_semantics_state *state)
396 state->conn->case_sensitive = state->case_sensitive;
397 state->conn->case_preserve = state->case_preserve;
398 state->conn->short_case_preserve = state->short_case_preserve;
402 /****************************************************************************
404 ****************************************************************************/
406 static struct case_semantics_state *set_posix_case_semantics(TALLOC_CTX *mem_ctx,
407 connection_struct *conn)
409 struct case_semantics_state *result;
411 if (!(result = talloc(mem_ctx, struct case_semantics_state))) {
416 result->case_sensitive = conn->case_sensitive;
417 result->case_preserve = conn->case_preserve;
418 result->short_case_preserve = conn->short_case_preserve;
421 conn->case_sensitive = True;
422 conn->case_preserve = True;
423 conn->short_case_preserve = True;
425 talloc_set_destructor(result, restore_case_semantics);
430 /****************************************************************************
431 Reply to an NT create and X call.
432 ****************************************************************************/
434 void reply_ntcreate_and_X(struct smb_request *req)
436 connection_struct *conn = req->conn;
437 struct smb_filename *smb_fname = NULL;
441 uint32 file_attributes;
443 uint32 create_disposition;
444 uint32 create_options;
446 uint64_t allocation_size;
447 /* Breakout the oplock request bits so we can set the
448 reply bits separately. */
450 SMB_OFF_T file_len = 0;
452 files_struct *fsp = NULL;
454 struct timespec create_timespec;
455 struct timespec c_timespec;
456 struct timespec a_timespec;
457 struct timespec m_timespec;
458 struct timespec write_time_ts;
461 uint8_t oplock_granted = NO_OPLOCK_RETURN;
462 struct case_semantics_state *case_state = NULL;
463 TALLOC_CTX *ctx = talloc_tos();
465 START_PROFILE(SMBntcreateX);
468 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
472 flags = IVAL(req->vwv+3, 1);
473 access_mask = IVAL(req->vwv+7, 1);
474 file_attributes = IVAL(req->vwv+13, 1);
475 share_access = IVAL(req->vwv+15, 1);
476 create_disposition = IVAL(req->vwv+17, 1);
477 create_options = IVAL(req->vwv+19, 1);
478 root_dir_fid = (uint16)IVAL(req->vwv+5, 1);
480 allocation_size = (uint64_t)IVAL(req->vwv+9, 1);
481 #ifdef LARGE_SMB_OFF_T
482 allocation_size |= (((uint64_t)IVAL(req->vwv+11, 1)) << 32);
485 srvstr_get_path_req(ctx, req, &fname, (const char *)req->buf,
486 STR_TERMINATE, &status);
488 if (!NT_STATUS_IS_OK(status)) {
489 reply_nterror(req, status);
493 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
494 "file_attributes = 0x%x, share_access = 0x%x, "
495 "create_disposition = 0x%x create_options = 0x%x "
496 "root_dir_fid = 0x%x, fname = %s\n",
498 (unsigned int)access_mask,
499 (unsigned int)file_attributes,
500 (unsigned int)share_access,
501 (unsigned int)create_disposition,
502 (unsigned int)create_options,
503 (unsigned int)root_dir_fid,
507 * we need to remove ignored bits when they come directly from the client
508 * because we reuse some of them for internal stuff
510 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
513 * If it's an IPC, use the pipe handler.
517 if (lp_nt_pipe_support()) {
518 do_ntcreate_pipe_open(conn, req);
521 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
525 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
526 if (oplock_request) {
527 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
531 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
532 case_state = set_posix_case_semantics(ctx, conn);
534 reply_nterror(req, NT_STATUS_NO_MEMORY);
539 status = filename_convert(ctx,
541 req->flags2 & FLAGS2_DFS_PATHNAMES,
547 TALLOC_FREE(case_state);
549 if (!NT_STATUS_IS_OK(status)) {
550 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
552 NT_STATUS_PATH_NOT_COVERED,
556 reply_nterror(req, status);
561 * Bug #6898 - clients using Windows opens should
562 * never be able to set this attribute into the
565 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
567 status = SMB_VFS_CREATE_FILE(
570 root_dir_fid, /* root_dir_fid */
571 smb_fname, /* fname */
572 access_mask, /* access_mask */
573 share_access, /* share_access */
574 create_disposition, /* create_disposition*/
575 create_options, /* create_options */
576 file_attributes, /* file_attributes */
577 oplock_request, /* oplock_request */
578 allocation_size, /* allocation_size */
579 0, /* private_flags */
585 if (!NT_STATUS_IS_OK(status)) {
586 if (open_was_deferred(req->mid)) {
587 /* We have re-scheduled this call, no error. */
590 reply_openerror(req, status);
594 /* Ensure we're pointing at the correct stat struct. */
595 TALLOC_FREE(smb_fname);
596 smb_fname = fsp->fsp_name;
599 * If the caller set the extended oplock request bit
600 * and we granted one (by whatever means) - set the
601 * correct bit for extended oplock reply.
604 if (oplock_request &&
605 (lp_fake_oplocks(SNUM(conn))
606 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
609 * Exclusive oplock granted
612 if (flags & REQUEST_BATCH_OPLOCK) {
613 oplock_granted = BATCH_OPLOCK_RETURN;
615 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
617 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
618 oplock_granted = LEVEL_II_OPLOCK_RETURN;
620 oplock_granted = NO_OPLOCK_RETURN;
623 file_len = smb_fname->st.st_ex_size;
625 if (flags & EXTENDED_RESPONSE_REQUIRED) {
626 /* This is very strange. We
627 * return 50 words, but only set
628 * the wcnt to 42 ? It's definately
629 * what happens on the wire....
631 reply_outbuf(req, 50, 0);
632 SCVAL(req->outbuf,smb_wct,42);
634 reply_outbuf(req, 34, 0);
637 p = (char *)req->outbuf + smb_vwv2;
639 SCVAL(p, 0, oplock_granted);
642 SSVAL(p,0,fsp->fnum);
644 if ((create_disposition == FILE_SUPERSEDE)
645 && (info == FILE_WAS_OVERWRITTEN)) {
646 SIVAL(p,0,FILE_WAS_SUPERSEDED);
652 fattr = dos_mode(conn, smb_fname);
654 fattr = FILE_ATTRIBUTE_NORMAL;
657 /* Deal with other possible opens having a modified
659 ZERO_STRUCT(write_time_ts);
660 get_file_infos(fsp->file_id, 0, NULL, &write_time_ts);
661 if (!null_timespec(write_time_ts)) {
662 update_stat_ex_mtime(&smb_fname->st, write_time_ts);
666 create_timespec = get_create_timespec(conn, fsp, smb_fname);
667 a_timespec = smb_fname->st.st_ex_atime;
668 m_timespec = smb_fname->st.st_ex_mtime;
669 c_timespec = get_change_timespec(conn, fsp, smb_fname);
671 if (lp_dos_filetime_resolution(SNUM(conn))) {
672 dos_filetime_timespec(&create_timespec);
673 dos_filetime_timespec(&a_timespec);
674 dos_filetime_timespec(&m_timespec);
675 dos_filetime_timespec(&c_timespec);
678 put_long_date_timespec(conn->ts_res, p, create_timespec); /* create time. */
680 put_long_date_timespec(conn->ts_res, p, a_timespec); /* access time */
682 put_long_date_timespec(conn->ts_res, p, m_timespec); /* write time */
684 put_long_date_timespec(conn->ts_res, p, c_timespec); /* change time */
686 SIVAL(p,0,fattr); /* File Attributes. */
688 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn,fsp,&smb_fname->st));
690 SOFF_T(p,0,file_len);
692 if (flags & EXTENDED_RESPONSE_REQUIRED) {
693 uint16_t file_status = (NO_EAS|NO_SUBSTREAMS|NO_REPARSETAG);
694 size_t num_names = 0;
695 unsigned int num_streams;
696 struct stream_struct *streams = NULL;
698 /* Do we have any EA's ? */
699 status = get_ea_names_from_file(ctx, conn, fsp,
700 smb_fname->base_name, NULL, &num_names);
701 if (NT_STATUS_IS_OK(status) && num_names) {
702 file_status &= ~NO_EAS;
704 status = SMB_VFS_STREAMINFO(conn, NULL, smb_fname->base_name, ctx,
705 &num_streams, &streams);
706 /* There is always one stream, ::$DATA. */
707 if (NT_STATUS_IS_OK(status) && num_streams > 1) {
708 file_status &= ~NO_SUBSTREAMS;
710 TALLOC_FREE(streams);
711 SSVAL(p,2,file_status);
714 SCVAL(p,0,fsp->is_directory ? 1 : 0);
716 if (flags & EXTENDED_RESPONSE_REQUIRED) {
719 if (fsp->is_directory ||
720 can_write_to_file(conn, smb_fname)) {
721 perms = FILE_GENERIC_ALL;
723 perms = FILE_GENERIC_READ|FILE_EXECUTE;
728 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n",
729 fsp->fnum, smb_fname_str_dbg(smb_fname)));
733 END_PROFILE(SMBntcreateX);
737 /****************************************************************************
738 Reply to a NT_TRANSACT_CREATE call to open a pipe.
739 ****************************************************************************/
741 static void do_nt_transact_create_pipe(connection_struct *conn,
742 struct smb_request *req,
743 uint16 **ppsetup, uint32 setup_count,
744 char **ppparams, uint32 parameter_count,
745 char **ppdata, uint32 data_count)
748 char *params = *ppparams;
754 TALLOC_CTX *ctx = talloc_tos();
757 * Ensure minimum number of parameters sent.
760 if(parameter_count < 54) {
761 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
762 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
766 flags = IVAL(params,0);
768 srvstr_get_path(ctx, params, req->flags2, &fname, params+53,
769 parameter_count-53, STR_TERMINATE,
771 if (!NT_STATUS_IS_OK(status)) {
772 reply_nterror(req, status);
776 nt_open_pipe(fname, conn, req, &pnum);
783 /* Realloc the size of parameters and data we will return */
784 if (flags & EXTENDED_RESPONSE_REQUIRED) {
785 /* Extended response is 32 more byyes. */
790 params = nttrans_realloc(ppparams, param_len);
792 reply_nterror(req, NT_STATUS_NO_MEMORY);
797 SCVAL(p,0,NO_OPLOCK_RETURN);
802 SIVAL(p,0,FILE_WAS_OPENED);
806 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
809 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
811 SSVAL(p,2, 0x5FF); /* ? */
814 if (flags & EXTENDED_RESPONSE_REQUIRED) {
816 SIVAL(p,0,FILE_GENERIC_ALL);
818 * For pipes W2K3 seems to return
820 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
822 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
825 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
827 /* Send the required number of replies */
828 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
833 /****************************************************************************
834 Internal fn to set security descriptors.
835 ****************************************************************************/
837 NTSTATUS set_sd(files_struct *fsp, uint8_t *data, uint32_t sd_len,
838 uint32_t security_info_sent)
840 struct security_descriptor *psd = NULL;
844 return NT_STATUS_INVALID_PARAMETER;
847 if (!CAN_WRITE(fsp->conn)) {
848 return NT_STATUS_ACCESS_DENIED;
851 if (!lp_nt_acl_support(SNUM(fsp->conn))) {
855 status = unmarshall_sec_desc(talloc_tos(), data, sd_len, &psd);
857 if (!NT_STATUS_IS_OK(status)) {
861 if (psd->owner_sid == NULL) {
862 security_info_sent &= ~SECINFO_OWNER;
864 if (psd->group_sid == NULL) {
865 security_info_sent &= ~SECINFO_GROUP;
868 /* Ensure we have at least one thing set. */
869 if ((security_info_sent & (SECINFO_OWNER|SECINFO_GROUP|SECINFO_DACL|SECINFO_SACL)) == 0) {
870 return NT_STATUS_INVALID_PARAMETER;
873 /* Ensure we have the rights to do this. */
874 if (security_info_sent & SECINFO_OWNER) {
875 if (!(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
876 return NT_STATUS_ACCESS_DENIED;
880 if (security_info_sent & SECINFO_GROUP) {
881 if (!(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
882 return NT_STATUS_ACCESS_DENIED;
886 if (security_info_sent & SECINFO_DACL) {
887 if (!(fsp->access_mask & SEC_STD_WRITE_DAC)) {
888 return NT_STATUS_ACCESS_DENIED;
890 /* Convert all the generic bits. */
892 security_acl_map_generic(psd->dacl, &file_generic_mapping);
896 if (security_info_sent & SECINFO_SACL) {
897 if (!(fsp->access_mask & SEC_FLAG_SYSTEM_SECURITY)) {
898 return NT_STATUS_ACCESS_DENIED;
900 /* Convert all the generic bits. */
902 security_acl_map_generic(psd->sacl, &file_generic_mapping);
906 if (DEBUGLEVEL >= 10) {
907 DEBUG(10,("set_sd for file %s\n", fsp_str_dbg(fsp)));
908 NDR_PRINT_DEBUG(security_descriptor, psd);
911 status = SMB_VFS_FSET_NT_ACL(fsp, security_info_sent, psd);
918 /****************************************************************************
919 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
920 ****************************************************************************/
922 struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
924 struct ea_list *ea_list_head = NULL;
931 while (offset + 4 <= data_size) {
932 size_t next_offset = IVAL(pdata,offset);
933 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
939 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
940 if (next_offset == 0) {
943 offset += next_offset;
949 /****************************************************************************
950 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
951 ****************************************************************************/
953 static void call_nt_transact_create(connection_struct *conn,
954 struct smb_request *req,
955 uint16 **ppsetup, uint32 setup_count,
956 char **ppparams, uint32 parameter_count,
957 char **ppdata, uint32 data_count,
958 uint32 max_data_count)
960 struct smb_filename *smb_fname = NULL;
962 char *params = *ppparams;
963 char *data = *ppdata;
964 /* Breakout the oplock request bits so we can set the reply bits separately. */
966 SMB_OFF_T file_len = 0;
968 files_struct *fsp = NULL;
972 uint32 file_attributes;
974 uint32 create_disposition;
975 uint32 create_options;
977 struct security_descriptor *sd = NULL;
980 struct timespec create_timespec;
981 struct timespec c_timespec;
982 struct timespec a_timespec;
983 struct timespec m_timespec;
984 struct timespec write_time_ts;
985 struct ea_list *ea_list = NULL;
988 uint64_t allocation_size;
990 uint8_t oplock_granted;
991 struct case_semantics_state *case_state = NULL;
992 TALLOC_CTX *ctx = talloc_tos();
994 DEBUG(5,("call_nt_transact_create\n"));
997 * If it's an IPC, use the pipe handler.
1001 if (lp_nt_pipe_support()) {
1002 do_nt_transact_create_pipe(
1004 ppsetup, setup_count,
1005 ppparams, parameter_count,
1006 ppdata, data_count);
1009 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1014 * Ensure minimum number of parameters sent.
1017 if(parameter_count < 54) {
1018 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1019 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1023 flags = IVAL(params,0);
1024 access_mask = IVAL(params,8);
1025 file_attributes = IVAL(params,20);
1026 share_access = IVAL(params,24);
1027 create_disposition = IVAL(params,28);
1028 create_options = IVAL(params,32);
1029 sd_len = IVAL(params,36);
1030 ea_len = IVAL(params,40);
1031 root_dir_fid = (uint16)IVAL(params,4);
1032 allocation_size = (uint64_t)IVAL(params,12);
1033 #ifdef LARGE_SMB_OFF_T
1034 allocation_size |= (((uint64_t)IVAL(params,16)) << 32);
1038 * we need to remove ignored bits when they come directly from the client
1039 * because we reuse some of them for internal stuff
1041 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
1043 /* Ensure the data_len is correct for the sd and ea values given. */
1044 if ((ea_len + sd_len > data_count)
1045 || (ea_len > data_count) || (sd_len > data_count)
1046 || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
1047 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
1048 "%u, data_count = %u\n", (unsigned int)ea_len,
1049 (unsigned int)sd_len, (unsigned int)data_count));
1050 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1055 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
1058 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
1060 if (!NT_STATUS_IS_OK(status)) {
1061 DEBUG(10, ("call_nt_transact_create: "
1062 "unmarshall_sec_desc failed: %s\n",
1063 nt_errstr(status)));
1064 reply_nterror(req, status);
1070 if (!lp_ea_support(SNUM(conn))) {
1071 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
1072 "EA's not supported.\n",
1073 (unsigned int)ea_len));
1074 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
1079 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
1080 "too small (should be more than 10)\n",
1081 (unsigned int)ea_len ));
1082 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1086 /* We have already checked that ea_len <= data_count here. */
1087 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
1089 if (ea_list == NULL) {
1090 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1095 srvstr_get_path(ctx, params, req->flags2, &fname,
1096 params+53, parameter_count-53,
1097 STR_TERMINATE, &status);
1098 if (!NT_STATUS_IS_OK(status)) {
1099 reply_nterror(req, status);
1103 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1104 case_state = set_posix_case_semantics(ctx, conn);
1106 reply_nterror(req, NT_STATUS_NO_MEMORY);
1111 status = filename_convert(ctx,
1113 req->flags2 & FLAGS2_DFS_PATHNAMES,
1119 TALLOC_FREE(case_state);
1121 if (!NT_STATUS_IS_OK(status)) {
1122 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1123 reply_botherror(req,
1124 NT_STATUS_PATH_NOT_COVERED,
1125 ERRSRV, ERRbadpath);
1128 reply_nterror(req, status);
1132 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1133 if (oplock_request) {
1134 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
1139 * Bug #6898 - clients using Windows opens should
1140 * never be able to set this attribute into the
1143 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
1145 status = SMB_VFS_CREATE_FILE(
1148 root_dir_fid, /* root_dir_fid */
1149 smb_fname, /* fname */
1150 access_mask, /* access_mask */
1151 share_access, /* share_access */
1152 create_disposition, /* create_disposition*/
1153 create_options, /* create_options */
1154 file_attributes, /* file_attributes */
1155 oplock_request, /* oplock_request */
1156 allocation_size, /* allocation_size */
1157 0, /* private_flags */
1159 ea_list, /* ea_list */
1163 if(!NT_STATUS_IS_OK(status)) {
1164 if (open_was_deferred(req->mid)) {
1165 /* We have re-scheduled this call, no error. */
1168 reply_openerror(req, status);
1172 /* Ensure we're pointing at the correct stat struct. */
1173 TALLOC_FREE(smb_fname);
1174 smb_fname = fsp->fsp_name;
1177 * If the caller set the extended oplock request bit
1178 * and we granted one (by whatever means) - set the
1179 * correct bit for extended oplock reply.
1182 if (oplock_request &&
1183 (lp_fake_oplocks(SNUM(conn))
1184 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
1187 * Exclusive oplock granted
1190 if (flags & REQUEST_BATCH_OPLOCK) {
1191 oplock_granted = BATCH_OPLOCK_RETURN;
1193 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
1195 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1196 oplock_granted = LEVEL_II_OPLOCK_RETURN;
1198 oplock_granted = NO_OPLOCK_RETURN;
1201 file_len = smb_fname->st.st_ex_size;
1203 /* Realloc the size of parameters and data we will return */
1204 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1205 /* Extended response is 32 more byyes. */
1210 params = nttrans_realloc(ppparams, param_len);
1211 if(params == NULL) {
1212 reply_nterror(req, NT_STATUS_NO_MEMORY);
1217 SCVAL(p, 0, oplock_granted);
1220 SSVAL(p,0,fsp->fnum);
1222 if ((create_disposition == FILE_SUPERSEDE)
1223 && (info == FILE_WAS_OVERWRITTEN)) {
1224 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1230 fattr = dos_mode(conn, smb_fname);
1232 fattr = FILE_ATTRIBUTE_NORMAL;
1235 /* Deal with other possible opens having a modified
1237 ZERO_STRUCT(write_time_ts);
1238 get_file_infos(fsp->file_id, 0, NULL, &write_time_ts);
1239 if (!null_timespec(write_time_ts)) {
1240 update_stat_ex_mtime(&smb_fname->st, write_time_ts);
1244 create_timespec = get_create_timespec(conn, fsp, smb_fname);
1245 a_timespec = smb_fname->st.st_ex_atime;
1246 m_timespec = smb_fname->st.st_ex_mtime;
1247 c_timespec = get_change_timespec(conn, fsp, smb_fname);
1249 if (lp_dos_filetime_resolution(SNUM(conn))) {
1250 dos_filetime_timespec(&create_timespec);
1251 dos_filetime_timespec(&a_timespec);
1252 dos_filetime_timespec(&m_timespec);
1253 dos_filetime_timespec(&c_timespec);
1256 put_long_date_timespec(conn->ts_res, p, create_timespec); /* create time. */
1258 put_long_date_timespec(conn->ts_res, p, a_timespec); /* access time */
1260 put_long_date_timespec(conn->ts_res, p, m_timespec); /* write time */
1262 put_long_date_timespec(conn->ts_res, p, c_timespec); /* change time */
1264 SIVAL(p,0,fattr); /* File Attributes. */
1266 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn, fsp, &smb_fname->st));
1268 SOFF_T(p,0,file_len);
1270 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1271 uint16_t file_status = (NO_EAS|NO_SUBSTREAMS|NO_REPARSETAG);
1272 size_t num_names = 0;
1273 unsigned int num_streams;
1274 struct stream_struct *streams = NULL;
1276 /* Do we have any EA's ? */
1277 status = get_ea_names_from_file(ctx, conn, fsp,
1278 smb_fname->base_name, NULL, &num_names);
1279 if (NT_STATUS_IS_OK(status) && num_names) {
1280 file_status &= ~NO_EAS;
1282 status = SMB_VFS_STREAMINFO(conn, NULL, smb_fname->base_name, ctx,
1283 &num_streams, &streams);
1284 /* There is always one stream, ::$DATA. */
1285 if (NT_STATUS_IS_OK(status) && num_streams > 1) {
1286 file_status &= ~NO_SUBSTREAMS;
1288 TALLOC_FREE(streams);
1289 SSVAL(p,2,file_status);
1292 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1294 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1297 if (fsp->is_directory ||
1298 can_write_to_file(conn, smb_fname)) {
1299 perms = FILE_GENERIC_ALL;
1301 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1306 DEBUG(5,("call_nt_transact_create: open name = %s\n",
1307 smb_fname_str_dbg(smb_fname)));
1309 /* Send the required number of replies */
1310 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1315 /****************************************************************************
1316 Reply to a NT CANCEL request.
1317 conn POINTER CAN BE NULL HERE !
1318 ****************************************************************************/
1320 void reply_ntcancel(struct smb_request *req)
1323 * Go through and cancel any pending change notifies.
1326 START_PROFILE(SMBntcancel);
1327 srv_cancel_sign_response(req->sconn);
1328 remove_pending_change_notify_requests_by_mid(req->sconn, req->mid);
1329 remove_pending_lock_requests_by_mid_smb1(req->sconn, req->mid);
1331 DEBUG(3,("reply_ntcancel: cancel called on mid = %llu.\n",
1332 (unsigned long long)req->mid));
1334 END_PROFILE(SMBntcancel);
1338 /****************************************************************************
1340 ****************************************************************************/
1342 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
1343 connection_struct *conn,
1344 struct smb_request *req,
1345 struct smb_filename *smb_fname_src,
1346 struct smb_filename *smb_fname_dst,
1349 files_struct *fsp1,*fsp2;
1353 NTSTATUS status = NT_STATUS_OK;
1356 if (!CAN_WRITE(conn)) {
1357 status = NT_STATUS_MEDIA_WRITE_PROTECTED;
1361 /* Source must already exist. */
1362 if (!VALID_STAT(smb_fname_src->st)) {
1363 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
1367 /* Ensure attributes match. */
1368 fattr = dos_mode(conn, smb_fname_src);
1369 if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
1370 status = NT_STATUS_NO_SUCH_FILE;
1374 /* Disallow if dst file already exists. */
1375 if (VALID_STAT(smb_fname_dst->st)) {
1376 status = NT_STATUS_OBJECT_NAME_COLLISION;
1380 /* No links from a directory. */
1381 if (S_ISDIR(smb_fname_src->st.st_ex_mode)) {
1382 status = NT_STATUS_FILE_IS_A_DIRECTORY;
1386 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1387 smb_fname_str_dbg(smb_fname_src),
1388 smb_fname_str_dbg(smb_fname_dst)));
1390 status = SMB_VFS_CREATE_FILE(
1393 0, /* root_dir_fid */
1394 smb_fname_src, /* fname */
1395 FILE_READ_DATA|FILE_READ_ATTRIBUTES|
1396 FILE_READ_EA, /* access_mask */
1397 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1399 FILE_OPEN, /* create_disposition*/
1400 0, /* create_options */
1401 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
1402 NO_OPLOCK, /* oplock_request */
1403 0, /* allocation_size */
1404 0, /* private_flags */
1410 if (!NT_STATUS_IS_OK(status)) {
1414 status = SMB_VFS_CREATE_FILE(
1417 0, /* root_dir_fid */
1418 smb_fname_dst, /* fname */
1419 FILE_WRITE_DATA|FILE_WRITE_ATTRIBUTES|
1420 FILE_WRITE_EA, /* access_mask */
1421 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1423 FILE_CREATE, /* create_disposition*/
1424 0, /* create_options */
1425 fattr, /* file_attributes */
1426 NO_OPLOCK, /* oplock_request */
1427 0, /* allocation_size */
1428 0, /* private_flags */
1434 if (!NT_STATUS_IS_OK(status)) {
1435 close_file(NULL, fsp1, ERROR_CLOSE);
1439 if (smb_fname_src->st.st_ex_size) {
1440 ret = vfs_transfer_file(fsp1, fsp2, smb_fname_src->st.st_ex_size);
1444 * As we are opening fsp1 read-only we only expect
1445 * an error on close on fsp2 if we are out of space.
1446 * Thus we don't look at the error return from the
1449 close_file(NULL, fsp1, NORMAL_CLOSE);
1451 /* Ensure the modtime is set correctly on the destination file. */
1452 set_close_write_time(fsp2, smb_fname_src->st.st_ex_mtime);
1454 status = close_file(NULL, fsp2, NORMAL_CLOSE);
1456 /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
1457 creates the file. This isn't the correct thing to do in the copy
1459 if (!parent_dirname(talloc_tos(), smb_fname_dst->base_name, &parent,
1461 status = NT_STATUS_NO_MEMORY;
1464 file_set_dosmode(conn, smb_fname_dst, fattr, parent, false);
1465 TALLOC_FREE(parent);
1467 if (ret < (SMB_OFF_T)smb_fname_src->st.st_ex_size) {
1468 status = NT_STATUS_DISK_FULL;
1472 if (!NT_STATUS_IS_OK(status)) {
1473 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1474 nt_errstr(status), smb_fname_str_dbg(smb_fname_src),
1475 smb_fname_str_dbg(smb_fname_dst)));
1481 /****************************************************************************
1482 Reply to a NT rename request.
1483 ****************************************************************************/
1485 void reply_ntrename(struct smb_request *req)
1487 connection_struct *conn = req->conn;
1488 struct smb_filename *smb_fname_old = NULL;
1489 struct smb_filename *smb_fname_new = NULL;
1490 char *oldname = NULL;
1491 char *newname = NULL;
1494 bool src_has_wcard = False;
1495 bool dest_has_wcard = False;
1497 uint32_t ucf_flags_src = 0;
1498 uint32_t ucf_flags_dst = 0;
1500 TALLOC_CTX *ctx = talloc_tos();
1501 bool stream_rename = false;
1503 START_PROFILE(SMBntrename);
1506 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1510 attrs = SVAL(req->vwv+0, 0);
1511 rename_type = SVAL(req->vwv+1, 0);
1513 p = (const char *)req->buf + 1;
1514 p += srvstr_get_path_req_wcard(ctx, req, &oldname, p, STR_TERMINATE,
1515 &status, &src_has_wcard);
1516 if (!NT_STATUS_IS_OK(status)) {
1517 reply_nterror(req, status);
1521 if (ms_has_wild(oldname)) {
1522 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1527 p += srvstr_get_path_req_wcard(ctx, req, &newname, p, STR_TERMINATE,
1528 &status, &dest_has_wcard);
1529 if (!NT_STATUS_IS_OK(status)) {
1530 reply_nterror(req, status);
1534 if (!lp_posix_pathnames()) {
1535 /* The newname must begin with a ':' if the
1536 oldname contains a ':'. */
1537 if (strchr_m(oldname, ':')) {
1538 if (newname[0] != ':') {
1539 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1542 stream_rename = true;
1547 * If this is a rename operation, allow wildcards and save the
1548 * destination's last component.
1550 if (rename_type == RENAME_FLAG_RENAME) {
1551 ucf_flags_src = UCF_COND_ALLOW_WCARD_LCOMP;
1552 ucf_flags_dst = UCF_COND_ALLOW_WCARD_LCOMP | UCF_SAVE_LCOMP;
1555 /* rename_internals() calls unix_convert(), so don't call it here. */
1556 status = filename_convert(ctx, conn,
1557 req->flags2 & FLAGS2_DFS_PATHNAMES,
1562 if (!NT_STATUS_IS_OK(status)) {
1563 if (NT_STATUS_EQUAL(status,
1564 NT_STATUS_PATH_NOT_COVERED)) {
1565 reply_botherror(req,
1566 NT_STATUS_PATH_NOT_COVERED,
1567 ERRSRV, ERRbadpath);
1570 reply_nterror(req, status);
1574 status = filename_convert(ctx, conn,
1575 req->flags2 & FLAGS2_DFS_PATHNAMES,
1580 if (!NT_STATUS_IS_OK(status)) {
1581 if (NT_STATUS_EQUAL(status,
1582 NT_STATUS_PATH_NOT_COVERED)) {
1583 reply_botherror(req,
1584 NT_STATUS_PATH_NOT_COVERED,
1585 ERRSRV, ERRbadpath);
1588 reply_nterror(req, status);
1592 if (stream_rename) {
1593 /* smb_fname_new must be the same as smb_fname_old. */
1594 TALLOC_FREE(smb_fname_new->base_name);
1595 smb_fname_new->base_name = talloc_strdup(smb_fname_new,
1596 smb_fname_old->base_name);
1597 if (!smb_fname_new->base_name) {
1598 reply_nterror(req, NT_STATUS_NO_MEMORY);
1603 DEBUG(3,("reply_ntrename: %s -> %s\n",
1604 smb_fname_str_dbg(smb_fname_old),
1605 smb_fname_str_dbg(smb_fname_new)));
1607 switch(rename_type) {
1608 case RENAME_FLAG_RENAME:
1609 status = rename_internals(ctx, conn, req,
1610 smb_fname_old, smb_fname_new,
1611 attrs, False, src_has_wcard,
1615 case RENAME_FLAG_HARD_LINK:
1616 if (src_has_wcard || dest_has_wcard) {
1618 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1620 status = hardlink_internals(ctx, conn,
1627 case RENAME_FLAG_COPY:
1628 if (src_has_wcard || dest_has_wcard) {
1630 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1632 status = copy_internals(ctx, conn, req,
1638 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1639 status = NT_STATUS_INVALID_PARAMETER;
1642 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1646 if (!NT_STATUS_IS_OK(status)) {
1647 if (open_was_deferred(req->mid)) {
1648 /* We have re-scheduled this call. */
1652 reply_nterror(req, status);
1656 reply_outbuf(req, 0, 0);
1658 END_PROFILE(SMBntrename);
1662 /****************************************************************************
1663 Reply to a notify change - queue the request and
1664 don't allow a directory to be opened.
1665 ****************************************************************************/
1667 static void smbd_smb1_notify_reply(struct smb_request *req,
1668 NTSTATUS error_code,
1669 uint8_t *buf, size_t len)
1671 send_nt_replies(req->conn, req, error_code, (char *)buf, len, NULL, 0);
1674 static void call_nt_transact_notify_change(connection_struct *conn,
1675 struct smb_request *req,
1679 uint32 parameter_count,
1680 char **ppdata, uint32 data_count,
1681 uint32 max_data_count,
1682 uint32 max_param_count)
1684 uint16 *setup = *ppsetup;
1690 if(setup_count < 6) {
1691 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1695 fsp = file_fsp(req, SVAL(setup,4));
1696 filter = IVAL(setup, 0);
1697 recursive = (SVAL(setup, 6) != 0) ? True : False;
1699 DEBUG(3,("call_nt_transact_notify_change\n"));
1702 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1707 char *filter_string;
1709 if (!(filter_string = notify_filter_string(NULL, filter))) {
1710 reply_nterror(req,NT_STATUS_NO_MEMORY);
1714 DEBUG(3,("call_nt_transact_notify_change: notify change "
1715 "called on %s, filter = %s, recursive = %d\n",
1716 fsp_str_dbg(fsp), filter_string, recursive));
1718 TALLOC_FREE(filter_string);
1721 if((!fsp->is_directory) || (conn != fsp->conn)) {
1722 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1726 if (fsp->notify == NULL) {
1728 status = change_notify_create(fsp, filter, recursive);
1730 if (!NT_STATUS_IS_OK(status)) {
1731 DEBUG(10, ("change_notify_create returned %s\n",
1732 nt_errstr(status)));
1733 reply_nterror(req, status);
1738 if (fsp->notify->num_changes != 0) {
1741 * We've got changes pending, respond immediately
1745 * TODO: write a torture test to check the filtering behaviour
1749 change_notify_reply(req,
1753 smbd_smb1_notify_reply);
1756 * change_notify_reply() above has independently sent its
1763 * No changes pending, queue the request
1766 status = change_notify_add_request(req,
1770 smbd_smb1_notify_reply);
1771 if (!NT_STATUS_IS_OK(status)) {
1772 reply_nterror(req, status);
1777 /****************************************************************************
1778 Reply to an NT transact rename command.
1779 ****************************************************************************/
1781 static void call_nt_transact_rename(connection_struct *conn,
1782 struct smb_request *req,
1783 uint16 **ppsetup, uint32 setup_count,
1784 char **ppparams, uint32 parameter_count,
1785 char **ppdata, uint32 data_count,
1786 uint32 max_data_count)
1788 char *params = *ppparams;
1789 char *new_name = NULL;
1790 files_struct *fsp = NULL;
1791 bool dest_has_wcard = False;
1793 TALLOC_CTX *ctx = talloc_tos();
1795 if(parameter_count < 5) {
1796 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1800 fsp = file_fsp(req, SVAL(params, 0));
1801 if (!check_fsp(conn, req, fsp)) {
1804 srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
1805 parameter_count - 4,
1806 STR_TERMINATE, &status, &dest_has_wcard);
1807 if (!NT_STATUS_IS_OK(status)) {
1808 reply_nterror(req, status);
1813 * W2K3 ignores this request as the RAW-RENAME test
1814 * demonstrates, so we do.
1816 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1818 DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1819 fsp_str_dbg(fsp), new_name));
1824 /******************************************************************************
1825 Fake up a completely empty SD.
1826 *******************************************************************************/
1828 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, struct security_descriptor **ppsd)
1832 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1834 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1835 return NT_STATUS_NO_MEMORY;
1838 return NT_STATUS_OK;
1841 /****************************************************************************
1842 Reply to query a security descriptor.
1843 Callable from SMB2 and SMB2.
1844 If it returns NT_STATUS_BUFFER_TOO_SMALL, pdata_size is initialized with
1846 ****************************************************************************/
1848 NTSTATUS smbd_do_query_security_desc(connection_struct *conn,
1849 TALLOC_CTX *mem_ctx,
1851 uint32_t security_info_wanted,
1852 uint32_t max_data_count,
1853 uint8_t **ppmarshalled_sd,
1857 struct security_descriptor *psd = NULL;
1860 * Get the permissions to return.
1863 if ((security_info_wanted & SECINFO_SACL) &&
1864 !(fsp->access_mask & SEC_FLAG_SYSTEM_SECURITY)) {
1865 return NT_STATUS_ACCESS_DENIED;
1868 if ((security_info_wanted & (SECINFO_DACL|SECINFO_OWNER|SECINFO_GROUP)) &&
1869 !(fsp->access_mask & SEC_STD_READ_CONTROL)) {
1870 return NT_STATUS_ACCESS_DENIED;
1873 if (!lp_nt_acl_support(SNUM(conn))) {
1874 status = get_null_nt_acl(mem_ctx, &psd);
1876 status = SMB_VFS_FGET_NT_ACL(
1877 fsp, security_info_wanted, &psd);
1879 if (!NT_STATUS_IS_OK(status)) {
1883 if (!(security_info_wanted & SECINFO_OWNER)) {
1884 psd->owner_sid = NULL;
1886 if (!(security_info_wanted & SECINFO_GROUP)) {
1887 psd->group_sid = NULL;
1889 if (!(security_info_wanted & SECINFO_DACL)) {
1892 if (!(security_info_wanted & SECINFO_SACL)) {
1896 /* If the SACL/DACL is NULL, but was requested, we mark that it is
1897 * present in the reply to match Windows behavior */
1898 if (psd->sacl == NULL &&
1899 security_info_wanted & SECINFO_SACL)
1900 psd->type |= SEC_DESC_SACL_PRESENT;
1901 if (psd->dacl == NULL &&
1902 security_info_wanted & SECINFO_DACL)
1903 psd->type |= SEC_DESC_DACL_PRESENT;
1905 *psd_size = ndr_size_security_descriptor(psd, 0);
1907 DEBUG(3,("smbd_do_query_security_desc: sd_size = %lu.\n",
1908 (unsigned long)*psd_size));
1910 if (DEBUGLEVEL >= 10) {
1911 DEBUG(10,("smbd_do_query_security_desc for file %s\n",
1913 NDR_PRINT_DEBUG(security_descriptor, psd);
1916 if (max_data_count < *psd_size) {
1918 return NT_STATUS_BUFFER_TOO_SMALL;
1921 status = marshall_sec_desc(mem_ctx, psd,
1922 ppmarshalled_sd, psd_size);
1924 if (!NT_STATUS_IS_OK(status)) {
1930 return NT_STATUS_OK;
1933 /****************************************************************************
1934 SMB1 reply to query a security descriptor.
1935 ****************************************************************************/
1937 static void call_nt_transact_query_security_desc(connection_struct *conn,
1938 struct smb_request *req,
1942 uint32 parameter_count,
1945 uint32 max_data_count)
1947 char *params = *ppparams;
1948 char *data = *ppdata;
1950 uint32 security_info_wanted;
1951 files_struct *fsp = NULL;
1953 uint8_t *marshalled_sd = NULL;
1955 if(parameter_count < 8) {
1956 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1960 fsp = file_fsp(req, SVAL(params,0));
1962 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1966 security_info_wanted = IVAL(params,4);
1968 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, "
1969 "info_wanted = 0x%x\n", fsp_str_dbg(fsp),
1970 (unsigned int)security_info_wanted));
1972 params = nttrans_realloc(ppparams, 4);
1973 if(params == NULL) {
1974 reply_nterror(req, NT_STATUS_NO_MEMORY);
1979 * Get the permissions to return.
1982 status = smbd_do_query_security_desc(conn,
1985 security_info_wanted,
1990 if (NT_STATUS_EQUAL(status, NT_STATUS_BUFFER_TOO_SMALL)) {
1991 SIVAL(params,0,(uint32_t)sd_size);
1992 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
1993 params, 4, NULL, 0);
1997 if (!NT_STATUS_IS_OK(status)) {
1998 reply_nterror(req, status);
2002 SMB_ASSERT(sd_size > 0);
2004 SIVAL(params,0,(uint32_t)sd_size);
2006 if (max_data_count < sd_size) {
2007 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
2008 params, 4, NULL, 0);
2013 * Allocate the data we will return.
2016 data = nttrans_realloc(ppdata, sd_size);
2018 reply_nterror(req, NT_STATUS_NO_MEMORY);
2022 memcpy(data, marshalled_sd, sd_size);
2024 send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
2029 /****************************************************************************
2030 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
2031 ****************************************************************************/
2033 static void call_nt_transact_set_security_desc(connection_struct *conn,
2034 struct smb_request *req,
2038 uint32 parameter_count,
2041 uint32 max_data_count)
2043 char *params= *ppparams;
2044 char *data = *ppdata;
2045 files_struct *fsp = NULL;
2046 uint32 security_info_sent = 0;
2049 if(parameter_count < 8) {
2050 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2054 if((fsp = file_fsp(req, SVAL(params,0))) == NULL) {
2055 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2059 if (!CAN_WRITE(fsp->conn)) {
2060 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2064 if(!lp_nt_acl_support(SNUM(conn))) {
2068 security_info_sent = IVAL(params,4);
2070 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n",
2071 fsp_str_dbg(fsp), (unsigned int)security_info_sent));
2073 if (data_count == 0) {
2074 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2078 status = set_sd(fsp, (uint8 *)data, data_count, security_info_sent);
2080 if (!NT_STATUS_IS_OK(status)) {
2081 reply_nterror(req, status);
2086 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2090 /****************************************************************************
2092 ****************************************************************************/
2094 static void call_nt_transact_ioctl(connection_struct *conn,
2095 struct smb_request *req,
2096 uint16 **ppsetup, uint32 setup_count,
2097 char **ppparams, uint32 parameter_count,
2098 char **ppdata, uint32 data_count,
2099 uint32 max_data_count)
2106 char *pdata = *ppdata;
2108 if (setup_count != 8) {
2109 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
2110 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2114 function = IVAL(*ppsetup, 0);
2115 fidnum = SVAL(*ppsetup, 4);
2116 isFSctl = CVAL(*ppsetup, 6);
2117 compfilter = CVAL(*ppsetup, 7);
2119 DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
2120 function, fidnum, isFSctl, compfilter));
2122 fsp=file_fsp(req, fidnum);
2123 /* this check is done in each implemented function case for now
2124 because I don't want to break anything... --metze
2125 FSP_BELONGS_CONN(fsp,conn);*/
2127 SMB_PERFCOUNT_SET_IOCTL(&req->pcd, function);
2130 case FSCTL_SET_SPARSE:
2132 bool set_sparse = true;
2135 if (data_count >= 1 && pdata[0] == 0) {
2139 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X]set[%u]\n",
2140 fidnum, set_sparse));
2142 if (!check_fsp_open(conn, req, fsp)) {
2146 status = file_set_sparse(conn, fsp, set_sparse);
2147 if (!NT_STATUS_IS_OK(status)) {
2148 DEBUG(9,("FSCTL_SET_SPARSE: fname[%s] set[%u] - %s\n",
2149 smb_fname_str_dbg(fsp->fsp_name), set_sparse, nt_errstr(status)));
2150 reply_nterror(req, status);
2154 DEBUG(10,("FSCTL_SET_SPARSE: fname[%s] set[%u] - %s\n",
2155 smb_fname_str_dbg(fsp->fsp_name), set_sparse, nt_errstr(status)));
2156 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2159 case FSCTL_CREATE_OR_GET_OBJECT_ID:
2161 unsigned char objid[16];
2163 /* This should return the object-id on this file.
2164 * I think I'll make this be the inode+dev. JRA.
2167 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
2169 if (!check_fsp_open(conn, req, fsp)) {
2174 pdata = nttrans_realloc(ppdata, data_count);
2175 if (pdata == NULL) {
2176 reply_nterror(req, NT_STATUS_NO_MEMORY);
2180 /* For backwards compatibility only store the dev/inode. */
2181 push_file_id_16(pdata, &fsp->file_id);
2182 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
2183 push_file_id_16(pdata+32, &fsp->file_id);
2184 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
2189 case FSCTL_GET_REPARSE_POINT:
2190 /* pretend this fail - my winXP does it like this
2194 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2195 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
2198 case FSCTL_SET_REPARSE_POINT:
2199 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
2203 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2204 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
2207 case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
2210 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
2211 * and return their volume names. If max_data_count is 16, then it is just
2212 * asking for the number of volumes and length of the combined names.
2214 * pdata is the data allocated by our caller, but that uses
2215 * total_data_count (which is 0 in our case) rather than max_data_count.
2216 * Allocate the correct amount and return the pointer to let
2217 * it be deallocated when we return.
2219 SHADOW_COPY_DATA *shadow_data = NULL;
2220 TALLOC_CTX *shadow_mem_ctx = NULL;
2221 bool labels = False;
2222 uint32 labels_data_count = 0;
2226 if (!check_fsp_open(conn, req, fsp)) {
2230 if (max_data_count < 16) {
2231 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
2233 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2237 if (max_data_count > 16) {
2241 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
2242 if (shadow_mem_ctx == NULL) {
2243 DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
2244 reply_nterror(req, NT_STATUS_NO_MEMORY);
2248 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
2249 if (shadow_data == NULL) {
2250 DEBUG(0,("TALLOC_ZERO() failed!\n"));
2251 talloc_destroy(shadow_mem_ctx);
2252 reply_nterror(req, NT_STATUS_NO_MEMORY);
2256 shadow_data->mem_ctx = shadow_mem_ctx;
2259 * Call the VFS routine to actually do the work.
2261 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
2262 talloc_destroy(shadow_data->mem_ctx);
2263 if (errno == ENOSYS) {
2264 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
2265 conn->connectpath));
2266 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2269 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
2270 conn->connectpath));
2271 reply_nterror(req, NT_STATUS_UNSUCCESSFUL);
2276 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
2281 data_count = 12+labels_data_count+4;
2284 if (max_data_count<data_count) {
2285 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
2286 max_data_count,data_count));
2287 talloc_destroy(shadow_data->mem_ctx);
2288 reply_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
2292 pdata = nttrans_realloc(ppdata, data_count);
2293 if (pdata == NULL) {
2294 talloc_destroy(shadow_data->mem_ctx);
2295 reply_nterror(req, NT_STATUS_NO_MEMORY);
2301 /* num_volumes 4 bytes */
2302 SIVAL(pdata,0,shadow_data->num_volumes);
2305 /* num_labels 4 bytes */
2306 SIVAL(pdata,4,shadow_data->num_volumes);
2309 /* needed_data_count 4 bytes */
2310 SIVAL(pdata, 8, labels_data_count+4);
2314 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
2315 shadow_data->num_volumes, fsp_str_dbg(fsp)));
2316 if (labels && shadow_data->labels) {
2317 for (i=0;i<shadow_data->num_volumes;i++) {
2318 srvstr_push(pdata, req->flags2,
2319 cur_pdata, shadow_data->labels[i],
2320 2*sizeof(SHADOW_COPY_LABEL),
2321 STR_UNICODE|STR_TERMINATE);
2322 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
2323 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
2327 talloc_destroy(shadow_data->mem_ctx);
2329 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
2335 case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
2337 /* pretend this succeeded -
2339 * we have to send back a list with all files owned by this SID
2341 * but I have to check that --metze
2347 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
2349 if (!check_fsp_open(conn, req, fsp)) {
2353 if (data_count < 8) {
2354 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2358 sid_len = MIN(data_count-4,SID_MAX_SIZE);
2360 /* unknown 4 bytes: this is not the length of the sid :-( */
2361 /*unknown = IVAL(pdata,0);*/
2363 if (!sid_parse(pdata+4,sid_len,&sid)) {
2364 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2367 DEBUGADD(10, ("for SID: %s\n", sid_string_dbg(&sid)));
2369 if (!sid_to_uid(&sid, &uid)) {
2370 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2371 sid_string_dbg(&sid),
2372 (unsigned long)sid_len));
2376 /* we can take a look at the find source :-)
2378 * find ./ -uid $uid -name '*' is what we need here
2381 * and send 4bytes len and then NULL terminated unicode strings
2384 * but I don't know how to deal with the paged results
2385 * (maybe we can hang the result anywhere in the fsp struct)
2387 * we don't send all files at once
2388 * and at the next we should *not* start from the beginning,
2389 * so we have to cache the result
2394 /* this works for now... */
2395 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2398 case FSCTL_QUERY_ALLOCATED_RANGES:
2400 /* FIXME: This is just a dummy reply, telling that all of the
2401 * file is allocated. MKS cp needs that.
2402 * Adding the real allocated ranges via FIEMAP on Linux
2403 * and SEEK_DATA/SEEK_HOLE on Solaris is needed to make
2404 * this FSCTL correct for sparse files.
2407 uint64_t offset, length;
2409 if (!check_fsp_open(conn, req, fsp)) {
2413 if (data_count != 16) {
2414 DEBUG(0,("FSCTL_QUERY_ALLOCATED_RANGES: data_count(%u) != 16 is invalid!\n",
2416 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2420 if (max_data_count < 16) {
2421 DEBUG(0,("FSCTL_QUERY_ALLOCATED_RANGES: max_data_count(%u) < 16 is invalid!\n",
2423 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2427 offset = BVAL(pdata,0);
2428 length = BVAL(pdata,8);
2430 if (offset + length < offset) {
2431 /* No 64-bit integer wrap. */
2432 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2436 status = vfs_stat_fsp(fsp);
2437 if (!NT_STATUS_IS_OK(status)) {
2438 reply_nterror(req, status);
2442 if (offset > fsp->fsp_name->st.st_ex_size ||
2443 fsp->fsp_name->st.st_ex_size == 0 ||
2445 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2447 uint64_t end = offset + length;
2448 end = MIN(end, fsp->fsp_name->st.st_ex_size);
2451 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
2456 case FSCTL_IS_VOLUME_DIRTY:
2457 DEBUG(10,("FSCTL_IS_VOLUME_DIRTY: called on FID[0x%04X] "
2458 "(but not implemented)\n", (int)fidnum));
2460 * http://msdn.microsoft.com/en-us/library/cc232128%28PROT.10%29.aspx
2461 * says we have to respond with NT_STATUS_INVALID_PARAMETER
2463 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2466 /* Only print this once... */
2467 if (!logged_ioctl_message) {
2468 logged_ioctl_message = true;
2469 DEBUG(2,("call_nt_transact_ioctl(0x%x): "
2470 "Currently not implemented.\n",
2475 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2479 #ifdef HAVE_SYS_QUOTAS
2480 /****************************************************************************
2481 Reply to get user quota
2482 ****************************************************************************/
2484 static void call_nt_transact_get_user_quota(connection_struct *conn,
2485 struct smb_request *req,
2489 uint32 parameter_count,
2492 uint32 max_data_count)
2494 NTSTATUS nt_status = NT_STATUS_OK;
2495 char *params = *ppparams;
2496 char *pdata = *ppdata;
2498 int data_len=0,param_len=0;
2501 files_struct *fsp = NULL;
2505 bool start_enum = True;
2506 SMB_NTQUOTA_STRUCT qt;
2507 SMB_NTQUOTA_LIST *tmp_list;
2508 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2513 if (conn->session_info->utok.uid != 0) {
2514 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2515 "[%s]\n", lp_servicename(SNUM(conn)),
2516 conn->session_info->unix_name));
2517 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2522 * Ensure minimum number of parameters sent.
2525 if (parameter_count < 4) {
2526 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2527 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2531 /* maybe we can check the quota_fnum */
2532 fsp = file_fsp(req, SVAL(params,0));
2533 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2534 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2535 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2539 /* the NULL pointer checking for fsp->fake_file_handle->pd
2540 * is done by CHECK_NTQUOTA_HANDLE_OK()
2542 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->private_data;
2544 level = SVAL(params,2);
2546 /* unknown 12 bytes leading in params */
2549 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2550 /* seems that we should continue with the enum here --metze */
2552 if (qt_handle->quota_list!=NULL &&
2553 qt_handle->tmp_list==NULL) {
2556 free_ntquota_list(&(qt_handle->quota_list));
2558 /* Realloc the size of parameters and data we will return */
2560 params = nttrans_realloc(ppparams, param_len);
2561 if(params == NULL) {
2562 reply_nterror(req, NT_STATUS_NO_MEMORY);
2567 SIVAL(params,0,data_len);
2574 case TRANSACT_GET_USER_QUOTA_LIST_START:
2576 if (qt_handle->quota_list==NULL &&
2577 qt_handle->tmp_list==NULL) {
2581 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2582 reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
2586 /* Realloc the size of parameters and data we will return */
2588 params = nttrans_realloc(ppparams, param_len);
2589 if(params == NULL) {
2590 reply_nterror(req, NT_STATUS_NO_MEMORY);
2594 /* we should not trust the value in max_data_count*/
2595 max_data_count = MIN(max_data_count,2048);
2597 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2599 reply_nterror(req, NT_STATUS_NO_MEMORY);
2605 /* set params Size of returned Quota Data 4 bytes*/
2606 /* but set it later when we know it */
2608 /* for each entry push the data */
2611 qt_handle->tmp_list = qt_handle->quota_list;
2614 tmp_list = qt_handle->tmp_list;
2616 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2617 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2619 sid_len = ndr_size_dom_sid(
2620 &tmp_list->quotas->sid, 0);
2621 entry_len = 40 + sid_len;
2623 /* nextoffset entry 4 bytes */
2624 SIVAL(entry,0,entry_len);
2626 /* then the len of the SID 4 bytes */
2627 SIVAL(entry,4,sid_len);
2629 /* unknown data 8 bytes uint64_t */
2630 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-metze*/
2632 /* the used disk space 8 bytes uint64_t */
2633 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2635 /* the soft quotas 8 bytes uint64_t */
2636 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2638 /* the hard quotas 8 bytes uint64_t */
2639 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2641 /* and now the SID */
2642 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2645 qt_handle->tmp_list = tmp_list;
2647 /* overwrite the offset of the last entry */
2648 SIVAL(entry-entry_len,0,0);
2650 data_len = 4+qt_len;
2651 /* overwrite the params quota_data_len */
2652 SIVAL(params,0,data_len);
2656 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2658 /* unknown 4 bytes IVAL(pdata,0) */
2660 if (data_count < 8) {
2661 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2662 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2666 sid_len = IVAL(pdata,4);
2667 /* Ensure this is less than 1mb. */
2668 if (sid_len > (1024*1024)) {
2669 reply_nterror(req, NT_STATUS_NO_MEMORY);
2673 if (data_count < 8+sid_len) {
2674 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2675 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2679 data_len = 4+40+sid_len;
2681 if (max_data_count < data_len) {
2682 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2683 max_data_count, data_len));
2685 SIVAL(params,0,data_len);
2687 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2691 if (!sid_parse(pdata+8,sid_len,&sid)) {
2692 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2696 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2699 * we have to return zero's in all fields
2700 * instead of returning an error here
2705 /* Realloc the size of parameters and data we will return */
2707 params = nttrans_realloc(ppparams, param_len);
2708 if(params == NULL) {
2709 reply_nterror(req, NT_STATUS_NO_MEMORY);
2713 pdata = nttrans_realloc(ppdata, data_len);
2715 reply_nterror(req, NT_STATUS_NO_MEMORY);
2721 /* set params Size of returned Quota Data 4 bytes*/
2722 SIVAL(params,0,data_len);
2724 /* nextoffset entry 4 bytes */
2727 /* then the len of the SID 4 bytes */
2728 SIVAL(entry,4,sid_len);
2730 /* unknown data 8 bytes uint64_t */
2731 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-mezte*/
2733 /* the used disk space 8 bytes uint64_t */
2734 SBIG_UINT(entry,16,qt.usedspace);
2736 /* the soft quotas 8 bytes uint64_t */
2737 SBIG_UINT(entry,24,qt.softlim);
2739 /* the hard quotas 8 bytes uint64_t */
2740 SBIG_UINT(entry,32,qt.hardlim);
2742 /* and now the SID */
2743 sid_linearize(entry+40, sid_len, &sid);
2748 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2749 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2754 send_nt_replies(conn, req, nt_status, params, param_len,
2758 /****************************************************************************
2759 Reply to set user quota
2760 ****************************************************************************/
2762 static void call_nt_transact_set_user_quota(connection_struct *conn,
2763 struct smb_request *req,
2767 uint32 parameter_count,
2770 uint32 max_data_count)
2772 char *params = *ppparams;
2773 char *pdata = *ppdata;
2774 int data_len=0,param_len=0;
2775 SMB_NTQUOTA_STRUCT qt;
2778 files_struct *fsp = NULL;
2783 if (conn->session_info->utok.uid != 0) {
2784 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2785 "[%s]\n", lp_servicename(SNUM(conn)),
2786 conn->session_info->unix_name));
2787 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2792 * Ensure minimum number of parameters sent.
2795 if (parameter_count < 2) {
2796 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2797 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2801 /* maybe we can check the quota_fnum */
2802 fsp = file_fsp(req, SVAL(params,0));
2803 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2804 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2805 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2809 if (data_count < 40) {
2810 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2811 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2815 /* offset to next quota record.
2816 * 4 bytes IVAL(pdata,0)
2821 sid_len = IVAL(pdata,4);
2823 if (data_count < 40+sid_len || (40+sid_len < sid_len)) {
2824 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2825 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2829 /* unknown 8 bytes in pdata
2830 * maybe its the change time in NTTIME
2833 /* the used space 8 bytes (uint64_t)*/
2834 qt.usedspace = (uint64_t)IVAL(pdata,16);
2835 #ifdef LARGE_SMB_OFF_T
2836 qt.usedspace |= (((uint64_t)IVAL(pdata,20)) << 32);
2837 #else /* LARGE_SMB_OFF_T */
2838 if ((IVAL(pdata,20) != 0)&&
2839 ((qt.usedspace != 0xFFFFFFFF)||
2840 (IVAL(pdata,20)!=0xFFFFFFFF))) {
2841 /* more than 32 bits? */
2842 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2845 #endif /* LARGE_SMB_OFF_T */
2847 /* the soft quotas 8 bytes (uint64_t)*/
2848 qt.softlim = (uint64_t)IVAL(pdata,24);
2849 #ifdef LARGE_SMB_OFF_T
2850 qt.softlim |= (((uint64_t)IVAL(pdata,28)) << 32);
2851 #else /* LARGE_SMB_OFF_T */
2852 if ((IVAL(pdata,28) != 0)&&
2853 ((qt.softlim != 0xFFFFFFFF)||
2854 (IVAL(pdata,28)!=0xFFFFFFFF))) {
2855 /* more than 32 bits? */
2856 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2859 #endif /* LARGE_SMB_OFF_T */
2861 /* the hard quotas 8 bytes (uint64_t)*/
2862 qt.hardlim = (uint64_t)IVAL(pdata,32);
2863 #ifdef LARGE_SMB_OFF_T
2864 qt.hardlim |= (((uint64_t)IVAL(pdata,36)) << 32);
2865 #else /* LARGE_SMB_OFF_T */
2866 if ((IVAL(pdata,36) != 0)&&
2867 ((qt.hardlim != 0xFFFFFFFF)||
2868 (IVAL(pdata,36)!=0xFFFFFFFF))) {
2869 /* more than 32 bits? */
2870 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2873 #endif /* LARGE_SMB_OFF_T */
2875 if (!sid_parse(pdata+40,sid_len,&sid)) {
2876 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2880 DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid)));
2882 /* 44 unknown bytes left... */
2884 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2885 reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
2889 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2892 #endif /* HAVE_SYS_QUOTAS */
2894 static void handle_nttrans(connection_struct *conn,
2895 struct trans_state *state,
2896 struct smb_request *req)
2898 if (get_Protocol() >= PROTOCOL_NT1) {
2899 req->flags2 |= 0x40; /* IS_LONG_NAME */
2900 SSVAL(req->inbuf,smb_flg2,req->flags2);
2904 SMB_PERFCOUNT_SET_SUBOP(&req->pcd, state->call);
2906 /* Now we must call the relevant NT_TRANS function */
2907 switch(state->call) {
2908 case NT_TRANSACT_CREATE:
2910 START_PROFILE(NT_transact_create);
2911 call_nt_transact_create(
2913 &state->setup, state->setup_count,
2914 &state->param, state->total_param,
2915 &state->data, state->total_data,
2916 state->max_data_return);
2917 END_PROFILE(NT_transact_create);
2921 case NT_TRANSACT_IOCTL:
2923 START_PROFILE(NT_transact_ioctl);
2924 call_nt_transact_ioctl(
2926 &state->setup, state->setup_count,
2927 &state->param, state->total_param,
2928 &state->data, state->total_data,
2929 state->max_data_return);
2930 END_PROFILE(NT_transact_ioctl);
2934 case NT_TRANSACT_SET_SECURITY_DESC:
2936 START_PROFILE(NT_transact_set_security_desc);
2937 call_nt_transact_set_security_desc(
2939 &state->setup, state->setup_count,
2940 &state->param, state->total_param,
2941 &state->data, state->total_data,
2942 state->max_data_return);
2943 END_PROFILE(NT_transact_set_security_desc);
2947 case NT_TRANSACT_NOTIFY_CHANGE:
2949 START_PROFILE(NT_transact_notify_change);
2950 call_nt_transact_notify_change(
2952 &state->setup, state->setup_count,
2953 &state->param, state->total_param,
2954 &state->data, state->total_data,
2955 state->max_data_return,
2956 state->max_param_return);
2957 END_PROFILE(NT_transact_notify_change);
2961 case NT_TRANSACT_RENAME:
2963 START_PROFILE(NT_transact_rename);
2964 call_nt_transact_rename(
2966 &state->setup, state->setup_count,
2967 &state->param, state->total_param,
2968 &state->data, state->total_data,
2969 state->max_data_return);
2970 END_PROFILE(NT_transact_rename);
2974 case NT_TRANSACT_QUERY_SECURITY_DESC:
2976 START_PROFILE(NT_transact_query_security_desc);
2977 call_nt_transact_query_security_desc(
2979 &state->setup, state->setup_count,
2980 &state->param, state->total_param,
2981 &state->data, state->total_data,
2982 state->max_data_return);
2983 END_PROFILE(NT_transact_query_security_desc);
2987 #ifdef HAVE_SYS_QUOTAS
2988 case NT_TRANSACT_GET_USER_QUOTA:
2990 START_PROFILE(NT_transact_get_user_quota);
2991 call_nt_transact_get_user_quota(
2993 &state->setup, state->setup_count,
2994 &state->param, state->total_param,
2995 &state->data, state->total_data,
2996 state->max_data_return);
2997 END_PROFILE(NT_transact_get_user_quota);
3001 case NT_TRANSACT_SET_USER_QUOTA:
3003 START_PROFILE(NT_transact_set_user_quota);
3004 call_nt_transact_set_user_quota(
3006 &state->setup, state->setup_count,
3007 &state->param, state->total_param,
3008 &state->data, state->total_data,
3009 state->max_data_return);
3010 END_PROFILE(NT_transact_set_user_quota);
3013 #endif /* HAVE_SYS_QUOTAS */
3016 /* Error in request */
3017 DEBUG(0,("handle_nttrans: Unknown request %d in "
3018 "nttrans call\n", state->call));
3019 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
3025 /****************************************************************************
3026 Reply to a SMBNTtrans.
3027 ****************************************************************************/
3029 void reply_nttrans(struct smb_request *req)
3031 connection_struct *conn = req->conn;
3036 uint16 function_code;
3038 struct trans_state *state;
3040 START_PROFILE(SMBnttrans);
3042 if (req->wct < 19) {
3043 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3044 END_PROFILE(SMBnttrans);
3048 pscnt = IVAL(req->vwv+9, 1);
3049 psoff = IVAL(req->vwv+11, 1);
3050 dscnt = IVAL(req->vwv+13, 1);
3051 dsoff = IVAL(req->vwv+15, 1);
3052 function_code = SVAL(req->vwv+18, 0);
3054 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
3055 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
3056 END_PROFILE(SMBnttrans);
3060 result = allow_new_trans(conn->pending_trans, req->mid);
3061 if (!NT_STATUS_IS_OK(result)) {
3062 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
3063 reply_nterror(req, result);
3064 END_PROFILE(SMBnttrans);
3068 if ((state = TALLOC_P(conn, struct trans_state)) == NULL) {
3069 reply_nterror(req, NT_STATUS_NO_MEMORY);
3070 END_PROFILE(SMBnttrans);
3074 state->cmd = SMBnttrans;
3076 state->mid = req->mid;
3077 state->vuid = req->vuid;
3078 state->total_data = IVAL(req->vwv+3, 1);
3080 state->total_param = IVAL(req->vwv+1, 1);
3081 state->param = NULL;
3082 state->max_data_return = IVAL(req->vwv+7, 1);
3083 state->max_param_return = IVAL(req->vwv+5, 1);
3085 /* setup count is in *words* */
3086 state->setup_count = 2*CVAL(req->vwv+17, 1);
3087 state->setup = NULL;
3088 state->call = function_code;
3090 DEBUG(10, ("num_setup=%u, "
3091 "param_total=%u, this_param=%u, max_param=%u, "
3092 "data_total=%u, this_data=%u, max_data=%u, "
3093 "param_offset=%u, data_offset=%u\n",
3094 (unsigned)state->setup_count,
3095 (unsigned)state->total_param, (unsigned)pscnt,
3096 (unsigned)state->max_param_return,
3097 (unsigned)state->total_data, (unsigned)dscnt,
3098 (unsigned)state->max_data_return,
3099 (unsigned)psoff, (unsigned)dsoff));
3102 * All nttrans messages we handle have smb_wct == 19 +
3103 * state->setup_count. Ensure this is so as a sanity check.
3106 if(req->wct != 19 + (state->setup_count/2)) {
3107 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
3108 req->wct, 19 + (state->setup_count/2)));
3112 /* Don't allow more than 128mb for each value. */
3113 if ((state->total_data > (1024*1024*128)) ||
3114 (state->total_param > (1024*1024*128))) {
3115 reply_nterror(req, NT_STATUS_NO_MEMORY);
3116 END_PROFILE(SMBnttrans);
3120 if ((dscnt > state->total_data) || (pscnt > state->total_param))
3123 if (state->total_data) {
3125 if (trans_oob(state->total_data, 0, dscnt)
3126 || trans_oob(smb_len(req->inbuf), dsoff, dscnt)) {
3130 /* Can't use talloc here, the core routines do realloc on the
3131 * params and data. */
3132 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
3133 DEBUG(0,("reply_nttrans: data malloc fail for %u "
3134 "bytes !\n", (unsigned int)state->total_data));
3136 reply_nterror(req, NT_STATUS_NO_MEMORY);
3137 END_PROFILE(SMBnttrans);
3141 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
3144 if (state->total_param) {
3146 if (trans_oob(state->total_param, 0, pscnt)
3147 || trans_oob(smb_len(req->inbuf), psoff, pscnt)) {
3151 /* Can't use talloc here, the core routines do realloc on the
3152 * params and data. */
3153 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
3154 DEBUG(0,("reply_nttrans: param malloc fail for %u "
3155 "bytes !\n", (unsigned int)state->total_param));
3156 SAFE_FREE(state->data);
3158 reply_nterror(req, NT_STATUS_NO_MEMORY);
3159 END_PROFILE(SMBnttrans);
3163 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
3166 state->received_data = dscnt;
3167 state->received_param = pscnt;
3169 if(state->setup_count > 0) {
3170 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
3171 state->setup_count));
3174 * No overflow possible here, state->setup_count is an
3175 * unsigned int, being filled by a single byte from
3176 * CVAL(req->vwv+13, 0) above. The cast in the comparison
3177 * below is not necessary, it's here to clarify things. The
3178 * validity of req->vwv and req->wct has been checked in
3179 * init_smb_request already.
3181 if ((state->setup_count/2) + 19 > (unsigned int)req->wct) {
3185 state->setup = (uint16 *)TALLOC(state, state->setup_count);
3186 if (state->setup == NULL) {
3187 DEBUG(0,("reply_nttrans : Out of memory\n"));
3188 SAFE_FREE(state->data);
3189 SAFE_FREE(state->param);
3191 reply_nterror(req, NT_STATUS_NO_MEMORY);
3192 END_PROFILE(SMBnttrans);
3196 memcpy(state->setup, req->vwv+19, state->setup_count);
3197 dump_data(10, (uint8 *)state->setup, state->setup_count);
3200 if ((state->received_data == state->total_data) &&
3201 (state->received_param == state->total_param)) {
3202 handle_nttrans(conn, state, req);
3203 SAFE_FREE(state->param);
3204 SAFE_FREE(state->data);
3206 END_PROFILE(SMBnttrans);
3210 DLIST_ADD(conn->pending_trans, state);
3212 /* We need to send an interim response then receive the rest
3213 of the parameter/data bytes */
3214 reply_outbuf(req, 0, 0);
3215 show_msg((char *)req->outbuf);
3216 END_PROFILE(SMBnttrans);
3221 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
3222 SAFE_FREE(state->data);
3223 SAFE_FREE(state->param);
3225 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3226 END_PROFILE(SMBnttrans);
3230 /****************************************************************************
3231 Reply to a SMBnttranss
3232 ****************************************************************************/
3234 void reply_nttranss(struct smb_request *req)
3236 connection_struct *conn = req->conn;
3237 uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
3238 struct trans_state *state;
3240 START_PROFILE(SMBnttranss);
3242 show_msg((char *)req->inbuf);
3244 if (req->wct < 18) {
3245 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3246 END_PROFILE(SMBnttranss);
3250 for (state = conn->pending_trans; state != NULL;
3251 state = state->next) {
3252 if (state->mid == req->mid) {
3257 if ((state == NULL) || (state->cmd != SMBnttrans)) {
3258 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3259 END_PROFILE(SMBnttranss);
3263 /* Revise state->total_param and state->total_data in case they have
3264 changed downwards */
3265 if (IVAL(req->vwv+1, 1) < state->total_param) {
3266 state->total_param = IVAL(req->vwv+1, 1);
3268 if (IVAL(req->vwv+3, 1) < state->total_data) {
3269 state->total_data = IVAL(req->vwv+3, 1);
3272 pcnt = IVAL(req->vwv+5, 1);
3273 poff = IVAL(req->vwv+7, 1);
3274 pdisp = IVAL(req->vwv+9, 1);
3276 dcnt = IVAL(req->vwv+11, 1);
3277 doff = IVAL(req->vwv+13, 1);
3278 ddisp = IVAL(req->vwv+15, 1);
3280 state->received_param += pcnt;
3281 state->received_data += dcnt;
3283 if ((state->received_data > state->total_data) ||
3284 (state->received_param > state->total_param))
3288 if (trans_oob(state->total_param, pdisp, pcnt)
3289 || trans_oob(smb_len(req->inbuf), poff, pcnt)) {
3292 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,pcnt);
3296 if (trans_oob(state->total_data, ddisp, dcnt)
3297 || trans_oob(smb_len(req->inbuf), doff, dcnt)) {
3300 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
3303 if ((state->received_param < state->total_param) ||
3304 (state->received_data < state->total_data)) {
3305 END_PROFILE(SMBnttranss);
3309 handle_nttrans(conn, state, req);
3311 DLIST_REMOVE(conn->pending_trans, state);
3312 SAFE_FREE(state->data);
3313 SAFE_FREE(state->param);
3315 END_PROFILE(SMBnttranss);
3320 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
3321 DLIST_REMOVE(conn->pending_trans, state);
3322 SAFE_FREE(state->data);
3323 SAFE_FREE(state->param);
3325 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3326 END_PROFILE(SMBnttranss);