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/globals.h"
24 #include "fake_file.h"
25 #include "../libcli/security/security.h"
26 #include "../librpc/gen_ndr/ndr_security.h"
27 #include "passdb/lookup_sid.h"
29 extern const struct generic_mapping file_generic_mapping;
31 static char *nttrans_realloc(char **ptr, size_t size)
34 smb_panic("nttrans_realloc() called with NULL ptr");
37 *ptr = (char *)SMB_REALLOC(*ptr, size);
41 memset(*ptr,'\0',size);
45 /****************************************************************************
46 Send the required number of replies back.
47 We assume all fields other than the data fields are
48 set correctly for the type of call.
49 HACK ! Always assumes smb_setup field is zero.
50 ****************************************************************************/
52 void send_nt_replies(connection_struct *conn,
53 struct smb_request *req, NTSTATUS nt_error,
54 char *params, int paramsize,
55 char *pdata, int datasize)
57 int data_to_send = datasize;
58 int params_to_send = paramsize;
62 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
63 int alignment_offset = 1;
64 int data_alignment_offset = 0;
65 struct smbd_server_connection *sconn = req->sconn;
66 int max_send = sconn->smb1.sessions.max_send;
69 * If there genuinely are no parameters or data to send just send
73 if(params_to_send == 0 && data_to_send == 0) {
74 reply_outbuf(req, 18, 0);
75 if (NT_STATUS_V(nt_error)) {
76 error_packet_set((char *)req->outbuf,
80 show_msg((char *)req->outbuf);
81 if (!srv_send_smb(sconn,
84 IS_CONN_ENCRYPTED(conn),
86 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
88 TALLOC_FREE(req->outbuf);
93 * When sending params and data ensure that both are nicely aligned.
94 * Only do this alignment when there is also data to send - else
95 * can cause NT redirector problems.
98 if (((params_to_send % 4) != 0) && (data_to_send != 0)) {
99 data_alignment_offset = 4 - (params_to_send % 4);
103 * Space is bufsize minus Netbios over TCP header minus SMB header.
104 * The alignment_offset is to align the param bytes on a four byte
105 * boundary (2 bytes for data len, one byte pad).
106 * NT needs this to work correctly.
109 useable_space = max_send - (smb_size
112 + data_alignment_offset);
114 if (useable_space < 0) {
115 char *msg = talloc_asprintf(
117 "send_nt_replies failed sanity useable_space = %d!!!",
119 DEBUG(0, ("%s\n", msg));
120 exit_server_cleanly(msg);
123 while (params_to_send || data_to_send) {
126 * Calculate whether we will totally or partially fill this packet.
129 total_sent_thistime = params_to_send + data_to_send;
132 * We can never send more than useable_space.
135 total_sent_thistime = MIN(total_sent_thistime, useable_space);
137 reply_outbuf(req, 18,
138 total_sent_thistime + alignment_offset
139 + data_alignment_offset);
142 * We might have had SMBnttranss in req->inbuf, fix that.
144 SCVAL(req->outbuf, smb_com, SMBnttrans);
147 * Set total params and data to be sent.
150 SIVAL(req->outbuf,smb_ntr_TotalParameterCount,paramsize);
151 SIVAL(req->outbuf,smb_ntr_TotalDataCount,datasize);
154 * Calculate how many parameters and data we can fit into
155 * this packet. Parameters get precedence.
158 params_sent_thistime = MIN(params_to_send,useable_space);
159 data_sent_thistime = useable_space - params_sent_thistime;
160 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
162 SIVAL(req->outbuf, smb_ntr_ParameterCount,
163 params_sent_thistime);
165 if(params_sent_thistime == 0) {
166 SIVAL(req->outbuf,smb_ntr_ParameterOffset,0);
167 SIVAL(req->outbuf,smb_ntr_ParameterDisplacement,0);
170 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
171 * parameter bytes, however the first 4 bytes of outbuf are
172 * the Netbios over TCP header. Thus use smb_base() to subtract
173 * them from the calculation.
176 SIVAL(req->outbuf,smb_ntr_ParameterOffset,
177 ((smb_buf(req->outbuf)+alignment_offset)
178 - smb_base(req->outbuf)));
180 * Absolute displacement of param bytes sent in this packet.
183 SIVAL(req->outbuf, smb_ntr_ParameterDisplacement,
188 * Deal with the data portion.
191 SIVAL(req->outbuf, smb_ntr_DataCount, data_sent_thistime);
193 if(data_sent_thistime == 0) {
194 SIVAL(req->outbuf,smb_ntr_DataOffset,0);
195 SIVAL(req->outbuf,smb_ntr_DataDisplacement, 0);
198 * The offset of the data bytes is the offset of the
199 * parameter bytes plus the number of parameters being sent this time.
202 SIVAL(req->outbuf, smb_ntr_DataOffset,
203 ((smb_buf(req->outbuf)+alignment_offset) -
204 smb_base(req->outbuf))
205 + params_sent_thistime + data_alignment_offset);
206 SIVAL(req->outbuf,smb_ntr_DataDisplacement, pd - pdata);
210 * Copy the param bytes into the packet.
213 if(params_sent_thistime) {
214 if (alignment_offset != 0) {
215 memset(smb_buf(req->outbuf), 0,
218 memcpy((smb_buf(req->outbuf)+alignment_offset), pp,
219 params_sent_thistime);
223 * Copy in the data bytes
226 if(data_sent_thistime) {
227 if (data_alignment_offset != 0) {
228 memset((smb_buf(req->outbuf)+alignment_offset+
229 params_sent_thistime), 0,
230 data_alignment_offset);
232 memcpy(smb_buf(req->outbuf)+alignment_offset
233 +params_sent_thistime+data_alignment_offset,
234 pd,data_sent_thistime);
237 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
238 params_sent_thistime, data_sent_thistime, useable_space));
239 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
240 params_to_send, data_to_send, paramsize, datasize));
242 if (NT_STATUS_V(nt_error)) {
243 error_packet_set((char *)req->outbuf,
248 /* Send the packet */
249 show_msg((char *)req->outbuf);
250 if (!srv_send_smb(sconn,
253 IS_CONN_ENCRYPTED(conn),
255 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
258 TALLOC_FREE(req->outbuf);
260 pp += params_sent_thistime;
261 pd += data_sent_thistime;
263 params_to_send -= params_sent_thistime;
264 data_to_send -= data_sent_thistime;
270 if(params_to_send < 0 || data_to_send < 0) {
271 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
272 params_to_send, data_to_send));
273 exit_server_cleanly("send_nt_replies: internal error");
278 /****************************************************************************
279 Reply to an NT create and X call on a pipe
280 ****************************************************************************/
282 static void nt_open_pipe(char *fname, connection_struct *conn,
283 struct smb_request *req, int *ppnum)
288 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
290 /* Strip \\ off the name. */
293 status = open_np_file(req, fname, &fsp);
294 if (!NT_STATUS_IS_OK(status)) {
295 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
296 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
300 reply_nterror(req, status);
308 /****************************************************************************
309 Reply to an NT create and X call for pipes.
310 ****************************************************************************/
312 static void do_ntcreate_pipe_open(connection_struct *conn,
313 struct smb_request *req)
318 uint32 flags = IVAL(req->vwv+3, 1);
319 TALLOC_CTX *ctx = talloc_tos();
321 srvstr_pull_req_talloc(ctx, req, &fname, req->buf, STR_TERMINATE);
324 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
328 nt_open_pipe(fname, conn, req, &pnum);
336 * Deal with pipe return.
339 if (flags & EXTENDED_RESPONSE_REQUIRED) {
340 /* This is very strange. We
341 * return 50 words, but only set
342 * the wcnt to 42 ? It's definately
343 * what happens on the wire....
345 reply_outbuf(req, 50, 0);
346 SCVAL(req->outbuf,smb_wct,42);
348 reply_outbuf(req, 34, 0);
351 p = (char *)req->outbuf + smb_vwv2;
355 SIVAL(p,0,FILE_WAS_OPENED);
358 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
361 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
363 SSVAL(p,2, 0x5FF); /* ? */
366 if (flags & EXTENDED_RESPONSE_REQUIRED) {
368 SIVAL(p,0,FILE_GENERIC_ALL);
370 * For pipes W2K3 seems to return
372 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
374 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
377 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
382 struct case_semantics_state {
383 connection_struct *conn;
386 bool short_case_preserve;
389 /****************************************************************************
390 Restore case semantics.
391 ****************************************************************************/
393 static int restore_case_semantics(struct case_semantics_state *state)
395 state->conn->case_sensitive = state->case_sensitive;
396 state->conn->case_preserve = state->case_preserve;
397 state->conn->short_case_preserve = state->short_case_preserve;
401 /****************************************************************************
403 ****************************************************************************/
405 static struct case_semantics_state *set_posix_case_semantics(TALLOC_CTX *mem_ctx,
406 connection_struct *conn)
408 struct case_semantics_state *result;
410 if (!(result = talloc(mem_ctx, struct case_semantics_state))) {
415 result->case_sensitive = conn->case_sensitive;
416 result->case_preserve = conn->case_preserve;
417 result->short_case_preserve = conn->short_case_preserve;
420 conn->case_sensitive = True;
421 conn->case_preserve = True;
422 conn->short_case_preserve = True;
424 talloc_set_destructor(result, restore_case_semantics);
429 /****************************************************************************
430 Reply to an NT create and X call.
431 ****************************************************************************/
433 void reply_ntcreate_and_X(struct smb_request *req)
435 connection_struct *conn = req->conn;
436 struct smb_filename *smb_fname = NULL;
440 uint32 file_attributes;
442 uint32 create_disposition;
443 uint32 create_options;
445 uint64_t allocation_size;
446 /* Breakout the oplock request bits so we can set the
447 reply bits separately. */
449 SMB_OFF_T file_len = 0;
451 files_struct *fsp = NULL;
453 struct timespec create_timespec;
454 struct timespec c_timespec;
455 struct timespec a_timespec;
456 struct timespec m_timespec;
457 struct timespec write_time_ts;
460 uint8_t oplock_granted = NO_OPLOCK_RETURN;
461 struct case_semantics_state *case_state = NULL;
462 TALLOC_CTX *ctx = talloc_tos();
464 START_PROFILE(SMBntcreateX);
467 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
471 flags = IVAL(req->vwv+3, 1);
472 access_mask = IVAL(req->vwv+7, 1);
473 file_attributes = IVAL(req->vwv+13, 1);
474 share_access = IVAL(req->vwv+15, 1);
475 create_disposition = IVAL(req->vwv+17, 1);
476 create_options = IVAL(req->vwv+19, 1);
477 root_dir_fid = (uint16)IVAL(req->vwv+5, 1);
479 allocation_size = (uint64_t)IVAL(req->vwv+9, 1);
480 #ifdef LARGE_SMB_OFF_T
481 allocation_size |= (((uint64_t)IVAL(req->vwv+11, 1)) << 32);
484 srvstr_get_path_req(ctx, req, &fname, (const char *)req->buf,
485 STR_TERMINATE, &status);
487 if (!NT_STATUS_IS_OK(status)) {
488 reply_nterror(req, status);
492 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
493 "file_attributes = 0x%x, share_access = 0x%x, "
494 "create_disposition = 0x%x create_options = 0x%x "
495 "root_dir_fid = 0x%x, fname = %s\n",
497 (unsigned int)access_mask,
498 (unsigned int)file_attributes,
499 (unsigned int)share_access,
500 (unsigned int)create_disposition,
501 (unsigned int)create_options,
502 (unsigned int)root_dir_fid,
506 * we need to remove ignored bits when they come directly from the client
507 * because we reuse some of them for internal stuff
509 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
512 * If it's an IPC, use the pipe handler.
516 if (lp_nt_pipe_support()) {
517 do_ntcreate_pipe_open(conn, req);
520 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
524 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
525 if (oplock_request) {
526 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
530 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
531 case_state = set_posix_case_semantics(ctx, conn);
533 reply_nterror(req, NT_STATUS_NO_MEMORY);
538 status = filename_convert(ctx,
540 req->flags2 & FLAGS2_DFS_PATHNAMES,
546 TALLOC_FREE(case_state);
548 if (!NT_STATUS_IS_OK(status)) {
549 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
551 NT_STATUS_PATH_NOT_COVERED,
555 reply_nterror(req, status);
560 * Bug #6898 - clients using Windows opens should
561 * never be able to set this attribute into the
564 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
566 status = SMB_VFS_CREATE_FILE(
569 root_dir_fid, /* root_dir_fid */
570 smb_fname, /* fname */
571 access_mask, /* access_mask */
572 share_access, /* share_access */
573 create_disposition, /* create_disposition*/
574 create_options, /* create_options */
575 file_attributes, /* file_attributes */
576 oplock_request, /* oplock_request */
577 allocation_size, /* allocation_size */
578 0, /* private_flags */
584 if (!NT_STATUS_IS_OK(status)) {
585 if (open_was_deferred(req->mid)) {
586 /* We have re-scheduled this call, no error. */
589 reply_openerror(req, status);
593 /* Ensure we're pointing at the correct stat struct. */
594 TALLOC_FREE(smb_fname);
595 smb_fname = fsp->fsp_name;
598 * If the caller set the extended oplock request bit
599 * and we granted one (by whatever means) - set the
600 * correct bit for extended oplock reply.
603 if (oplock_request &&
604 (lp_fake_oplocks(SNUM(conn))
605 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
608 * Exclusive oplock granted
611 if (flags & REQUEST_BATCH_OPLOCK) {
612 oplock_granted = BATCH_OPLOCK_RETURN;
614 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
616 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
617 oplock_granted = LEVEL_II_OPLOCK_RETURN;
619 oplock_granted = NO_OPLOCK_RETURN;
622 file_len = smb_fname->st.st_ex_size;
624 if (flags & EXTENDED_RESPONSE_REQUIRED) {
625 /* This is very strange. We
626 * return 50 words, but only set
627 * the wcnt to 42 ? It's definately
628 * what happens on the wire....
630 reply_outbuf(req, 50, 0);
631 SCVAL(req->outbuf,smb_wct,42);
633 reply_outbuf(req, 34, 0);
636 p = (char *)req->outbuf + smb_vwv2;
638 SCVAL(p, 0, oplock_granted);
641 SSVAL(p,0,fsp->fnum);
643 if ((create_disposition == FILE_SUPERSEDE)
644 && (info == FILE_WAS_OVERWRITTEN)) {
645 SIVAL(p,0,FILE_WAS_SUPERSEDED);
651 fattr = dos_mode(conn, smb_fname);
653 fattr = FILE_ATTRIBUTE_NORMAL;
656 /* Deal with other possible opens having a modified
658 ZERO_STRUCT(write_time_ts);
659 get_file_infos(fsp->file_id, 0, NULL, &write_time_ts);
660 if (!null_timespec(write_time_ts)) {
661 update_stat_ex_mtime(&smb_fname->st, write_time_ts);
665 create_timespec = get_create_timespec(conn, fsp, smb_fname);
666 a_timespec = smb_fname->st.st_ex_atime;
667 m_timespec = smb_fname->st.st_ex_mtime;
668 c_timespec = get_change_timespec(conn, fsp, smb_fname);
670 if (lp_dos_filetime_resolution(SNUM(conn))) {
671 dos_filetime_timespec(&create_timespec);
672 dos_filetime_timespec(&a_timespec);
673 dos_filetime_timespec(&m_timespec);
674 dos_filetime_timespec(&c_timespec);
677 put_long_date_timespec(conn->ts_res, p, create_timespec); /* create time. */
679 put_long_date_timespec(conn->ts_res, p, a_timespec); /* access time */
681 put_long_date_timespec(conn->ts_res, p, m_timespec); /* write time */
683 put_long_date_timespec(conn->ts_res, p, c_timespec); /* change time */
685 SIVAL(p,0,fattr); /* File Attributes. */
687 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn,fsp,&smb_fname->st));
689 SOFF_T(p,0,file_len);
691 if (flags & EXTENDED_RESPONSE_REQUIRED) {
692 uint16_t file_status = (NO_EAS|NO_SUBSTREAMS|NO_REPARSETAG);
693 size_t num_names = 0;
694 unsigned int num_streams;
695 struct stream_struct *streams = NULL;
697 /* Do we have any EA's ? */
698 status = get_ea_names_from_file(ctx, conn, fsp,
699 smb_fname->base_name, NULL, &num_names);
700 if (NT_STATUS_IS_OK(status) && num_names) {
701 file_status &= ~NO_EAS;
703 status = SMB_VFS_STREAMINFO(conn, NULL, smb_fname->base_name, ctx,
704 &num_streams, &streams);
705 /* There is always one stream, ::$DATA. */
706 if (NT_STATUS_IS_OK(status) && num_streams > 1) {
707 file_status &= ~NO_SUBSTREAMS;
709 TALLOC_FREE(streams);
710 SSVAL(p,2,file_status);
713 SCVAL(p,0,fsp->is_directory ? 1 : 0);
715 if (flags & EXTENDED_RESPONSE_REQUIRED) {
718 if (fsp->is_directory ||
719 can_write_to_file(conn, smb_fname)) {
720 perms = FILE_GENERIC_ALL;
722 perms = FILE_GENERIC_READ|FILE_EXECUTE;
727 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n",
728 fsp->fnum, smb_fname_str_dbg(smb_fname)));
732 END_PROFILE(SMBntcreateX);
736 /****************************************************************************
737 Reply to a NT_TRANSACT_CREATE call to open a pipe.
738 ****************************************************************************/
740 static void do_nt_transact_create_pipe(connection_struct *conn,
741 struct smb_request *req,
742 uint16 **ppsetup, uint32 setup_count,
743 char **ppparams, uint32 parameter_count,
744 char **ppdata, uint32 data_count)
747 char *params = *ppparams;
753 TALLOC_CTX *ctx = talloc_tos();
756 * Ensure minimum number of parameters sent.
759 if(parameter_count < 54) {
760 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
761 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
765 flags = IVAL(params,0);
767 srvstr_get_path(ctx, params, req->flags2, &fname, params+53,
768 parameter_count-53, STR_TERMINATE,
770 if (!NT_STATUS_IS_OK(status)) {
771 reply_nterror(req, status);
775 nt_open_pipe(fname, conn, req, &pnum);
782 /* Realloc the size of parameters and data we will return */
783 if (flags & EXTENDED_RESPONSE_REQUIRED) {
784 /* Extended response is 32 more byyes. */
789 params = nttrans_realloc(ppparams, param_len);
791 reply_nterror(req, NT_STATUS_NO_MEMORY);
796 SCVAL(p,0,NO_OPLOCK_RETURN);
801 SIVAL(p,0,FILE_WAS_OPENED);
805 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
808 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
810 SSVAL(p,2, 0x5FF); /* ? */
813 if (flags & EXTENDED_RESPONSE_REQUIRED) {
815 SIVAL(p,0,FILE_GENERIC_ALL);
817 * For pipes W2K3 seems to return
819 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
821 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
824 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
826 /* Send the required number of replies */
827 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
832 /****************************************************************************
833 Internal fn to set security descriptors.
834 ****************************************************************************/
836 NTSTATUS set_sd(files_struct *fsp, uint8_t *data, uint32_t sd_len,
837 uint32_t security_info_sent)
839 struct security_descriptor *psd = NULL;
843 return NT_STATUS_INVALID_PARAMETER;
846 if (!CAN_WRITE(fsp->conn)) {
847 return NT_STATUS_ACCESS_DENIED;
850 if (!lp_nt_acl_support(SNUM(fsp->conn))) {
854 status = unmarshall_sec_desc(talloc_tos(), data, sd_len, &psd);
856 if (!NT_STATUS_IS_OK(status)) {
860 if (psd->owner_sid == NULL) {
861 security_info_sent &= ~SECINFO_OWNER;
863 if (psd->group_sid == NULL) {
864 security_info_sent &= ~SECINFO_GROUP;
867 /* Ensure we have at least one thing set. */
868 if ((security_info_sent & (SECINFO_OWNER|SECINFO_GROUP|SECINFO_DACL|SECINFO_SACL)) == 0) {
869 return NT_STATUS_INVALID_PARAMETER;
872 /* Ensure we have the rights to do this. */
873 if (security_info_sent & SECINFO_OWNER) {
874 if (!(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
875 return NT_STATUS_ACCESS_DENIED;
879 if (security_info_sent & SECINFO_GROUP) {
880 if (!(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
881 return NT_STATUS_ACCESS_DENIED;
885 if (security_info_sent & SECINFO_DACL) {
886 if (!(fsp->access_mask & SEC_STD_WRITE_DAC)) {
887 return NT_STATUS_ACCESS_DENIED;
889 /* Convert all the generic bits. */
891 security_acl_map_generic(psd->dacl, &file_generic_mapping);
895 if (security_info_sent & SECINFO_SACL) {
896 if (!(fsp->access_mask & SEC_FLAG_SYSTEM_SECURITY)) {
897 return NT_STATUS_ACCESS_DENIED;
899 /* Convert all the generic bits. */
901 security_acl_map_generic(psd->sacl, &file_generic_mapping);
905 if (DEBUGLEVEL >= 10) {
906 DEBUG(10,("set_sd for file %s\n", fsp_str_dbg(fsp)));
907 NDR_PRINT_DEBUG(security_descriptor, psd);
910 status = SMB_VFS_FSET_NT_ACL(fsp, security_info_sent, psd);
917 /****************************************************************************
918 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
919 ****************************************************************************/
921 struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
923 struct ea_list *ea_list_head = NULL;
930 while (offset + 4 <= data_size) {
931 size_t next_offset = IVAL(pdata,offset);
932 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
938 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
939 if (next_offset == 0) {
942 offset += next_offset;
948 /****************************************************************************
949 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
950 ****************************************************************************/
952 static void call_nt_transact_create(connection_struct *conn,
953 struct smb_request *req,
954 uint16 **ppsetup, uint32 setup_count,
955 char **ppparams, uint32 parameter_count,
956 char **ppdata, uint32 data_count,
957 uint32 max_data_count)
959 struct smb_filename *smb_fname = NULL;
961 char *params = *ppparams;
962 char *data = *ppdata;
963 /* Breakout the oplock request bits so we can set the reply bits separately. */
965 SMB_OFF_T file_len = 0;
967 files_struct *fsp = NULL;
971 uint32 file_attributes;
973 uint32 create_disposition;
974 uint32 create_options;
976 struct security_descriptor *sd = NULL;
979 struct timespec create_timespec;
980 struct timespec c_timespec;
981 struct timespec a_timespec;
982 struct timespec m_timespec;
983 struct timespec write_time_ts;
984 struct ea_list *ea_list = NULL;
987 uint64_t allocation_size;
989 uint8_t oplock_granted;
990 struct case_semantics_state *case_state = NULL;
991 TALLOC_CTX *ctx = talloc_tos();
993 DEBUG(5,("call_nt_transact_create\n"));
996 * If it's an IPC, use the pipe handler.
1000 if (lp_nt_pipe_support()) {
1001 do_nt_transact_create_pipe(
1003 ppsetup, setup_count,
1004 ppparams, parameter_count,
1005 ppdata, data_count);
1008 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1013 * Ensure minimum number of parameters sent.
1016 if(parameter_count < 54) {
1017 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1018 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1022 flags = IVAL(params,0);
1023 access_mask = IVAL(params,8);
1024 file_attributes = IVAL(params,20);
1025 share_access = IVAL(params,24);
1026 create_disposition = IVAL(params,28);
1027 create_options = IVAL(params,32);
1028 sd_len = IVAL(params,36);
1029 ea_len = IVAL(params,40);
1030 root_dir_fid = (uint16)IVAL(params,4);
1031 allocation_size = (uint64_t)IVAL(params,12);
1032 #ifdef LARGE_SMB_OFF_T
1033 allocation_size |= (((uint64_t)IVAL(params,16)) << 32);
1037 * we need to remove ignored bits when they come directly from the client
1038 * because we reuse some of them for internal stuff
1040 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
1042 /* Ensure the data_len is correct for the sd and ea values given. */
1043 if ((ea_len + sd_len > data_count)
1044 || (ea_len > data_count) || (sd_len > data_count)
1045 || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
1046 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
1047 "%u, data_count = %u\n", (unsigned int)ea_len,
1048 (unsigned int)sd_len, (unsigned int)data_count));
1049 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1054 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
1057 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
1059 if (!NT_STATUS_IS_OK(status)) {
1060 DEBUG(10, ("call_nt_transact_create: "
1061 "unmarshall_sec_desc failed: %s\n",
1062 nt_errstr(status)));
1063 reply_nterror(req, status);
1069 if (!lp_ea_support(SNUM(conn))) {
1070 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
1071 "EA's not supported.\n",
1072 (unsigned int)ea_len));
1073 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
1078 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
1079 "too small (should be more than 10)\n",
1080 (unsigned int)ea_len ));
1081 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1085 /* We have already checked that ea_len <= data_count here. */
1086 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
1088 if (ea_list == NULL) {
1089 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1094 srvstr_get_path(ctx, params, req->flags2, &fname,
1095 params+53, parameter_count-53,
1096 STR_TERMINATE, &status);
1097 if (!NT_STATUS_IS_OK(status)) {
1098 reply_nterror(req, status);
1102 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1103 case_state = set_posix_case_semantics(ctx, conn);
1105 reply_nterror(req, NT_STATUS_NO_MEMORY);
1110 status = filename_convert(ctx,
1112 req->flags2 & FLAGS2_DFS_PATHNAMES,
1118 TALLOC_FREE(case_state);
1120 if (!NT_STATUS_IS_OK(status)) {
1121 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1122 reply_botherror(req,
1123 NT_STATUS_PATH_NOT_COVERED,
1124 ERRSRV, ERRbadpath);
1127 reply_nterror(req, status);
1131 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1132 if (oplock_request) {
1133 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
1138 * Bug #6898 - clients using Windows opens should
1139 * never be able to set this attribute into the
1142 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
1144 status = SMB_VFS_CREATE_FILE(
1147 root_dir_fid, /* root_dir_fid */
1148 smb_fname, /* fname */
1149 access_mask, /* access_mask */
1150 share_access, /* share_access */
1151 create_disposition, /* create_disposition*/
1152 create_options, /* create_options */
1153 file_attributes, /* file_attributes */
1154 oplock_request, /* oplock_request */
1155 allocation_size, /* allocation_size */
1156 0, /* private_flags */
1158 ea_list, /* ea_list */
1162 if(!NT_STATUS_IS_OK(status)) {
1163 if (open_was_deferred(req->mid)) {
1164 /* We have re-scheduled this call, no error. */
1167 reply_openerror(req, status);
1171 /* Ensure we're pointing at the correct stat struct. */
1172 TALLOC_FREE(smb_fname);
1173 smb_fname = fsp->fsp_name;
1176 * If the caller set the extended oplock request bit
1177 * and we granted one (by whatever means) - set the
1178 * correct bit for extended oplock reply.
1181 if (oplock_request &&
1182 (lp_fake_oplocks(SNUM(conn))
1183 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
1186 * Exclusive oplock granted
1189 if (flags & REQUEST_BATCH_OPLOCK) {
1190 oplock_granted = BATCH_OPLOCK_RETURN;
1192 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
1194 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1195 oplock_granted = LEVEL_II_OPLOCK_RETURN;
1197 oplock_granted = NO_OPLOCK_RETURN;
1200 file_len = smb_fname->st.st_ex_size;
1202 /* Realloc the size of parameters and data we will return */
1203 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1204 /* Extended response is 32 more byyes. */
1209 params = nttrans_realloc(ppparams, param_len);
1210 if(params == NULL) {
1211 reply_nterror(req, NT_STATUS_NO_MEMORY);
1216 SCVAL(p, 0, oplock_granted);
1219 SSVAL(p,0,fsp->fnum);
1221 if ((create_disposition == FILE_SUPERSEDE)
1222 && (info == FILE_WAS_OVERWRITTEN)) {
1223 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1229 fattr = dos_mode(conn, smb_fname);
1231 fattr = FILE_ATTRIBUTE_NORMAL;
1234 /* Deal with other possible opens having a modified
1236 ZERO_STRUCT(write_time_ts);
1237 get_file_infos(fsp->file_id, 0, NULL, &write_time_ts);
1238 if (!null_timespec(write_time_ts)) {
1239 update_stat_ex_mtime(&smb_fname->st, write_time_ts);
1243 create_timespec = get_create_timespec(conn, fsp, smb_fname);
1244 a_timespec = smb_fname->st.st_ex_atime;
1245 m_timespec = smb_fname->st.st_ex_mtime;
1246 c_timespec = get_change_timespec(conn, fsp, smb_fname);
1248 if (lp_dos_filetime_resolution(SNUM(conn))) {
1249 dos_filetime_timespec(&create_timespec);
1250 dos_filetime_timespec(&a_timespec);
1251 dos_filetime_timespec(&m_timespec);
1252 dos_filetime_timespec(&c_timespec);
1255 put_long_date_timespec(conn->ts_res, p, create_timespec); /* create time. */
1257 put_long_date_timespec(conn->ts_res, p, a_timespec); /* access time */
1259 put_long_date_timespec(conn->ts_res, p, m_timespec); /* write time */
1261 put_long_date_timespec(conn->ts_res, p, c_timespec); /* change time */
1263 SIVAL(p,0,fattr); /* File Attributes. */
1265 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn, fsp, &smb_fname->st));
1267 SOFF_T(p,0,file_len);
1269 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1270 uint16_t file_status = (NO_EAS|NO_SUBSTREAMS|NO_REPARSETAG);
1271 size_t num_names = 0;
1272 unsigned int num_streams;
1273 struct stream_struct *streams = NULL;
1275 /* Do we have any EA's ? */
1276 status = get_ea_names_from_file(ctx, conn, fsp,
1277 smb_fname->base_name, NULL, &num_names);
1278 if (NT_STATUS_IS_OK(status) && num_names) {
1279 file_status &= ~NO_EAS;
1281 status = SMB_VFS_STREAMINFO(conn, NULL, smb_fname->base_name, ctx,
1282 &num_streams, &streams);
1283 /* There is always one stream, ::$DATA. */
1284 if (NT_STATUS_IS_OK(status) && num_streams > 1) {
1285 file_status &= ~NO_SUBSTREAMS;
1287 TALLOC_FREE(streams);
1288 SSVAL(p,2,file_status);
1291 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1293 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1296 if (fsp->is_directory ||
1297 can_write_to_file(conn, smb_fname)) {
1298 perms = FILE_GENERIC_ALL;
1300 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1305 DEBUG(5,("call_nt_transact_create: open name = %s\n",
1306 smb_fname_str_dbg(smb_fname)));
1308 /* Send the required number of replies */
1309 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1314 /****************************************************************************
1315 Reply to a NT CANCEL request.
1316 conn POINTER CAN BE NULL HERE !
1317 ****************************************************************************/
1319 void reply_ntcancel(struct smb_request *req)
1322 * Go through and cancel any pending change notifies.
1325 START_PROFILE(SMBntcancel);
1326 srv_cancel_sign_response(req->sconn);
1327 remove_pending_change_notify_requests_by_mid(req->sconn, req->mid);
1328 remove_pending_lock_requests_by_mid_smb1(req->sconn, req->mid);
1330 DEBUG(3,("reply_ntcancel: cancel called on mid = %llu.\n",
1331 (unsigned long long)req->mid));
1333 END_PROFILE(SMBntcancel);
1337 /****************************************************************************
1339 ****************************************************************************/
1341 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
1342 connection_struct *conn,
1343 struct smb_request *req,
1344 struct smb_filename *smb_fname_src,
1345 struct smb_filename *smb_fname_dst,
1348 files_struct *fsp1,*fsp2;
1352 NTSTATUS status = NT_STATUS_OK;
1355 if (!CAN_WRITE(conn)) {
1356 status = NT_STATUS_MEDIA_WRITE_PROTECTED;
1360 /* Source must already exist. */
1361 if (!VALID_STAT(smb_fname_src->st)) {
1362 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
1366 /* Ensure attributes match. */
1367 fattr = dos_mode(conn, smb_fname_src);
1368 if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
1369 status = NT_STATUS_NO_SUCH_FILE;
1373 /* Disallow if dst file already exists. */
1374 if (VALID_STAT(smb_fname_dst->st)) {
1375 status = NT_STATUS_OBJECT_NAME_COLLISION;
1379 /* No links from a directory. */
1380 if (S_ISDIR(smb_fname_src->st.st_ex_mode)) {
1381 status = NT_STATUS_FILE_IS_A_DIRECTORY;
1385 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1386 smb_fname_str_dbg(smb_fname_src),
1387 smb_fname_str_dbg(smb_fname_dst)));
1389 status = SMB_VFS_CREATE_FILE(
1392 0, /* root_dir_fid */
1393 smb_fname_src, /* fname */
1394 FILE_READ_DATA|FILE_READ_ATTRIBUTES|
1395 FILE_READ_EA, /* access_mask */
1396 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1398 FILE_OPEN, /* create_disposition*/
1399 0, /* create_options */
1400 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
1401 NO_OPLOCK, /* oplock_request */
1402 0, /* allocation_size */
1403 0, /* private_flags */
1409 if (!NT_STATUS_IS_OK(status)) {
1413 status = SMB_VFS_CREATE_FILE(
1416 0, /* root_dir_fid */
1417 smb_fname_dst, /* fname */
1418 FILE_WRITE_DATA|FILE_WRITE_ATTRIBUTES|
1419 FILE_WRITE_EA, /* access_mask */
1420 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1422 FILE_CREATE, /* create_disposition*/
1423 0, /* create_options */
1424 fattr, /* file_attributes */
1425 NO_OPLOCK, /* oplock_request */
1426 0, /* allocation_size */
1427 0, /* private_flags */
1433 if (!NT_STATUS_IS_OK(status)) {
1434 close_file(NULL, fsp1, ERROR_CLOSE);
1438 if (smb_fname_src->st.st_ex_size) {
1439 ret = vfs_transfer_file(fsp1, fsp2, smb_fname_src->st.st_ex_size);
1443 * As we are opening fsp1 read-only we only expect
1444 * an error on close on fsp2 if we are out of space.
1445 * Thus we don't look at the error return from the
1448 close_file(NULL, fsp1, NORMAL_CLOSE);
1450 /* Ensure the modtime is set correctly on the destination file. */
1451 set_close_write_time(fsp2, smb_fname_src->st.st_ex_mtime);
1453 status = close_file(NULL, fsp2, NORMAL_CLOSE);
1455 /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
1456 creates the file. This isn't the correct thing to do in the copy
1458 if (!parent_dirname(talloc_tos(), smb_fname_dst->base_name, &parent,
1460 status = NT_STATUS_NO_MEMORY;
1463 file_set_dosmode(conn, smb_fname_dst, fattr, parent, false);
1464 TALLOC_FREE(parent);
1466 if (ret < (SMB_OFF_T)smb_fname_src->st.st_ex_size) {
1467 status = NT_STATUS_DISK_FULL;
1471 if (!NT_STATUS_IS_OK(status)) {
1472 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1473 nt_errstr(status), smb_fname_str_dbg(smb_fname_src),
1474 smb_fname_str_dbg(smb_fname_dst)));
1480 /****************************************************************************
1481 Reply to a NT rename request.
1482 ****************************************************************************/
1484 void reply_ntrename(struct smb_request *req)
1486 connection_struct *conn = req->conn;
1487 struct smb_filename *smb_fname_old = NULL;
1488 struct smb_filename *smb_fname_new = NULL;
1489 char *oldname = NULL;
1490 char *newname = NULL;
1493 bool src_has_wcard = False;
1494 bool dest_has_wcard = False;
1496 uint32_t ucf_flags_src = 0;
1497 uint32_t ucf_flags_dst = 0;
1499 TALLOC_CTX *ctx = talloc_tos();
1500 bool stream_rename = false;
1502 START_PROFILE(SMBntrename);
1505 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1509 attrs = SVAL(req->vwv+0, 0);
1510 rename_type = SVAL(req->vwv+1, 0);
1512 p = (const char *)req->buf + 1;
1513 p += srvstr_get_path_req_wcard(ctx, req, &oldname, p, STR_TERMINATE,
1514 &status, &src_has_wcard);
1515 if (!NT_STATUS_IS_OK(status)) {
1516 reply_nterror(req, status);
1520 if (ms_has_wild(oldname)) {
1521 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1526 p += srvstr_get_path_req_wcard(ctx, req, &newname, p, STR_TERMINATE,
1527 &status, &dest_has_wcard);
1528 if (!NT_STATUS_IS_OK(status)) {
1529 reply_nterror(req, status);
1533 if (!lp_posix_pathnames()) {
1534 /* The newname must begin with a ':' if the
1535 oldname contains a ':'. */
1536 if (strchr_m(oldname, ':')) {
1537 if (newname[0] != ':') {
1538 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1541 stream_rename = true;
1546 * If this is a rename operation, allow wildcards and save the
1547 * destination's last component.
1549 if (rename_type == RENAME_FLAG_RENAME) {
1550 ucf_flags_src = UCF_COND_ALLOW_WCARD_LCOMP;
1551 ucf_flags_dst = UCF_COND_ALLOW_WCARD_LCOMP | UCF_SAVE_LCOMP;
1554 /* rename_internals() calls unix_convert(), so don't call it here. */
1555 status = filename_convert(ctx, conn,
1556 req->flags2 & FLAGS2_DFS_PATHNAMES,
1561 if (!NT_STATUS_IS_OK(status)) {
1562 if (NT_STATUS_EQUAL(status,
1563 NT_STATUS_PATH_NOT_COVERED)) {
1564 reply_botherror(req,
1565 NT_STATUS_PATH_NOT_COVERED,
1566 ERRSRV, ERRbadpath);
1569 reply_nterror(req, status);
1573 status = filename_convert(ctx, conn,
1574 req->flags2 & FLAGS2_DFS_PATHNAMES,
1579 if (!NT_STATUS_IS_OK(status)) {
1580 if (NT_STATUS_EQUAL(status,
1581 NT_STATUS_PATH_NOT_COVERED)) {
1582 reply_botherror(req,
1583 NT_STATUS_PATH_NOT_COVERED,
1584 ERRSRV, ERRbadpath);
1587 reply_nterror(req, status);
1591 if (stream_rename) {
1592 /* smb_fname_new must be the same as smb_fname_old. */
1593 TALLOC_FREE(smb_fname_new->base_name);
1594 smb_fname_new->base_name = talloc_strdup(smb_fname_new,
1595 smb_fname_old->base_name);
1596 if (!smb_fname_new->base_name) {
1597 reply_nterror(req, NT_STATUS_NO_MEMORY);
1602 DEBUG(3,("reply_ntrename: %s -> %s\n",
1603 smb_fname_str_dbg(smb_fname_old),
1604 smb_fname_str_dbg(smb_fname_new)));
1606 switch(rename_type) {
1607 case RENAME_FLAG_RENAME:
1608 status = rename_internals(ctx, conn, req,
1609 smb_fname_old, smb_fname_new,
1610 attrs, False, src_has_wcard,
1614 case RENAME_FLAG_HARD_LINK:
1615 if (src_has_wcard || dest_has_wcard) {
1617 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1619 status = hardlink_internals(ctx, conn,
1626 case RENAME_FLAG_COPY:
1627 if (src_has_wcard || dest_has_wcard) {
1629 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1631 status = copy_internals(ctx, conn, req,
1637 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1638 status = NT_STATUS_INVALID_PARAMETER;
1641 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1645 if (!NT_STATUS_IS_OK(status)) {
1646 if (open_was_deferred(req->mid)) {
1647 /* We have re-scheduled this call. */
1651 reply_nterror(req, status);
1655 reply_outbuf(req, 0, 0);
1657 END_PROFILE(SMBntrename);
1661 /****************************************************************************
1662 Reply to a notify change - queue the request and
1663 don't allow a directory to be opened.
1664 ****************************************************************************/
1666 static void smbd_smb1_notify_reply(struct smb_request *req,
1667 NTSTATUS error_code,
1668 uint8_t *buf, size_t len)
1670 send_nt_replies(req->conn, req, error_code, (char *)buf, len, NULL, 0);
1673 static void call_nt_transact_notify_change(connection_struct *conn,
1674 struct smb_request *req,
1678 uint32 parameter_count,
1679 char **ppdata, uint32 data_count,
1680 uint32 max_data_count,
1681 uint32 max_param_count)
1683 uint16 *setup = *ppsetup;
1689 if(setup_count < 6) {
1690 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1694 fsp = file_fsp(req, SVAL(setup,4));
1695 filter = IVAL(setup, 0);
1696 recursive = (SVAL(setup, 6) != 0) ? True : False;
1698 DEBUG(3,("call_nt_transact_notify_change\n"));
1701 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1706 char *filter_string;
1708 if (!(filter_string = notify_filter_string(NULL, filter))) {
1709 reply_nterror(req,NT_STATUS_NO_MEMORY);
1713 DEBUG(3,("call_nt_transact_notify_change: notify change "
1714 "called on %s, filter = %s, recursive = %d\n",
1715 fsp_str_dbg(fsp), filter_string, recursive));
1717 TALLOC_FREE(filter_string);
1720 if((!fsp->is_directory) || (conn != fsp->conn)) {
1721 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1725 if (fsp->notify == NULL) {
1727 status = change_notify_create(fsp, filter, recursive);
1729 if (!NT_STATUS_IS_OK(status)) {
1730 DEBUG(10, ("change_notify_create returned %s\n",
1731 nt_errstr(status)));
1732 reply_nterror(req, status);
1737 if (fsp->notify->num_changes != 0) {
1740 * We've got changes pending, respond immediately
1744 * TODO: write a torture test to check the filtering behaviour
1748 change_notify_reply(req,
1752 smbd_smb1_notify_reply);
1755 * change_notify_reply() above has independently sent its
1762 * No changes pending, queue the request
1765 status = change_notify_add_request(req,
1769 smbd_smb1_notify_reply);
1770 if (!NT_STATUS_IS_OK(status)) {
1771 reply_nterror(req, status);
1776 /****************************************************************************
1777 Reply to an NT transact rename command.
1778 ****************************************************************************/
1780 static void call_nt_transact_rename(connection_struct *conn,
1781 struct smb_request *req,
1782 uint16 **ppsetup, uint32 setup_count,
1783 char **ppparams, uint32 parameter_count,
1784 char **ppdata, uint32 data_count,
1785 uint32 max_data_count)
1787 char *params = *ppparams;
1788 char *new_name = NULL;
1789 files_struct *fsp = NULL;
1790 bool dest_has_wcard = False;
1792 TALLOC_CTX *ctx = talloc_tos();
1794 if(parameter_count < 5) {
1795 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1799 fsp = file_fsp(req, SVAL(params, 0));
1800 if (!check_fsp(conn, req, fsp)) {
1803 srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
1804 parameter_count - 4,
1805 STR_TERMINATE, &status, &dest_has_wcard);
1806 if (!NT_STATUS_IS_OK(status)) {
1807 reply_nterror(req, status);
1812 * W2K3 ignores this request as the RAW-RENAME test
1813 * demonstrates, so we do.
1815 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1817 DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1818 fsp_str_dbg(fsp), new_name));
1823 /******************************************************************************
1824 Fake up a completely empty SD.
1825 *******************************************************************************/
1827 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, struct security_descriptor **ppsd)
1831 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1833 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1834 return NT_STATUS_NO_MEMORY;
1837 return NT_STATUS_OK;
1840 /****************************************************************************
1841 Reply to query a security descriptor.
1842 Callable from SMB2 and SMB2.
1843 If it returns NT_STATUS_BUFFER_TOO_SMALL, pdata_size is initialized with
1845 ****************************************************************************/
1847 NTSTATUS smbd_do_query_security_desc(connection_struct *conn,
1848 TALLOC_CTX *mem_ctx,
1850 uint32_t security_info_wanted,
1851 uint32_t max_data_count,
1852 uint8_t **ppmarshalled_sd,
1856 struct security_descriptor *psd = NULL;
1859 * Get the permissions to return.
1862 if ((security_info_wanted & SECINFO_SACL) &&
1863 !(fsp->access_mask & SEC_FLAG_SYSTEM_SECURITY)) {
1864 return NT_STATUS_ACCESS_DENIED;
1867 if ((security_info_wanted & (SECINFO_DACL|SECINFO_OWNER|SECINFO_GROUP)) &&
1868 !(fsp->access_mask & SEC_STD_READ_CONTROL)) {
1869 return NT_STATUS_ACCESS_DENIED;
1872 if (!lp_nt_acl_support(SNUM(conn))) {
1873 status = get_null_nt_acl(mem_ctx, &psd);
1875 status = SMB_VFS_FGET_NT_ACL(
1876 fsp, security_info_wanted, &psd);
1878 if (!NT_STATUS_IS_OK(status)) {
1882 if (!(security_info_wanted & SECINFO_OWNER)) {
1883 psd->owner_sid = NULL;
1885 if (!(security_info_wanted & SECINFO_GROUP)) {
1886 psd->group_sid = NULL;
1888 if (!(security_info_wanted & SECINFO_DACL)) {
1891 if (!(security_info_wanted & SECINFO_SACL)) {
1895 /* If the SACL/DACL is NULL, but was requested, we mark that it is
1896 * present in the reply to match Windows behavior */
1897 if (psd->sacl == NULL &&
1898 security_info_wanted & SECINFO_SACL)
1899 psd->type |= SEC_DESC_SACL_PRESENT;
1900 if (psd->dacl == NULL &&
1901 security_info_wanted & SECINFO_DACL)
1902 psd->type |= SEC_DESC_DACL_PRESENT;
1904 *psd_size = ndr_size_security_descriptor(psd, 0);
1906 DEBUG(3,("smbd_do_query_security_desc: sd_size = %lu.\n",
1907 (unsigned long)*psd_size));
1909 if (DEBUGLEVEL >= 10) {
1910 DEBUG(10,("smbd_do_query_security_desc for file %s\n",
1912 NDR_PRINT_DEBUG(security_descriptor, psd);
1915 if (max_data_count < *psd_size) {
1917 return NT_STATUS_BUFFER_TOO_SMALL;
1920 status = marshall_sec_desc(mem_ctx, psd,
1921 ppmarshalled_sd, psd_size);
1923 if (!NT_STATUS_IS_OK(status)) {
1929 return NT_STATUS_OK;
1932 /****************************************************************************
1933 SMB1 reply to query a security descriptor.
1934 ****************************************************************************/
1936 static void call_nt_transact_query_security_desc(connection_struct *conn,
1937 struct smb_request *req,
1941 uint32 parameter_count,
1944 uint32 max_data_count)
1946 char *params = *ppparams;
1947 char *data = *ppdata;
1949 uint32 security_info_wanted;
1950 files_struct *fsp = NULL;
1952 uint8_t *marshalled_sd = NULL;
1954 if(parameter_count < 8) {
1955 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1959 fsp = file_fsp(req, SVAL(params,0));
1961 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1965 security_info_wanted = IVAL(params,4);
1967 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, "
1968 "info_wanted = 0x%x\n", fsp_str_dbg(fsp),
1969 (unsigned int)security_info_wanted));
1971 params = nttrans_realloc(ppparams, 4);
1972 if(params == NULL) {
1973 reply_nterror(req, NT_STATUS_NO_MEMORY);
1978 * Get the permissions to return.
1981 status = smbd_do_query_security_desc(conn,
1984 security_info_wanted,
1989 if (NT_STATUS_EQUAL(status, NT_STATUS_BUFFER_TOO_SMALL)) {
1990 SIVAL(params,0,(uint32_t)sd_size);
1991 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
1992 params, 4, NULL, 0);
1996 if (!NT_STATUS_IS_OK(status)) {
1997 reply_nterror(req, status);
2001 SMB_ASSERT(sd_size > 0);
2003 SIVAL(params,0,(uint32_t)sd_size);
2005 if (max_data_count < sd_size) {
2006 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
2007 params, 4, NULL, 0);
2012 * Allocate the data we will return.
2015 data = nttrans_realloc(ppdata, sd_size);
2017 reply_nterror(req, NT_STATUS_NO_MEMORY);
2021 memcpy(data, marshalled_sd, sd_size);
2023 send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
2028 /****************************************************************************
2029 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
2030 ****************************************************************************/
2032 static void call_nt_transact_set_security_desc(connection_struct *conn,
2033 struct smb_request *req,
2037 uint32 parameter_count,
2040 uint32 max_data_count)
2042 char *params= *ppparams;
2043 char *data = *ppdata;
2044 files_struct *fsp = NULL;
2045 uint32 security_info_sent = 0;
2048 if(parameter_count < 8) {
2049 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2053 if((fsp = file_fsp(req, SVAL(params,0))) == NULL) {
2054 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2058 if (!CAN_WRITE(fsp->conn)) {
2059 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2063 if(!lp_nt_acl_support(SNUM(conn))) {
2067 security_info_sent = IVAL(params,4);
2069 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n",
2070 fsp_str_dbg(fsp), (unsigned int)security_info_sent));
2072 if (data_count == 0) {
2073 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2077 status = set_sd(fsp, (uint8 *)data, data_count, security_info_sent);
2079 if (!NT_STATUS_IS_OK(status)) {
2080 reply_nterror(req, status);
2085 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2089 /****************************************************************************
2091 ****************************************************************************/
2093 static void call_nt_transact_ioctl(connection_struct *conn,
2094 struct smb_request *req,
2095 uint16 **ppsetup, uint32 setup_count,
2096 char **ppparams, uint32 parameter_count,
2097 char **ppdata, uint32 data_count,
2098 uint32 max_data_count)
2105 char *pdata = *ppdata;
2107 if (setup_count != 8) {
2108 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
2109 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2113 function = IVAL(*ppsetup, 0);
2114 fidnum = SVAL(*ppsetup, 4);
2115 isFSctl = CVAL(*ppsetup, 6);
2116 compfilter = CVAL(*ppsetup, 7);
2118 DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
2119 function, fidnum, isFSctl, compfilter));
2121 fsp=file_fsp(req, fidnum);
2122 /* this check is done in each implemented function case for now
2123 because I don't want to break anything... --metze
2124 FSP_BELONGS_CONN(fsp,conn);*/
2126 SMB_PERFCOUNT_SET_IOCTL(&req->pcd, function);
2129 case FSCTL_SET_SPARSE:
2131 bool set_sparse = true;
2134 if (data_count >= 1 && pdata[0] == 0) {
2138 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X]set[%u]\n",
2139 fidnum, set_sparse));
2141 if (!check_fsp_open(conn, req, fsp)) {
2145 status = file_set_sparse(conn, fsp, set_sparse);
2146 if (!NT_STATUS_IS_OK(status)) {
2147 DEBUG(9,("FSCTL_SET_SPARSE: fname[%s] set[%u] - %s\n",
2148 smb_fname_str_dbg(fsp->fsp_name), set_sparse, nt_errstr(status)));
2149 reply_nterror(req, status);
2153 DEBUG(10,("FSCTL_SET_SPARSE: fname[%s] set[%u] - %s\n",
2154 smb_fname_str_dbg(fsp->fsp_name), set_sparse, nt_errstr(status)));
2155 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2158 case FSCTL_CREATE_OR_GET_OBJECT_ID:
2160 unsigned char objid[16];
2162 /* This should return the object-id on this file.
2163 * I think I'll make this be the inode+dev. JRA.
2166 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
2168 if (!check_fsp_open(conn, req, fsp)) {
2173 pdata = nttrans_realloc(ppdata, data_count);
2174 if (pdata == NULL) {
2175 reply_nterror(req, NT_STATUS_NO_MEMORY);
2179 /* For backwards compatibility only store the dev/inode. */
2180 push_file_id_16(pdata, &fsp->file_id);
2181 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
2182 push_file_id_16(pdata+32, &fsp->file_id);
2183 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
2188 case FSCTL_GET_REPARSE_POINT:
2189 /* pretend this fail - my winXP does it like this
2193 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2194 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
2197 case FSCTL_SET_REPARSE_POINT:
2198 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
2202 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2203 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
2206 case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
2209 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
2210 * and return their volume names. If max_data_count is 16, then it is just
2211 * asking for the number of volumes and length of the combined names.
2213 * pdata is the data allocated by our caller, but that uses
2214 * total_data_count (which is 0 in our case) rather than max_data_count.
2215 * Allocate the correct amount and return the pointer to let
2216 * it be deallocated when we return.
2218 SHADOW_COPY_DATA *shadow_data = NULL;
2219 TALLOC_CTX *shadow_mem_ctx = NULL;
2220 bool labels = False;
2221 uint32 labels_data_count = 0;
2225 if (!check_fsp_open(conn, req, fsp)) {
2229 if (max_data_count < 16) {
2230 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
2232 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2236 if (max_data_count > 16) {
2240 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
2241 if (shadow_mem_ctx == NULL) {
2242 DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
2243 reply_nterror(req, NT_STATUS_NO_MEMORY);
2247 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
2248 if (shadow_data == NULL) {
2249 DEBUG(0,("TALLOC_ZERO() failed!\n"));
2250 talloc_destroy(shadow_mem_ctx);
2251 reply_nterror(req, NT_STATUS_NO_MEMORY);
2255 shadow_data->mem_ctx = shadow_mem_ctx;
2258 * Call the VFS routine to actually do the work.
2260 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
2261 talloc_destroy(shadow_data->mem_ctx);
2262 if (errno == ENOSYS) {
2263 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
2264 conn->connectpath));
2265 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2268 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
2269 conn->connectpath));
2270 reply_nterror(req, NT_STATUS_UNSUCCESSFUL);
2275 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
2280 data_count = 12+labels_data_count+4;
2283 if (max_data_count<data_count) {
2284 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
2285 max_data_count,data_count));
2286 talloc_destroy(shadow_data->mem_ctx);
2287 reply_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
2291 pdata = nttrans_realloc(ppdata, data_count);
2292 if (pdata == NULL) {
2293 talloc_destroy(shadow_data->mem_ctx);
2294 reply_nterror(req, NT_STATUS_NO_MEMORY);
2300 /* num_volumes 4 bytes */
2301 SIVAL(pdata,0,shadow_data->num_volumes);
2304 /* num_labels 4 bytes */
2305 SIVAL(pdata,4,shadow_data->num_volumes);
2308 /* needed_data_count 4 bytes */
2309 SIVAL(pdata, 8, labels_data_count+4);
2313 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
2314 shadow_data->num_volumes, fsp_str_dbg(fsp)));
2315 if (labels && shadow_data->labels) {
2316 for (i=0;i<shadow_data->num_volumes;i++) {
2317 srvstr_push(pdata, req->flags2,
2318 cur_pdata, shadow_data->labels[i],
2319 2*sizeof(SHADOW_COPY_LABEL),
2320 STR_UNICODE|STR_TERMINATE);
2321 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
2322 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
2326 talloc_destroy(shadow_data->mem_ctx);
2328 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
2334 case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
2336 /* pretend this succeeded -
2338 * we have to send back a list with all files owned by this SID
2340 * but I have to check that --metze
2346 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
2348 if (!check_fsp_open(conn, req, fsp)) {
2352 if (data_count < 8) {
2353 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2357 sid_len = MIN(data_count-4,SID_MAX_SIZE);
2359 /* unknown 4 bytes: this is not the length of the sid :-( */
2360 /*unknown = IVAL(pdata,0);*/
2362 if (!sid_parse(pdata+4,sid_len,&sid)) {
2363 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2366 DEBUGADD(10, ("for SID: %s\n", sid_string_dbg(&sid)));
2368 if (!sid_to_uid(&sid, &uid)) {
2369 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2370 sid_string_dbg(&sid),
2371 (unsigned long)sid_len));
2375 /* we can take a look at the find source :-)
2377 * find ./ -uid $uid -name '*' is what we need here
2380 * and send 4bytes len and then NULL terminated unicode strings
2383 * but I don't know how to deal with the paged results
2384 * (maybe we can hang the result anywhere in the fsp struct)
2386 * we don't send all files at once
2387 * and at the next we should *not* start from the beginning,
2388 * so we have to cache the result
2393 /* this works for now... */
2394 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2397 case FSCTL_QUERY_ALLOCATED_RANGES:
2399 /* FIXME: This is just a dummy reply, telling that all of the
2400 * file is allocated. MKS cp needs that.
2401 * Adding the real allocated ranges via FIEMAP on Linux
2402 * and SEEK_DATA/SEEK_HOLE on Solaris is needed to make
2403 * this FSCTL correct for sparse files.
2406 uint64_t offset, length;
2408 if (!check_fsp_open(conn, req, fsp)) {
2412 if (data_count != 16) {
2413 DEBUG(0,("FSCTL_QUERY_ALLOCATED_RANGES: data_count(%u) != 16 is invalid!\n",
2415 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2419 if (max_data_count < 16) {
2420 DEBUG(0,("FSCTL_QUERY_ALLOCATED_RANGES: max_data_count(%u) < 16 is invalid!\n",
2422 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2426 offset = BVAL(pdata,0);
2427 length = BVAL(pdata,8);
2429 if (offset + length < offset) {
2430 /* No 64-bit integer wrap. */
2431 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2435 status = vfs_stat_fsp(fsp);
2436 if (!NT_STATUS_IS_OK(status)) {
2437 reply_nterror(req, status);
2441 if (offset > fsp->fsp_name->st.st_ex_size ||
2442 fsp->fsp_name->st.st_ex_size == 0 ||
2444 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2446 uint64_t end = offset + length;
2447 end = MIN(end, fsp->fsp_name->st.st_ex_size);
2450 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
2455 case FSCTL_IS_VOLUME_DIRTY:
2456 DEBUG(10,("FSCTL_IS_VOLUME_DIRTY: called on FID[0x%04X] "
2457 "(but not implemented)\n", (int)fidnum));
2459 * http://msdn.microsoft.com/en-us/library/cc232128%28PROT.10%29.aspx
2460 * says we have to respond with NT_STATUS_INVALID_PARAMETER
2462 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2465 /* Only print this once... */
2466 if (!logged_ioctl_message) {
2467 logged_ioctl_message = true;
2468 DEBUG(2,("call_nt_transact_ioctl(0x%x): "
2469 "Currently not implemented.\n",
2474 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2478 #ifdef HAVE_SYS_QUOTAS
2479 /****************************************************************************
2480 Reply to get user quota
2481 ****************************************************************************/
2483 static void call_nt_transact_get_user_quota(connection_struct *conn,
2484 struct smb_request *req,
2488 uint32 parameter_count,
2491 uint32 max_data_count)
2493 NTSTATUS nt_status = NT_STATUS_OK;
2494 char *params = *ppparams;
2495 char *pdata = *ppdata;
2497 int data_len=0,param_len=0;
2500 files_struct *fsp = NULL;
2504 bool start_enum = True;
2505 SMB_NTQUOTA_STRUCT qt;
2506 SMB_NTQUOTA_LIST *tmp_list;
2507 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2512 if (conn->session_info->utok.uid != 0) {
2513 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2514 "[%s]\n", lp_servicename(SNUM(conn)),
2515 conn->session_info->unix_name));
2516 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2521 * Ensure minimum number of parameters sent.
2524 if (parameter_count < 4) {
2525 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2526 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2530 /* maybe we can check the quota_fnum */
2531 fsp = file_fsp(req, SVAL(params,0));
2532 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2533 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2534 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2538 /* the NULL pointer checking for fsp->fake_file_handle->pd
2539 * is done by CHECK_NTQUOTA_HANDLE_OK()
2541 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->private_data;
2543 level = SVAL(params,2);
2545 /* unknown 12 bytes leading in params */
2548 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2549 /* seems that we should continue with the enum here --metze */
2551 if (qt_handle->quota_list!=NULL &&
2552 qt_handle->tmp_list==NULL) {
2555 free_ntquota_list(&(qt_handle->quota_list));
2557 /* Realloc the size of parameters and data we will return */
2559 params = nttrans_realloc(ppparams, param_len);
2560 if(params == NULL) {
2561 reply_nterror(req, NT_STATUS_NO_MEMORY);
2566 SIVAL(params,0,data_len);
2573 case TRANSACT_GET_USER_QUOTA_LIST_START:
2575 if (qt_handle->quota_list==NULL &&
2576 qt_handle->tmp_list==NULL) {
2580 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2581 reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
2585 /* Realloc the size of parameters and data we will return */
2587 params = nttrans_realloc(ppparams, param_len);
2588 if(params == NULL) {
2589 reply_nterror(req, NT_STATUS_NO_MEMORY);
2593 /* we should not trust the value in max_data_count*/
2594 max_data_count = MIN(max_data_count,2048);
2596 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2598 reply_nterror(req, NT_STATUS_NO_MEMORY);
2604 /* set params Size of returned Quota Data 4 bytes*/
2605 /* but set it later when we know it */
2607 /* for each entry push the data */
2610 qt_handle->tmp_list = qt_handle->quota_list;
2613 tmp_list = qt_handle->tmp_list;
2615 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2616 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2618 sid_len = ndr_size_dom_sid(
2619 &tmp_list->quotas->sid, 0);
2620 entry_len = 40 + sid_len;
2622 /* nextoffset entry 4 bytes */
2623 SIVAL(entry,0,entry_len);
2625 /* then the len of the SID 4 bytes */
2626 SIVAL(entry,4,sid_len);
2628 /* unknown data 8 bytes uint64_t */
2629 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-metze*/
2631 /* the used disk space 8 bytes uint64_t */
2632 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2634 /* the soft quotas 8 bytes uint64_t */
2635 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2637 /* the hard quotas 8 bytes uint64_t */
2638 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2640 /* and now the SID */
2641 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2644 qt_handle->tmp_list = tmp_list;
2646 /* overwrite the offset of the last entry */
2647 SIVAL(entry-entry_len,0,0);
2649 data_len = 4+qt_len;
2650 /* overwrite the params quota_data_len */
2651 SIVAL(params,0,data_len);
2655 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2657 /* unknown 4 bytes IVAL(pdata,0) */
2659 if (data_count < 8) {
2660 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2661 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2665 sid_len = IVAL(pdata,4);
2666 /* Ensure this is less than 1mb. */
2667 if (sid_len > (1024*1024)) {
2668 reply_nterror(req, NT_STATUS_NO_MEMORY);
2672 if (data_count < 8+sid_len) {
2673 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2674 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2678 data_len = 4+40+sid_len;
2680 if (max_data_count < data_len) {
2681 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2682 max_data_count, data_len));
2684 SIVAL(params,0,data_len);
2686 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2690 if (!sid_parse(pdata+8,sid_len,&sid)) {
2691 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2695 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2698 * we have to return zero's in all fields
2699 * instead of returning an error here
2704 /* Realloc the size of parameters and data we will return */
2706 params = nttrans_realloc(ppparams, param_len);
2707 if(params == NULL) {
2708 reply_nterror(req, NT_STATUS_NO_MEMORY);
2712 pdata = nttrans_realloc(ppdata, data_len);
2714 reply_nterror(req, NT_STATUS_NO_MEMORY);
2720 /* set params Size of returned Quota Data 4 bytes*/
2721 SIVAL(params,0,data_len);
2723 /* nextoffset entry 4 bytes */
2726 /* then the len of the SID 4 bytes */
2727 SIVAL(entry,4,sid_len);
2729 /* unknown data 8 bytes uint64_t */
2730 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-mezte*/
2732 /* the used disk space 8 bytes uint64_t */
2733 SBIG_UINT(entry,16,qt.usedspace);
2735 /* the soft quotas 8 bytes uint64_t */
2736 SBIG_UINT(entry,24,qt.softlim);
2738 /* the hard quotas 8 bytes uint64_t */
2739 SBIG_UINT(entry,32,qt.hardlim);
2741 /* and now the SID */
2742 sid_linearize(entry+40, sid_len, &sid);
2747 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2748 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2753 send_nt_replies(conn, req, nt_status, params, param_len,
2757 /****************************************************************************
2758 Reply to set user quota
2759 ****************************************************************************/
2761 static void call_nt_transact_set_user_quota(connection_struct *conn,
2762 struct smb_request *req,
2766 uint32 parameter_count,
2769 uint32 max_data_count)
2771 char *params = *ppparams;
2772 char *pdata = *ppdata;
2773 int data_len=0,param_len=0;
2774 SMB_NTQUOTA_STRUCT qt;
2777 files_struct *fsp = NULL;
2782 if (conn->session_info->utok.uid != 0) {
2783 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2784 "[%s]\n", lp_servicename(SNUM(conn)),
2785 conn->session_info->unix_name));
2786 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2791 * Ensure minimum number of parameters sent.
2794 if (parameter_count < 2) {
2795 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2796 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2800 /* maybe we can check the quota_fnum */
2801 fsp = file_fsp(req, SVAL(params,0));
2802 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2803 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2804 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2808 if (data_count < 40) {
2809 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2810 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2814 /* offset to next quota record.
2815 * 4 bytes IVAL(pdata,0)
2820 sid_len = IVAL(pdata,4);
2822 if (data_count < 40+sid_len || (40+sid_len < sid_len)) {
2823 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2824 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2828 /* unknown 8 bytes in pdata
2829 * maybe its the change time in NTTIME
2832 /* the used space 8 bytes (uint64_t)*/
2833 qt.usedspace = (uint64_t)IVAL(pdata,16);
2834 #ifdef LARGE_SMB_OFF_T
2835 qt.usedspace |= (((uint64_t)IVAL(pdata,20)) << 32);
2836 #else /* LARGE_SMB_OFF_T */
2837 if ((IVAL(pdata,20) != 0)&&
2838 ((qt.usedspace != 0xFFFFFFFF)||
2839 (IVAL(pdata,20)!=0xFFFFFFFF))) {
2840 /* more than 32 bits? */
2841 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2844 #endif /* LARGE_SMB_OFF_T */
2846 /* the soft quotas 8 bytes (uint64_t)*/
2847 qt.softlim = (uint64_t)IVAL(pdata,24);
2848 #ifdef LARGE_SMB_OFF_T
2849 qt.softlim |= (((uint64_t)IVAL(pdata,28)) << 32);
2850 #else /* LARGE_SMB_OFF_T */
2851 if ((IVAL(pdata,28) != 0)&&
2852 ((qt.softlim != 0xFFFFFFFF)||
2853 (IVAL(pdata,28)!=0xFFFFFFFF))) {
2854 /* more than 32 bits? */
2855 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2858 #endif /* LARGE_SMB_OFF_T */
2860 /* the hard quotas 8 bytes (uint64_t)*/
2861 qt.hardlim = (uint64_t)IVAL(pdata,32);
2862 #ifdef LARGE_SMB_OFF_T
2863 qt.hardlim |= (((uint64_t)IVAL(pdata,36)) << 32);
2864 #else /* LARGE_SMB_OFF_T */
2865 if ((IVAL(pdata,36) != 0)&&
2866 ((qt.hardlim != 0xFFFFFFFF)||
2867 (IVAL(pdata,36)!=0xFFFFFFFF))) {
2868 /* more than 32 bits? */
2869 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2872 #endif /* LARGE_SMB_OFF_T */
2874 if (!sid_parse(pdata+40,sid_len,&sid)) {
2875 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2879 DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid)));
2881 /* 44 unknown bytes left... */
2883 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2884 reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
2888 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2891 #endif /* HAVE_SYS_QUOTAS */
2893 static void handle_nttrans(connection_struct *conn,
2894 struct trans_state *state,
2895 struct smb_request *req)
2897 if (get_Protocol() >= PROTOCOL_NT1) {
2898 req->flags2 |= 0x40; /* IS_LONG_NAME */
2899 SSVAL(req->inbuf,smb_flg2,req->flags2);
2903 SMB_PERFCOUNT_SET_SUBOP(&req->pcd, state->call);
2905 /* Now we must call the relevant NT_TRANS function */
2906 switch(state->call) {
2907 case NT_TRANSACT_CREATE:
2909 START_PROFILE(NT_transact_create);
2910 call_nt_transact_create(
2912 &state->setup, state->setup_count,
2913 &state->param, state->total_param,
2914 &state->data, state->total_data,
2915 state->max_data_return);
2916 END_PROFILE(NT_transact_create);
2920 case NT_TRANSACT_IOCTL:
2922 START_PROFILE(NT_transact_ioctl);
2923 call_nt_transact_ioctl(
2925 &state->setup, state->setup_count,
2926 &state->param, state->total_param,
2927 &state->data, state->total_data,
2928 state->max_data_return);
2929 END_PROFILE(NT_transact_ioctl);
2933 case NT_TRANSACT_SET_SECURITY_DESC:
2935 START_PROFILE(NT_transact_set_security_desc);
2936 call_nt_transact_set_security_desc(
2938 &state->setup, state->setup_count,
2939 &state->param, state->total_param,
2940 &state->data, state->total_data,
2941 state->max_data_return);
2942 END_PROFILE(NT_transact_set_security_desc);
2946 case NT_TRANSACT_NOTIFY_CHANGE:
2948 START_PROFILE(NT_transact_notify_change);
2949 call_nt_transact_notify_change(
2951 &state->setup, state->setup_count,
2952 &state->param, state->total_param,
2953 &state->data, state->total_data,
2954 state->max_data_return,
2955 state->max_param_return);
2956 END_PROFILE(NT_transact_notify_change);
2960 case NT_TRANSACT_RENAME:
2962 START_PROFILE(NT_transact_rename);
2963 call_nt_transact_rename(
2965 &state->setup, state->setup_count,
2966 &state->param, state->total_param,
2967 &state->data, state->total_data,
2968 state->max_data_return);
2969 END_PROFILE(NT_transact_rename);
2973 case NT_TRANSACT_QUERY_SECURITY_DESC:
2975 START_PROFILE(NT_transact_query_security_desc);
2976 call_nt_transact_query_security_desc(
2978 &state->setup, state->setup_count,
2979 &state->param, state->total_param,
2980 &state->data, state->total_data,
2981 state->max_data_return);
2982 END_PROFILE(NT_transact_query_security_desc);
2986 #ifdef HAVE_SYS_QUOTAS
2987 case NT_TRANSACT_GET_USER_QUOTA:
2989 START_PROFILE(NT_transact_get_user_quota);
2990 call_nt_transact_get_user_quota(
2992 &state->setup, state->setup_count,
2993 &state->param, state->total_param,
2994 &state->data, state->total_data,
2995 state->max_data_return);
2996 END_PROFILE(NT_transact_get_user_quota);
3000 case NT_TRANSACT_SET_USER_QUOTA:
3002 START_PROFILE(NT_transact_set_user_quota);
3003 call_nt_transact_set_user_quota(
3005 &state->setup, state->setup_count,
3006 &state->param, state->total_param,
3007 &state->data, state->total_data,
3008 state->max_data_return);
3009 END_PROFILE(NT_transact_set_user_quota);
3012 #endif /* HAVE_SYS_QUOTAS */
3015 /* Error in request */
3016 DEBUG(0,("handle_nttrans: Unknown request %d in "
3017 "nttrans call\n", state->call));
3018 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
3024 /****************************************************************************
3025 Reply to a SMBNTtrans.
3026 ****************************************************************************/
3028 void reply_nttrans(struct smb_request *req)
3030 connection_struct *conn = req->conn;
3035 uint16 function_code;
3037 struct trans_state *state;
3039 START_PROFILE(SMBnttrans);
3041 if (req->wct < 19) {
3042 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3043 END_PROFILE(SMBnttrans);
3047 pscnt = IVAL(req->vwv+9, 1);
3048 psoff = IVAL(req->vwv+11, 1);
3049 dscnt = IVAL(req->vwv+13, 1);
3050 dsoff = IVAL(req->vwv+15, 1);
3051 function_code = SVAL(req->vwv+18, 0);
3053 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
3054 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
3055 END_PROFILE(SMBnttrans);
3059 result = allow_new_trans(conn->pending_trans, req->mid);
3060 if (!NT_STATUS_IS_OK(result)) {
3061 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
3062 reply_nterror(req, result);
3063 END_PROFILE(SMBnttrans);
3067 if ((state = TALLOC_P(conn, struct trans_state)) == NULL) {
3068 reply_nterror(req, NT_STATUS_NO_MEMORY);
3069 END_PROFILE(SMBnttrans);
3073 state->cmd = SMBnttrans;
3075 state->mid = req->mid;
3076 state->vuid = req->vuid;
3077 state->total_data = IVAL(req->vwv+3, 1);
3079 state->total_param = IVAL(req->vwv+1, 1);
3080 state->param = NULL;
3081 state->max_data_return = IVAL(req->vwv+7, 1);
3082 state->max_param_return = IVAL(req->vwv+5, 1);
3084 /* setup count is in *words* */
3085 state->setup_count = 2*CVAL(req->vwv+17, 1);
3086 state->setup = NULL;
3087 state->call = function_code;
3089 DEBUG(10, ("num_setup=%u, "
3090 "param_total=%u, this_param=%u, max_param=%u, "
3091 "data_total=%u, this_data=%u, max_data=%u, "
3092 "param_offset=%u, data_offset=%u\n",
3093 (unsigned)state->setup_count,
3094 (unsigned)state->total_param, (unsigned)pscnt,
3095 (unsigned)state->max_param_return,
3096 (unsigned)state->total_data, (unsigned)dscnt,
3097 (unsigned)state->max_data_return,
3098 (unsigned)psoff, (unsigned)dsoff));
3101 * All nttrans messages we handle have smb_wct == 19 +
3102 * state->setup_count. Ensure this is so as a sanity check.
3105 if(req->wct != 19 + (state->setup_count/2)) {
3106 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
3107 req->wct, 19 + (state->setup_count/2)));
3111 /* Don't allow more than 128mb for each value. */
3112 if ((state->total_data > (1024*1024*128)) ||
3113 (state->total_param > (1024*1024*128))) {
3114 reply_nterror(req, NT_STATUS_NO_MEMORY);
3115 END_PROFILE(SMBnttrans);
3119 if ((dscnt > state->total_data) || (pscnt > state->total_param))
3122 if (state->total_data) {
3124 if (trans_oob(state->total_data, 0, dscnt)
3125 || trans_oob(smb_len(req->inbuf), dsoff, dscnt)) {
3129 /* Can't use talloc here, the core routines do realloc on the
3130 * params and data. */
3131 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
3132 DEBUG(0,("reply_nttrans: data malloc fail for %u "
3133 "bytes !\n", (unsigned int)state->total_data));
3135 reply_nterror(req, NT_STATUS_NO_MEMORY);
3136 END_PROFILE(SMBnttrans);
3140 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
3143 if (state->total_param) {
3145 if (trans_oob(state->total_param, 0, pscnt)
3146 || trans_oob(smb_len(req->inbuf), psoff, pscnt)) {
3150 /* Can't use talloc here, the core routines do realloc on the
3151 * params and data. */
3152 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
3153 DEBUG(0,("reply_nttrans: param malloc fail for %u "
3154 "bytes !\n", (unsigned int)state->total_param));
3155 SAFE_FREE(state->data);
3157 reply_nterror(req, NT_STATUS_NO_MEMORY);
3158 END_PROFILE(SMBnttrans);
3162 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
3165 state->received_data = dscnt;
3166 state->received_param = pscnt;
3168 if(state->setup_count > 0) {
3169 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
3170 state->setup_count));
3173 * No overflow possible here, state->setup_count is an
3174 * unsigned int, being filled by a single byte from
3175 * CVAL(req->vwv+13, 0) above. The cast in the comparison
3176 * below is not necessary, it's here to clarify things. The
3177 * validity of req->vwv and req->wct has been checked in
3178 * init_smb_request already.
3180 if ((state->setup_count/2) + 19 > (unsigned int)req->wct) {
3184 state->setup = (uint16 *)TALLOC(state, state->setup_count);
3185 if (state->setup == NULL) {
3186 DEBUG(0,("reply_nttrans : Out of memory\n"));
3187 SAFE_FREE(state->data);
3188 SAFE_FREE(state->param);
3190 reply_nterror(req, NT_STATUS_NO_MEMORY);
3191 END_PROFILE(SMBnttrans);
3195 memcpy(state->setup, req->vwv+19, state->setup_count);
3196 dump_data(10, (uint8 *)state->setup, state->setup_count);
3199 if ((state->received_data == state->total_data) &&
3200 (state->received_param == state->total_param)) {
3201 handle_nttrans(conn, state, req);
3202 SAFE_FREE(state->param);
3203 SAFE_FREE(state->data);
3205 END_PROFILE(SMBnttrans);
3209 DLIST_ADD(conn->pending_trans, state);
3211 /* We need to send an interim response then receive the rest
3212 of the parameter/data bytes */
3213 reply_outbuf(req, 0, 0);
3214 show_msg((char *)req->outbuf);
3215 END_PROFILE(SMBnttrans);
3220 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
3221 SAFE_FREE(state->data);
3222 SAFE_FREE(state->param);
3224 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3225 END_PROFILE(SMBnttrans);
3229 /****************************************************************************
3230 Reply to a SMBnttranss
3231 ****************************************************************************/
3233 void reply_nttranss(struct smb_request *req)
3235 connection_struct *conn = req->conn;
3236 uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
3237 struct trans_state *state;
3239 START_PROFILE(SMBnttranss);
3241 show_msg((char *)req->inbuf);
3243 if (req->wct < 18) {
3244 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3245 END_PROFILE(SMBnttranss);
3249 for (state = conn->pending_trans; state != NULL;
3250 state = state->next) {
3251 if (state->mid == req->mid) {
3256 if ((state == NULL) || (state->cmd != SMBnttrans)) {
3257 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3258 END_PROFILE(SMBnttranss);
3262 /* Revise state->total_param and state->total_data in case they have
3263 changed downwards */
3264 if (IVAL(req->vwv+1, 1) < state->total_param) {
3265 state->total_param = IVAL(req->vwv+1, 1);
3267 if (IVAL(req->vwv+3, 1) < state->total_data) {
3268 state->total_data = IVAL(req->vwv+3, 1);
3271 pcnt = IVAL(req->vwv+5, 1);
3272 poff = IVAL(req->vwv+7, 1);
3273 pdisp = IVAL(req->vwv+9, 1);
3275 dcnt = IVAL(req->vwv+11, 1);
3276 doff = IVAL(req->vwv+13, 1);
3277 ddisp = IVAL(req->vwv+15, 1);
3279 state->received_param += pcnt;
3280 state->received_data += dcnt;
3282 if ((state->received_data > state->total_data) ||
3283 (state->received_param > state->total_param))
3287 if (trans_oob(state->total_param, pdisp, pcnt)
3288 || trans_oob(smb_len(req->inbuf), poff, pcnt)) {
3291 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,pcnt);
3295 if (trans_oob(state->total_data, ddisp, dcnt)
3296 || trans_oob(smb_len(req->inbuf), doff, dcnt)) {
3299 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
3302 if ((state->received_param < state->total_param) ||
3303 (state->received_data < state->total_data)) {
3304 END_PROFILE(SMBnttranss);
3308 handle_nttrans(conn, state, req);
3310 DLIST_REMOVE(conn->pending_trans, state);
3311 SAFE_FREE(state->data);
3312 SAFE_FREE(state->param);
3314 END_PROFILE(SMBnttranss);
3319 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
3320 DLIST_REMOVE(conn->pending_trans, state);
3321 SAFE_FREE(state->data);
3322 SAFE_FREE(state->param);
3324 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3325 END_PROFILE(SMBnttranss);