2 Unix SMB/CIFS implementation.
3 SMB NT transaction handling
4 Copyright (C) Jeremy Allison 1994-2007
5 Copyright (C) Stefan (metze) Metzmacher 2003
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "smbd/globals.h"
24 extern enum protocol_types Protocol;
25 extern const struct generic_mapping file_generic_mapping;
27 static char *nttrans_realloc(char **ptr, size_t size)
30 smb_panic("nttrans_realloc() called with NULL ptr");
33 *ptr = (char *)SMB_REALLOC(*ptr, size);
37 memset(*ptr,'\0',size);
41 /****************************************************************************
42 Send the required number of replies back.
43 We assume all fields other than the data fields are
44 set correctly for the type of call.
45 HACK ! Always assumes smb_setup field is zero.
46 ****************************************************************************/
48 void send_nt_replies(connection_struct *conn,
49 struct smb_request *req, NTSTATUS nt_error,
50 char *params, int paramsize,
51 char *pdata, int datasize)
53 int data_to_send = datasize;
54 int params_to_send = paramsize;
58 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
59 int alignment_offset = 3;
60 int data_alignment_offset = 0;
61 struct smbd_server_connection *sconn = smbd_server_conn;
62 int max_send = sconn->smb1.sessions.max_send;
65 * If there genuinely are no parameters or data to send just send
69 if(params_to_send == 0 && data_to_send == 0) {
70 reply_outbuf(req, 18, 0);
71 if (NT_STATUS_V(nt_error)) {
72 error_packet_set((char *)req->outbuf,
76 show_msg((char *)req->outbuf);
81 * When sending params and data ensure that both are nicely aligned.
82 * Only do this alignment when there is also data to send - else
83 * can cause NT redirector problems.
86 if (((params_to_send % 4) != 0) && (data_to_send != 0)) {
87 data_alignment_offset = 4 - (params_to_send % 4);
91 * Space is bufsize minus Netbios over TCP header minus SMB header.
92 * The alignment_offset is to align the param bytes on a four byte
93 * boundary (2 bytes for data len, one byte pad).
94 * NT needs this to work correctly.
97 useable_space = max_send - (smb_size
100 + data_alignment_offset);
102 if (useable_space < 0) {
103 char *msg = talloc_asprintf(
105 "send_nt_replies failed sanity useable_space = %d!!!",
107 DEBUG(0, ("%s\n", msg));
108 exit_server_cleanly(msg);
111 while (params_to_send || data_to_send) {
114 * Calculate whether we will totally or partially fill this packet.
117 total_sent_thistime = params_to_send + data_to_send;
120 * We can never send more than useable_space.
123 total_sent_thistime = MIN(total_sent_thistime, useable_space);
125 reply_outbuf(req, 18,
126 total_sent_thistime + alignment_offset
127 + data_alignment_offset);
130 * We might have had SMBnttranss in req->inbuf, fix that.
132 SCVAL(req->outbuf, smb_com, SMBnttrans);
135 * Set total params and data to be sent.
138 SIVAL(req->outbuf,smb_ntr_TotalParameterCount,paramsize);
139 SIVAL(req->outbuf,smb_ntr_TotalDataCount,datasize);
142 * Calculate how many parameters and data we can fit into
143 * this packet. Parameters get precedence.
146 params_sent_thistime = MIN(params_to_send,useable_space);
147 data_sent_thistime = useable_space - params_sent_thistime;
148 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
150 SIVAL(req->outbuf, smb_ntr_ParameterCount,
151 params_sent_thistime);
153 if(params_sent_thistime == 0) {
154 SIVAL(req->outbuf,smb_ntr_ParameterOffset,0);
155 SIVAL(req->outbuf,smb_ntr_ParameterDisplacement,0);
158 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
159 * parameter bytes, however the first 4 bytes of outbuf are
160 * the Netbios over TCP header. Thus use smb_base() to subtract
161 * them from the calculation.
164 SIVAL(req->outbuf,smb_ntr_ParameterOffset,
165 ((smb_buf(req->outbuf)+alignment_offset)
166 - smb_base(req->outbuf)));
168 * Absolute displacement of param bytes sent in this packet.
171 SIVAL(req->outbuf, smb_ntr_ParameterDisplacement,
176 * Deal with the data portion.
179 SIVAL(req->outbuf, smb_ntr_DataCount, data_sent_thistime);
181 if(data_sent_thistime == 0) {
182 SIVAL(req->outbuf,smb_ntr_DataOffset,0);
183 SIVAL(req->outbuf,smb_ntr_DataDisplacement, 0);
186 * The offset of the data bytes is the offset of the
187 * parameter bytes plus the number of parameters being sent this time.
190 SIVAL(req->outbuf, smb_ntr_DataOffset,
191 ((smb_buf(req->outbuf)+alignment_offset) -
192 smb_base(req->outbuf))
193 + params_sent_thistime + data_alignment_offset);
194 SIVAL(req->outbuf,smb_ntr_DataDisplacement, pd - pdata);
198 * Copy the param bytes into the packet.
201 if(params_sent_thistime) {
202 if (alignment_offset != 0) {
203 memset(smb_buf(req->outbuf), 0,
206 memcpy((smb_buf(req->outbuf)+alignment_offset), pp,
207 params_sent_thistime);
211 * Copy in the data bytes
214 if(data_sent_thistime) {
215 if (data_alignment_offset != 0) {
216 memset((smb_buf(req->outbuf)+alignment_offset+
217 params_sent_thistime), 0,
218 data_alignment_offset);
220 memcpy(smb_buf(req->outbuf)+alignment_offset
221 +params_sent_thistime+data_alignment_offset,
222 pd,data_sent_thistime);
225 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
226 params_sent_thistime, data_sent_thistime, useable_space));
227 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
228 params_to_send, data_to_send, paramsize, datasize));
230 if (NT_STATUS_V(nt_error)) {
231 error_packet_set((char *)req->outbuf,
236 /* Send the packet */
237 show_msg((char *)req->outbuf);
238 if (!srv_send_smb(smbd_server_fd(),
241 IS_CONN_ENCRYPTED(conn),
243 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
246 TALLOC_FREE(req->outbuf);
248 pp += params_sent_thistime;
249 pd += data_sent_thistime;
251 params_to_send -= params_sent_thistime;
252 data_to_send -= data_sent_thistime;
258 if(params_to_send < 0 || data_to_send < 0) {
259 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
260 params_to_send, data_to_send));
261 exit_server_cleanly("send_nt_replies: internal error");
266 /****************************************************************************
267 Is it an NTFS stream name ?
268 An NTFS file name is <path>.<extention>:<stream name>:<stream type>
269 $DATA can be used as both a stream name and a stream type. A missing stream
270 name or type implies $DATA.
272 Both Windows stream names and POSIX files can contain the ':' character.
273 This function first checks for the existence of a colon in the last component
274 of the given name. If the name contains a colon we differentiate between a
275 stream and POSIX file by checking if the latter exists through a POSIX stat.
277 Function assumes we've already chdir() to the "root" directory of fname.
278 ****************************************************************************/
280 bool is_ntfs_stream_name(const char *fname)
282 const char *lastcomp;
283 SMB_STRUCT_STAT sbuf;
285 /* If all pathnames are treated as POSIX we ignore streams. */
286 if (lp_posix_pathnames()) {
290 /* Find the last component of the name. */
291 if ((lastcomp = strrchr_m(fname, '/')) != NULL)
296 /* If there is no colon in the last component, it's not a stream. */
297 if (strchr_m(lastcomp, ':') == NULL)
301 * If file already exists on disk, it's not a stream. The stat must
302 * bypass the vfs layer so streams modules don't intefere.
304 if (sys_stat(fname, &sbuf) == 0) {
305 DEBUG(5, ("is_ntfs_stream_name: file %s contains a ':' but is "
306 "not a stream\n", fname));
313 /****************************************************************************
314 Simple check to determine if the filename is a stream.
315 ***************************************************************************/
316 bool is_ntfs_stream_smb_fname(const struct smb_filename *smb_fname)
318 if (lp_posix_pathnames()) {
322 return smb_fname->stream_name;
325 /****************************************************************************
326 Returns true if the filename's stream == "::$DATA"
327 ***************************************************************************/
328 bool is_ntfs_default_stream_smb_fname(const struct smb_filename *smb_fname)
330 if (!is_ntfs_stream_smb_fname(smb_fname)) {
334 return StrCaseCmp(smb_fname->stream_name, "::$DATA") == 0;
337 /****************************************************************************
338 Reply to an NT create and X call on a pipe
339 ****************************************************************************/
341 static void nt_open_pipe(char *fname, connection_struct *conn,
342 struct smb_request *req, int *ppnum)
347 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
349 /* Strip \\ off the name. */
352 status = open_np_file(req, fname, &fsp);
353 if (!NT_STATUS_IS_OK(status)) {
354 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
355 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
359 reply_nterror(req, status);
367 /****************************************************************************
368 Reply to an NT create and X call for pipes.
369 ****************************************************************************/
371 static void do_ntcreate_pipe_open(connection_struct *conn,
372 struct smb_request *req)
377 uint32 flags = IVAL(req->vwv+3, 1);
378 TALLOC_CTX *ctx = talloc_tos();
380 srvstr_pull_req_talloc(ctx, req, &fname, req->buf, STR_TERMINATE);
383 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
387 nt_open_pipe(fname, conn, req, &pnum);
395 * Deal with pipe return.
398 if (flags & EXTENDED_RESPONSE_REQUIRED) {
399 /* This is very strange. We
400 * return 50 words, but only set
401 * the wcnt to 42 ? It's definately
402 * what happens on the wire....
404 reply_outbuf(req, 50, 0);
405 SCVAL(req->outbuf,smb_wct,42);
407 reply_outbuf(req, 34, 0);
410 p = (char *)req->outbuf + smb_vwv2;
414 SIVAL(p,0,FILE_WAS_OPENED);
417 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
420 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
422 SSVAL(p,2, 0x5FF); /* ? */
425 if (flags & EXTENDED_RESPONSE_REQUIRED) {
427 SIVAL(p,0,FILE_GENERIC_ALL);
429 * For pipes W2K3 seems to return
431 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
433 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
436 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
441 /****************************************************************************
442 Reply to an NT create and X call.
443 ****************************************************************************/
445 void reply_ntcreate_and_X(struct smb_request *req)
447 connection_struct *conn = req->conn;
448 struct smb_filename *smb_fname = NULL;
452 uint32 file_attributes;
454 uint32 create_disposition;
455 uint32 create_options;
457 uint64_t allocation_size;
458 /* Breakout the oplock request bits so we can set the
459 reply bits separately. */
461 SMB_OFF_T file_len = 0;
463 files_struct *fsp = NULL;
465 struct timespec c_timespec;
466 struct timespec a_timespec;
467 struct timespec m_timespec;
470 uint8_t oplock_granted = NO_OPLOCK_RETURN;
471 TALLOC_CTX *ctx = talloc_tos();
473 START_PROFILE(SMBntcreateX);
476 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
480 flags = IVAL(req->vwv+3, 1);
481 access_mask = IVAL(req->vwv+7, 1);
482 file_attributes = IVAL(req->vwv+13, 1);
483 share_access = IVAL(req->vwv+15, 1);
484 create_disposition = IVAL(req->vwv+17, 1);
485 create_options = IVAL(req->vwv+19, 1);
486 root_dir_fid = (uint16)IVAL(req->vwv+5, 1);
488 allocation_size = (uint64_t)IVAL(req->vwv+9, 1);
489 #ifdef LARGE_SMB_OFF_T
490 allocation_size |= (((uint64_t)IVAL(req->vwv+11, 1)) << 32);
493 srvstr_get_path_req(ctx, req, &fname, (const char *)req->buf,
494 STR_TERMINATE, &status);
496 if (!NT_STATUS_IS_OK(status)) {
497 reply_nterror(req, status);
501 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
502 "file_attributes = 0x%x, share_access = 0x%x, "
503 "create_disposition = 0x%x create_options = 0x%x "
504 "root_dir_fid = 0x%x, fname = %s\n",
506 (unsigned int)access_mask,
507 (unsigned int)file_attributes,
508 (unsigned int)share_access,
509 (unsigned int)create_disposition,
510 (unsigned int)create_options,
511 (unsigned int)root_dir_fid,
515 * we need to remove ignored bits when they come directly from the client
516 * because we reuse some of them for internal stuff
518 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
521 * If it's an IPC, use the pipe handler.
525 if (lp_nt_pipe_support()) {
526 do_ntcreate_pipe_open(conn, req);
529 reply_doserror(req, ERRDOS, ERRnoaccess);
533 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
534 if (oplock_request) {
535 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
539 status = filename_convert(ctx,
541 req->flags2 & FLAGS2_DFS_PATHNAMES,
546 if (!NT_STATUS_IS_OK(status)) {
547 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
549 NT_STATUS_PATH_NOT_COVERED,
553 reply_nterror(req, status);
557 status = SMB_VFS_CREATE_FILE(
560 root_dir_fid, /* root_dir_fid */
561 smb_fname, /* fname */
562 access_mask, /* access_mask */
563 share_access, /* share_access */
564 create_disposition, /* create_disposition*/
565 create_options, /* create_options */
566 file_attributes, /* file_attributes */
567 oplock_request, /* oplock_request */
568 allocation_size, /* allocation_size */
574 if (!NT_STATUS_IS_OK(status)) {
575 if (open_was_deferred(req->mid)) {
576 /* We have re-scheduled this call, no error. */
579 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
580 reply_botherror(req, status, ERRDOS, ERRfilexists);
583 reply_nterror(req, status);
589 * If the caller set the extended oplock request bit
590 * and we granted one (by whatever means) - set the
591 * correct bit for extended oplock reply.
594 if (oplock_request &&
595 (lp_fake_oplocks(SNUM(conn))
596 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
599 * Exclusive oplock granted
602 if (flags & REQUEST_BATCH_OPLOCK) {
603 oplock_granted = BATCH_OPLOCK_RETURN;
605 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
607 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
608 oplock_granted = LEVEL_II_OPLOCK_RETURN;
610 oplock_granted = NO_OPLOCK_RETURN;
613 file_len = smb_fname->st.st_ex_size;
614 fattr = dos_mode(conn,fsp->fsp_name,&smb_fname->st);
616 fattr = FILE_ATTRIBUTE_NORMAL;
619 if (flags & EXTENDED_RESPONSE_REQUIRED) {
620 /* This is very strange. We
621 * return 50 words, but only set
622 * the wcnt to 42 ? It's definately
623 * what happens on the wire....
625 reply_outbuf(req, 50, 0);
626 SCVAL(req->outbuf,smb_wct,42);
628 reply_outbuf(req, 34, 0);
631 p = (char *)req->outbuf + smb_vwv2;
633 SCVAL(p, 0, oplock_granted);
636 SSVAL(p,0,fsp->fnum);
638 if ((create_disposition == FILE_SUPERSEDE)
639 && (info == FILE_WAS_OVERWRITTEN)) {
640 SIVAL(p,0,FILE_WAS_SUPERSEDED);
647 c_timespec = smb_fname->st.st_ex_btime;
648 a_timespec = smb_fname->st.st_ex_atime;
649 m_timespec = smb_fname->st.st_ex_mtime;
651 if (lp_dos_filetime_resolution(SNUM(conn))) {
652 dos_filetime_timespec(&c_timespec);
653 dos_filetime_timespec(&a_timespec);
654 dos_filetime_timespec(&m_timespec);
657 put_long_date_timespec(p, c_timespec); /* create time. */
659 put_long_date_timespec(p, a_timespec); /* access time */
661 put_long_date_timespec(p, m_timespec); /* write time */
663 put_long_date_timespec(p, m_timespec); /* change time */
665 SIVAL(p,0,fattr); /* File Attributes. */
667 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn,fsp,&smb_fname->st));
669 SOFF_T(p,0,file_len);
671 if (flags & EXTENDED_RESPONSE_REQUIRED) {
675 SCVAL(p,0,fsp->is_directory ? 1 : 0);
677 if (flags & EXTENDED_RESPONSE_REQUIRED) {
680 if (fsp->is_directory ||
681 can_write_to_file(conn, smb_fname)) {
682 perms = FILE_GENERIC_ALL;
684 perms = FILE_GENERIC_READ|FILE_EXECUTE;
689 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n",
690 fsp->fnum, fsp->fsp_name));
694 TALLOC_FREE(smb_fname);
695 END_PROFILE(SMBntcreateX);
699 /****************************************************************************
700 Reply to a NT_TRANSACT_CREATE call to open a pipe.
701 ****************************************************************************/
703 static void do_nt_transact_create_pipe(connection_struct *conn,
704 struct smb_request *req,
705 uint16 **ppsetup, uint32 setup_count,
706 char **ppparams, uint32 parameter_count,
707 char **ppdata, uint32 data_count)
710 char *params = *ppparams;
716 TALLOC_CTX *ctx = talloc_tos();
719 * Ensure minimum number of parameters sent.
722 if(parameter_count < 54) {
723 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
724 reply_doserror(req, ERRDOS, ERRnoaccess);
728 flags = IVAL(params,0);
730 srvstr_get_path(ctx, params, req->flags2, &fname, params+53,
731 parameter_count-53, STR_TERMINATE,
733 if (!NT_STATUS_IS_OK(status)) {
734 reply_nterror(req, status);
738 nt_open_pipe(fname, conn, req, &pnum);
745 /* Realloc the size of parameters and data we will return */
746 if (flags & EXTENDED_RESPONSE_REQUIRED) {
747 /* Extended response is 32 more byyes. */
752 params = nttrans_realloc(ppparams, param_len);
754 reply_doserror(req, ERRDOS, ERRnomem);
759 SCVAL(p,0,NO_OPLOCK_RETURN);
764 SIVAL(p,0,FILE_WAS_OPENED);
768 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
771 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
773 SSVAL(p,2, 0x5FF); /* ? */
776 if (flags & EXTENDED_RESPONSE_REQUIRED) {
778 SIVAL(p,0,FILE_GENERIC_ALL);
780 * For pipes W2K3 seems to return
782 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
784 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
787 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
789 /* Send the required number of replies */
790 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
795 /****************************************************************************
796 Internal fn to set security descriptors.
797 ****************************************************************************/
799 static NTSTATUS set_sd(files_struct *fsp, uint8 *data, uint32 sd_len,
800 uint32 security_info_sent)
802 SEC_DESC *psd = NULL;
805 if (sd_len == 0 || !lp_nt_acl_support(SNUM(fsp->conn))) {
809 status = unmarshall_sec_desc(talloc_tos(), data, sd_len, &psd);
811 if (!NT_STATUS_IS_OK(status)) {
815 if (psd->owner_sid == NULL) {
816 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
818 if (psd->group_sid == NULL) {
819 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
822 /* Convert all the generic bits. */
823 security_acl_map_generic(psd->dacl, &file_generic_mapping);
824 security_acl_map_generic(psd->sacl, &file_generic_mapping);
826 if (DEBUGLEVEL >= 10) {
827 DEBUG(10,("set_sd for file %s\n", fsp->fsp_name ));
828 NDR_PRINT_DEBUG(security_descriptor, psd);
831 status = SMB_VFS_FSET_NT_ACL(fsp, security_info_sent, psd);
838 /****************************************************************************
839 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
840 ****************************************************************************/
842 static struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
844 struct ea_list *ea_list_head = NULL;
851 while (offset + 4 <= data_size) {
852 size_t next_offset = IVAL(pdata,offset);
853 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
859 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
860 if (next_offset == 0) {
863 offset += next_offset;
869 /****************************************************************************
870 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
871 ****************************************************************************/
873 static void call_nt_transact_create(connection_struct *conn,
874 struct smb_request *req,
875 uint16 **ppsetup, uint32 setup_count,
876 char **ppparams, uint32 parameter_count,
877 char **ppdata, uint32 data_count,
878 uint32 max_data_count)
880 struct smb_filename *smb_fname = NULL;
882 char *params = *ppparams;
883 char *data = *ppdata;
884 /* Breakout the oplock request bits so we can set the reply bits separately. */
886 SMB_OFF_T file_len = 0;
888 files_struct *fsp = NULL;
892 uint32 file_attributes;
894 uint32 create_disposition;
895 uint32 create_options;
897 struct security_descriptor *sd = NULL;
900 struct timespec c_timespec;
901 struct timespec a_timespec;
902 struct timespec m_timespec;
903 struct ea_list *ea_list = NULL;
906 uint64_t allocation_size;
908 uint8_t oplock_granted;
909 TALLOC_CTX *ctx = talloc_tos();
911 DEBUG(5,("call_nt_transact_create\n"));
914 * If it's an IPC, use the pipe handler.
918 if (lp_nt_pipe_support()) {
919 do_nt_transact_create_pipe(
921 ppsetup, setup_count,
922 ppparams, parameter_count,
926 reply_doserror(req, ERRDOS, ERRnoaccess);
931 * Ensure minimum number of parameters sent.
934 if(parameter_count < 54) {
935 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
936 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
940 flags = IVAL(params,0);
941 access_mask = IVAL(params,8);
942 file_attributes = IVAL(params,20);
943 share_access = IVAL(params,24);
944 create_disposition = IVAL(params,28);
945 create_options = IVAL(params,32);
946 sd_len = IVAL(params,36);
947 ea_len = IVAL(params,40);
948 root_dir_fid = (uint16)IVAL(params,4);
949 allocation_size = (uint64_t)IVAL(params,12);
950 #ifdef LARGE_SMB_OFF_T
951 allocation_size |= (((uint64_t)IVAL(params,16)) << 32);
955 * we need to remove ignored bits when they come directly from the client
956 * because we reuse some of them for internal stuff
958 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
960 /* Ensure the data_len is correct for the sd and ea values given. */
961 if ((ea_len + sd_len > data_count)
962 || (ea_len > data_count) || (sd_len > data_count)
963 || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
964 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
965 "%u, data_count = %u\n", (unsigned int)ea_len,
966 (unsigned int)sd_len, (unsigned int)data_count));
967 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
972 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
975 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
977 if (!NT_STATUS_IS_OK(status)) {
978 DEBUG(10, ("call_nt_transact_create: "
979 "unmarshall_sec_desc failed: %s\n",
981 reply_nterror(req, status);
987 if (!lp_ea_support(SNUM(conn))) {
988 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
989 "EA's not supported.\n",
990 (unsigned int)ea_len));
991 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
996 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
997 "too small (should be more than 10)\n",
998 (unsigned int)ea_len ));
999 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1003 /* We have already checked that ea_len <= data_count here. */
1004 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
1006 if (ea_list == NULL) {
1007 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1012 srvstr_get_path(ctx, params, req->flags2, &fname,
1013 params+53, parameter_count-53,
1014 STR_TERMINATE, &status);
1015 if (!NT_STATUS_IS_OK(status)) {
1016 reply_nterror(req, status);
1020 status = filename_convert(ctx,
1022 req->flags2 & FLAGS2_DFS_PATHNAMES,
1027 if (!NT_STATUS_IS_OK(status)) {
1028 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1029 reply_botherror(req,
1030 NT_STATUS_PATH_NOT_COVERED,
1031 ERRSRV, ERRbadpath);
1034 reply_nterror(req, status);
1038 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1039 if (oplock_request) {
1040 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
1044 status = SMB_VFS_CREATE_FILE(
1047 root_dir_fid, /* root_dir_fid */
1048 smb_fname, /* fname */
1049 access_mask, /* access_mask */
1050 share_access, /* share_access */
1051 create_disposition, /* create_disposition*/
1052 create_options, /* create_options */
1053 file_attributes, /* file_attributes */
1054 oplock_request, /* oplock_request */
1055 allocation_size, /* allocation_size */
1057 ea_list, /* ea_list */
1061 if(!NT_STATUS_IS_OK(status)) {
1062 if (open_was_deferred(req->mid)) {
1063 /* We have re-scheduled this call, no error. */
1066 reply_openerror(req, status);
1071 * If the caller set the extended oplock request bit
1072 * and we granted one (by whatever means) - set the
1073 * correct bit for extended oplock reply.
1076 if (oplock_request &&
1077 (lp_fake_oplocks(SNUM(conn))
1078 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
1081 * Exclusive oplock granted
1084 if (flags & REQUEST_BATCH_OPLOCK) {
1085 oplock_granted = BATCH_OPLOCK_RETURN;
1087 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
1089 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1090 oplock_granted = LEVEL_II_OPLOCK_RETURN;
1092 oplock_granted = NO_OPLOCK_RETURN;
1095 file_len = smb_fname->st.st_ex_size;
1096 fattr = dos_mode(conn, fsp->fsp_name, &smb_fname->st);
1098 fattr = FILE_ATTRIBUTE_NORMAL;
1101 /* Realloc the size of parameters and data we will return */
1102 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1103 /* Extended response is 32 more byyes. */
1108 params = nttrans_realloc(ppparams, param_len);
1109 if(params == NULL) {
1110 reply_doserror(req, ERRDOS, ERRnomem);
1115 SCVAL(p, 0, oplock_granted);
1118 SSVAL(p,0,fsp->fnum);
1120 if ((create_disposition == FILE_SUPERSEDE)
1121 && (info == FILE_WAS_OVERWRITTEN)) {
1122 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1129 c_timespec = smb_fname->st.st_ex_btime;
1130 a_timespec = smb_fname->st.st_ex_atime;
1131 m_timespec = smb_fname->st.st_ex_mtime;
1133 if (lp_dos_filetime_resolution(SNUM(conn))) {
1134 dos_filetime_timespec(&c_timespec);
1135 dos_filetime_timespec(&a_timespec);
1136 dos_filetime_timespec(&m_timespec);
1139 put_long_date_timespec(p, c_timespec); /* create time. */
1141 put_long_date_timespec(p, a_timespec); /* access time */
1143 put_long_date_timespec(p, m_timespec); /* write time */
1145 put_long_date_timespec(p, m_timespec); /* change time */
1147 SIVAL(p,0,fattr); /* File Attributes. */
1149 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn, fsp, &smb_fname->st));
1151 SOFF_T(p,0,file_len);
1153 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1157 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1159 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1162 if (fsp->is_directory ||
1163 can_write_to_file(conn, smb_fname)) {
1164 perms = FILE_GENERIC_ALL;
1166 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1171 DEBUG(5,("call_nt_transact_create: open name = %s\n", fsp->fsp_name));
1173 /* Send the required number of replies */
1174 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1176 TALLOC_FREE(smb_fname);
1180 /****************************************************************************
1181 Reply to a NT CANCEL request.
1182 conn POINTER CAN BE NULL HERE !
1183 ****************************************************************************/
1185 void reply_ntcancel(struct smb_request *req)
1188 * Go through and cancel any pending change notifies.
1191 START_PROFILE(SMBntcancel);
1192 srv_cancel_sign_response(smbd_server_conn);
1193 remove_pending_change_notify_requests_by_mid(req->mid);
1194 remove_pending_lock_requests_by_mid(req->mid);
1196 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", req->mid));
1198 END_PROFILE(SMBntcancel);
1202 /****************************************************************************
1204 ****************************************************************************/
1206 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
1207 connection_struct *conn,
1208 struct smb_request *req,
1209 struct smb_filename *smb_fname_src,
1210 struct smb_filename *smb_fname_dst,
1213 char *oldname = NULL;
1214 char *newname = NULL;
1215 files_struct *fsp1,*fsp2;
1219 NTSTATUS status = NT_STATUS_OK;
1222 if (!CAN_WRITE(conn)) {
1223 status = NT_STATUS_MEDIA_WRITE_PROTECTED;
1227 /* Source must already exist. */
1228 if (!VALID_STAT(smb_fname_src->st)) {
1229 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
1233 status = get_full_smb_filename(ctx, smb_fname_src, &oldname);
1234 if (!NT_STATUS_IS_OK(status)) {
1238 /* Ensure attributes match. */
1239 fattr = dos_mode(conn, oldname, &smb_fname_src->st);
1240 if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
1241 status = NT_STATUS_NO_SUCH_FILE;
1245 /* Disallow if dst file already exists. */
1246 if (VALID_STAT(smb_fname_dst->st)) {
1247 status = NT_STATUS_OBJECT_NAME_COLLISION;
1251 /* No links from a directory. */
1252 if (S_ISDIR(smb_fname_src->st.st_ex_mode)) {
1253 status = NT_STATUS_FILE_IS_A_DIRECTORY;
1257 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1258 smb_fname_str_dbg(smb_fname_src),
1259 smb_fname_str_dbg(smb_fname_dst)));
1261 status = SMB_VFS_CREATE_FILE(
1264 0, /* root_dir_fid */
1265 smb_fname_src, /* fname */
1266 FILE_READ_DATA, /* access_mask */
1267 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1269 FILE_OPEN, /* create_disposition*/
1270 0, /* create_options */
1271 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
1272 NO_OPLOCK, /* oplock_request */
1273 0, /* allocation_size */
1279 if (!NT_STATUS_IS_OK(status)) {
1283 status = SMB_VFS_CREATE_FILE(
1286 0, /* root_dir_fid */
1287 smb_fname_dst, /* fname */
1288 FILE_WRITE_DATA, /* access_mask */
1289 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1291 FILE_CREATE, /* create_disposition*/
1292 0, /* create_options */
1293 fattr, /* file_attributes */
1294 NO_OPLOCK, /* oplock_request */
1295 0, /* allocation_size */
1301 if (!NT_STATUS_IS_OK(status)) {
1302 close_file(NULL, fsp1, ERROR_CLOSE);
1306 if (smb_fname_src->st.st_ex_size) {
1307 ret = vfs_transfer_file(fsp1, fsp2, smb_fname_src->st.st_ex_size);
1311 * As we are opening fsp1 read-only we only expect
1312 * an error on close on fsp2 if we are out of space.
1313 * Thus we don't look at the error return from the
1316 close_file(NULL, fsp1, NORMAL_CLOSE);
1318 /* Ensure the modtime is set correctly on the destination file. */
1319 set_close_write_time(fsp2, smb_fname_src->st.st_ex_mtime);
1321 status = close_file(NULL, fsp2, NORMAL_CLOSE);
1323 status = get_full_smb_filename(ctx, smb_fname_dst, &newname);
1324 if (!NT_STATUS_IS_OK(status)) {
1328 /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
1329 creates the file. This isn't the correct thing to do in the copy
1331 if (!parent_dirname(talloc_tos(), newname, &parent, NULL)) {
1332 status = NT_STATUS_NO_MEMORY;
1335 file_set_dosmode(conn, newname, fattr, &smb_fname_dst->st, parent,
1337 TALLOC_FREE(parent);
1339 if (ret < (SMB_OFF_T)smb_fname_src->st.st_ex_size) {
1340 status = NT_STATUS_DISK_FULL;
1344 if (!NT_STATUS_IS_OK(status)) {
1345 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1346 nt_errstr(status), smb_fname_str_dbg(smb_fname_src),
1347 smb_fname_str_dbg(smb_fname_dst)));
1350 TALLOC_FREE(oldname);
1351 TALLOC_FREE(newname);
1356 /****************************************************************************
1357 Reply to a NT rename request.
1358 ****************************************************************************/
1360 void reply_ntrename(struct smb_request *req)
1362 connection_struct *conn = req->conn;
1363 struct smb_filename *smb_fname_old = NULL;
1364 struct smb_filename *smb_fname_new = NULL;
1365 char *oldname = NULL;
1366 char *newname = NULL;
1369 bool src_has_wcard = False;
1370 bool dest_has_wcard = False;
1373 TALLOC_CTX *ctx = talloc_tos();
1375 START_PROFILE(SMBntrename);
1378 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1379 END_PROFILE(SMBntrename);
1383 attrs = SVAL(req->vwv+0, 0);
1384 rename_type = SVAL(req->vwv+1, 0);
1386 p = (const char *)req->buf + 1;
1387 p += srvstr_get_path_req_wcard(ctx, req, &oldname, p, STR_TERMINATE,
1388 &status, &src_has_wcard);
1389 if (!NT_STATUS_IS_OK(status)) {
1390 reply_nterror(req, status);
1391 END_PROFILE(SMBntrename);
1395 if (ms_has_wild(oldname)) {
1396 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1397 END_PROFILE(SMBntrename);
1402 p += srvstr_get_path_req_wcard(ctx, req, &newname, p, STR_TERMINATE,
1403 &status, &dest_has_wcard);
1404 if (!NT_STATUS_IS_OK(status)) {
1405 reply_nterror(req, status);
1406 END_PROFILE(SMBntrename);
1410 status = filename_convert(ctx, conn,
1411 req->flags2 & FLAGS2_DFS_PATHNAMES,
1415 if (!NT_STATUS_IS_OK(status)) {
1416 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1417 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1418 ERRSRV, ERRbadpath);
1419 END_PROFILE(SMBntrename);
1422 reply_nterror(req, status);
1423 END_PROFILE(SMBntrename);
1427 status = filename_convert(ctx, conn,
1428 req->flags2 & FLAGS2_DFS_PATHNAMES,
1432 if (!NT_STATUS_IS_OK(status)) {
1433 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1434 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1435 ERRSRV, ERRbadpath);
1436 END_PROFILE(SMBntrename);
1439 reply_nterror(req, status);
1440 END_PROFILE(SMBntrename);
1444 /* The new name must begin with a ':' if the old name is a stream. */
1445 if (is_ntfs_stream_name(oldname) && (newname[0] != ':')) {
1446 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1447 END_PROFILE(SMBntrename);
1451 DEBUG(3,("reply_ntrename : %s -> %s\n",oldname,newname));
1453 switch(rename_type) {
1454 case RENAME_FLAG_RENAME:
1455 status = rename_internals(ctx, conn, req, oldname,
1456 newname, attrs, False, src_has_wcard,
1457 dest_has_wcard, DELETE_ACCESS);
1459 case RENAME_FLAG_HARD_LINK:
1460 if (src_has_wcard || dest_has_wcard) {
1462 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1464 status = hardlink_internals(ctx,
1470 case RENAME_FLAG_COPY:
1471 if (src_has_wcard || dest_has_wcard) {
1473 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1475 status = copy_internals(ctx, conn, req,
1481 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1482 status = NT_STATUS_INVALID_PARAMETER;
1485 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1489 if (!NT_STATUS_IS_OK(status)) {
1490 if (open_was_deferred(req->mid)) {
1491 /* We have re-scheduled this call. */
1492 END_PROFILE(SMBntrename);
1496 reply_nterror(req, status);
1497 END_PROFILE(SMBntrename);
1501 reply_outbuf(req, 0, 0);
1503 END_PROFILE(SMBntrename);
1507 /****************************************************************************
1508 Reply to a notify change - queue the request and
1509 don't allow a directory to be opened.
1510 ****************************************************************************/
1512 static void call_nt_transact_notify_change(connection_struct *conn,
1513 struct smb_request *req,
1517 uint32 parameter_count,
1518 char **ppdata, uint32 data_count,
1519 uint32 max_data_count,
1520 uint32 max_param_count)
1522 uint16 *setup = *ppsetup;
1528 if(setup_count < 6) {
1529 reply_doserror(req, ERRDOS, ERRbadfunc);
1533 fsp = file_fsp(req, SVAL(setup,4));
1534 filter = IVAL(setup, 0);
1535 recursive = (SVAL(setup, 6) != 0) ? True : False;
1537 DEBUG(3,("call_nt_transact_notify_change\n"));
1540 reply_doserror(req, ERRDOS, ERRbadfid);
1545 char *filter_string;
1547 if (!(filter_string = notify_filter_string(NULL, filter))) {
1548 reply_nterror(req,NT_STATUS_NO_MEMORY);
1552 DEBUG(3,("call_nt_transact_notify_change: notify change "
1553 "called on %s, filter = %s, recursive = %d\n",
1554 fsp->fsp_name, filter_string, recursive));
1556 TALLOC_FREE(filter_string);
1559 if((!fsp->is_directory) || (conn != fsp->conn)) {
1560 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1564 if (fsp->notify == NULL) {
1566 status = change_notify_create(fsp, filter, recursive);
1568 if (!NT_STATUS_IS_OK(status)) {
1569 DEBUG(10, ("change_notify_create returned %s\n",
1570 nt_errstr(status)));
1571 reply_nterror(req, status);
1576 if (fsp->notify->num_changes != 0) {
1579 * We've got changes pending, respond immediately
1583 * TODO: write a torture test to check the filtering behaviour
1587 change_notify_reply(fsp->conn, req, max_param_count,
1591 * change_notify_reply() above has independently sent its
1598 * No changes pending, queue the request
1601 status = change_notify_add_request(req,
1605 if (!NT_STATUS_IS_OK(status)) {
1606 reply_nterror(req, status);
1611 /****************************************************************************
1612 Reply to an NT transact rename command.
1613 ****************************************************************************/
1615 static void call_nt_transact_rename(connection_struct *conn,
1616 struct smb_request *req,
1617 uint16 **ppsetup, uint32 setup_count,
1618 char **ppparams, uint32 parameter_count,
1619 char **ppdata, uint32 data_count,
1620 uint32 max_data_count)
1622 char *params = *ppparams;
1623 char *new_name = NULL;
1624 files_struct *fsp = NULL;
1625 bool dest_has_wcard = False;
1627 TALLOC_CTX *ctx = talloc_tos();
1629 if(parameter_count < 5) {
1630 reply_doserror(req, ERRDOS, ERRbadfunc);
1634 fsp = file_fsp(req, SVAL(params, 0));
1635 if (!check_fsp(conn, req, fsp)) {
1638 srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
1639 parameter_count - 4,
1640 STR_TERMINATE, &status, &dest_has_wcard);
1641 if (!NT_STATUS_IS_OK(status)) {
1642 reply_nterror(req, status);
1647 * W2K3 ignores this request as the RAW-RENAME test
1648 * demonstrates, so we do.
1650 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1652 DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1653 fsp->fsp_name, new_name));
1658 /******************************************************************************
1659 Fake up a completely empty SD.
1660 *******************************************************************************/
1662 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
1666 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1668 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1669 return NT_STATUS_NO_MEMORY;
1672 return NT_STATUS_OK;
1675 /****************************************************************************
1676 Reply to query a security descriptor.
1677 ****************************************************************************/
1679 static void call_nt_transact_query_security_desc(connection_struct *conn,
1680 struct smb_request *req,
1684 uint32 parameter_count,
1687 uint32 max_data_count)
1689 char *params = *ppparams;
1690 char *data = *ppdata;
1691 SEC_DESC *psd = NULL;
1693 uint32 security_info_wanted;
1694 files_struct *fsp = NULL;
1698 if(parameter_count < 8) {
1699 reply_doserror(req, ERRDOS, ERRbadfunc);
1703 fsp = file_fsp(req, SVAL(params,0));
1705 reply_doserror(req, ERRDOS, ERRbadfid);
1709 security_info_wanted = IVAL(params,4);
1711 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, info_wanted = 0x%x\n", fsp->fsp_name,
1712 (unsigned int)security_info_wanted ));
1714 params = nttrans_realloc(ppparams, 4);
1715 if(params == NULL) {
1716 reply_doserror(req, ERRDOS, ERRnomem);
1721 * Get the permissions to return.
1724 if (!lp_nt_acl_support(SNUM(conn))) {
1725 status = get_null_nt_acl(talloc_tos(), &psd);
1727 status = SMB_VFS_FGET_NT_ACL(
1728 fsp, security_info_wanted, &psd);
1730 if (!NT_STATUS_IS_OK(status)) {
1731 reply_nterror(req, status);
1735 /* If the SACL/DACL is NULL, but was requested, we mark that it is
1736 * present in the reply to match Windows behavior */
1737 if (psd->sacl == NULL &&
1738 security_info_wanted & SACL_SECURITY_INFORMATION)
1739 psd->type |= SEC_DESC_SACL_PRESENT;
1740 if (psd->dacl == NULL &&
1741 security_info_wanted & DACL_SECURITY_INFORMATION)
1742 psd->type |= SEC_DESC_DACL_PRESENT;
1744 sd_size = ndr_size_security_descriptor(psd, NULL, 0);
1746 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
1748 if (DEBUGLEVEL >= 10) {
1749 DEBUG(10,("call_nt_transact_query_security_desc for file %s\n", fsp->fsp_name));
1750 NDR_PRINT_DEBUG(security_descriptor, psd);
1753 SIVAL(params,0,(uint32)sd_size);
1755 if (max_data_count < sd_size) {
1756 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
1757 params, 4, *ppdata, 0);
1762 * Allocate the data we will point this at.
1765 data = nttrans_realloc(ppdata, sd_size);
1767 reply_doserror(req, ERRDOS, ERRnomem);
1771 status = marshall_sec_desc(talloc_tos(), psd,
1772 &blob.data, &blob.length);
1774 if (!NT_STATUS_IS_OK(status)) {
1775 reply_nterror(req, status);
1779 SMB_ASSERT(sd_size == blob.length);
1780 memcpy(data, blob.data, sd_size);
1782 send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
1787 /****************************************************************************
1788 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
1789 ****************************************************************************/
1791 static void call_nt_transact_set_security_desc(connection_struct *conn,
1792 struct smb_request *req,
1796 uint32 parameter_count,
1799 uint32 max_data_count)
1801 char *params= *ppparams;
1802 char *data = *ppdata;
1803 files_struct *fsp = NULL;
1804 uint32 security_info_sent = 0;
1807 if(parameter_count < 8) {
1808 reply_doserror(req, ERRDOS, ERRbadfunc);
1812 if((fsp = file_fsp(req, SVAL(params,0))) == NULL) {
1813 reply_doserror(req, ERRDOS, ERRbadfid);
1817 if(!lp_nt_acl_support(SNUM(conn))) {
1821 security_info_sent = IVAL(params,4);
1823 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
1824 (unsigned int)security_info_sent ));
1826 if (data_count == 0) {
1827 reply_doserror(req, ERRDOS, ERRnoaccess);
1831 status = set_sd(fsp, (uint8 *)data, data_count, security_info_sent);
1833 if (!NT_STATUS_IS_OK(status)) {
1834 reply_nterror(req, status);
1839 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1843 /****************************************************************************
1845 ****************************************************************************/
1847 static void call_nt_transact_ioctl(connection_struct *conn,
1848 struct smb_request *req,
1849 uint16 **ppsetup, uint32 setup_count,
1850 char **ppparams, uint32 parameter_count,
1851 char **ppdata, uint32 data_count,
1852 uint32 max_data_count)
1859 char *pdata = *ppdata;
1861 if (setup_count != 8) {
1862 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
1863 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1867 function = IVAL(*ppsetup, 0);
1868 fidnum = SVAL(*ppsetup, 4);
1869 isFSctl = CVAL(*ppsetup, 6);
1870 compfilter = CVAL(*ppsetup, 7);
1872 DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
1873 function, fidnum, isFSctl, compfilter));
1875 fsp=file_fsp(req, fidnum);
1876 /* this check is done in each implemented function case for now
1877 because I don't want to break anything... --metze
1878 FSP_BELONGS_CONN(fsp,conn);*/
1880 SMB_PERFCOUNT_SET_IOCTL(&req->pcd, function);
1883 case FSCTL_SET_SPARSE:
1884 /* pretend this succeeded - tho strictly we should
1885 mark the file sparse (if the local fs supports it)
1886 so we can know if we need to pre-allocate or not */
1888 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
1889 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1892 case FSCTL_CREATE_OR_GET_OBJECT_ID:
1894 unsigned char objid[16];
1896 /* This should return the object-id on this file.
1897 * I think I'll make this be the inode+dev. JRA.
1900 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
1902 if (!fsp_belongs_conn(conn, req, fsp)) {
1907 pdata = nttrans_realloc(ppdata, data_count);
1908 if (pdata == NULL) {
1909 reply_nterror(req, NT_STATUS_NO_MEMORY);
1913 /* For backwards compatibility only store the dev/inode. */
1914 push_file_id_16(pdata, &fsp->file_id);
1915 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
1916 push_file_id_16(pdata+32, &fsp->file_id);
1917 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
1922 case FSCTL_GET_REPARSE_POINT:
1923 /* pretend this fail - my winXP does it like this
1927 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1928 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
1931 case FSCTL_SET_REPARSE_POINT:
1932 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
1936 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1937 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
1940 case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
1943 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
1944 * and return their volume names. If max_data_count is 16, then it is just
1945 * asking for the number of volumes and length of the combined names.
1947 * pdata is the data allocated by our caller, but that uses
1948 * total_data_count (which is 0 in our case) rather than max_data_count.
1949 * Allocate the correct amount and return the pointer to let
1950 * it be deallocated when we return.
1952 SHADOW_COPY_DATA *shadow_data = NULL;
1953 TALLOC_CTX *shadow_mem_ctx = NULL;
1954 bool labels = False;
1955 uint32 labels_data_count = 0;
1959 if (!fsp_belongs_conn(conn, req, fsp)) {
1963 if (max_data_count < 16) {
1964 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
1966 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1970 if (max_data_count > 16) {
1974 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
1975 if (shadow_mem_ctx == NULL) {
1976 DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
1977 reply_nterror(req, NT_STATUS_NO_MEMORY);
1981 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
1982 if (shadow_data == NULL) {
1983 DEBUG(0,("TALLOC_ZERO() failed!\n"));
1984 talloc_destroy(shadow_mem_ctx);
1985 reply_nterror(req, NT_STATUS_NO_MEMORY);
1989 shadow_data->mem_ctx = shadow_mem_ctx;
1992 * Call the VFS routine to actually do the work.
1994 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
1995 talloc_destroy(shadow_data->mem_ctx);
1996 if (errno == ENOSYS) {
1997 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
1998 conn->connectpath));
1999 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2002 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
2003 conn->connectpath));
2004 reply_nterror(req, NT_STATUS_UNSUCCESSFUL);
2009 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
2014 data_count = 12+labels_data_count+4;
2017 if (max_data_count<data_count) {
2018 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
2019 max_data_count,data_count));
2020 talloc_destroy(shadow_data->mem_ctx);
2021 reply_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
2025 pdata = nttrans_realloc(ppdata, data_count);
2026 if (pdata == NULL) {
2027 talloc_destroy(shadow_data->mem_ctx);
2028 reply_nterror(req, NT_STATUS_NO_MEMORY);
2034 /* num_volumes 4 bytes */
2035 SIVAL(pdata,0,shadow_data->num_volumes);
2038 /* num_labels 4 bytes */
2039 SIVAL(pdata,4,shadow_data->num_volumes);
2042 /* needed_data_count 4 bytes */
2043 SIVAL(pdata,8,labels_data_count);
2047 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
2048 shadow_data->num_volumes,fsp->fsp_name));
2049 if (labels && shadow_data->labels) {
2050 for (i=0;i<shadow_data->num_volumes;i++) {
2051 srvstr_push(pdata, req->flags2,
2052 cur_pdata, shadow_data->labels[i],
2053 2*sizeof(SHADOW_COPY_LABEL),
2054 STR_UNICODE|STR_TERMINATE);
2055 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
2056 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
2060 talloc_destroy(shadow_data->mem_ctx);
2062 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
2068 case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
2070 /* pretend this succeeded -
2072 * we have to send back a list with all files owned by this SID
2074 * but I have to check that --metze
2078 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
2080 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
2082 if (!fsp_belongs_conn(conn, req, fsp)) {
2086 /* unknown 4 bytes: this is not the length of the sid :-( */
2087 /*unknown = IVAL(pdata,0);*/
2089 sid_parse(pdata+4,sid_len,&sid);
2090 DEBUGADD(10, ("for SID: %s\n", sid_string_dbg(&sid)));
2092 if (!sid_to_uid(&sid, &uid)) {
2093 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2094 sid_string_dbg(&sid),
2095 (unsigned long)sid_len));
2099 /* we can take a look at the find source :-)
2101 * find ./ -uid $uid -name '*' is what we need here
2104 * and send 4bytes len and then NULL terminated unicode strings
2107 * but I don't know how to deal with the paged results
2108 * (maybe we can hang the result anywhere in the fsp struct)
2110 * we don't send all files at once
2111 * and at the next we should *not* start from the beginning,
2112 * so we have to cache the result
2117 /* this works for now... */
2118 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2122 if (!logged_ioctl_message) {
2123 logged_ioctl_message = true; /* Only print this once... */
2124 DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
2129 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2133 #ifdef HAVE_SYS_QUOTAS
2134 /****************************************************************************
2135 Reply to get user quota
2136 ****************************************************************************/
2138 static void call_nt_transact_get_user_quota(connection_struct *conn,
2139 struct smb_request *req,
2143 uint32 parameter_count,
2146 uint32 max_data_count)
2148 NTSTATUS nt_status = NT_STATUS_OK;
2149 char *params = *ppparams;
2150 char *pdata = *ppdata;
2152 int data_len=0,param_len=0;
2155 files_struct *fsp = NULL;
2159 bool start_enum = True;
2160 SMB_NTQUOTA_STRUCT qt;
2161 SMB_NTQUOTA_LIST *tmp_list;
2162 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2167 if (conn->server_info->utok.uid != 0) {
2168 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2169 "[%s]\n", lp_servicename(SNUM(conn)),
2170 conn->server_info->unix_name));
2171 reply_doserror(req, ERRDOS, ERRnoaccess);
2176 * Ensure minimum number of parameters sent.
2179 if (parameter_count < 4) {
2180 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2181 reply_doserror(req, ERRDOS, ERRinvalidparam);
2185 /* maybe we can check the quota_fnum */
2186 fsp = file_fsp(req, SVAL(params,0));
2187 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2188 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2189 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2193 /* the NULL pointer checking for fsp->fake_file_handle->pd
2194 * is done by CHECK_NTQUOTA_HANDLE_OK()
2196 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->private_data;
2198 level = SVAL(params,2);
2200 /* unknown 12 bytes leading in params */
2203 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2204 /* seems that we should continue with the enum here --metze */
2206 if (qt_handle->quota_list!=NULL &&
2207 qt_handle->tmp_list==NULL) {
2210 free_ntquota_list(&(qt_handle->quota_list));
2212 /* Realloc the size of parameters and data we will return */
2214 params = nttrans_realloc(ppparams, param_len);
2215 if(params == NULL) {
2216 reply_doserror(req, ERRDOS, ERRnomem);
2221 SIVAL(params,0,data_len);
2228 case TRANSACT_GET_USER_QUOTA_LIST_START:
2230 if (qt_handle->quota_list==NULL &&
2231 qt_handle->tmp_list==NULL) {
2235 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2236 reply_doserror(req, ERRSRV, ERRerror);
2240 /* Realloc the size of parameters and data we will return */
2242 params = nttrans_realloc(ppparams, param_len);
2243 if(params == NULL) {
2244 reply_doserror(req, ERRDOS, ERRnomem);
2248 /* we should not trust the value in max_data_count*/
2249 max_data_count = MIN(max_data_count,2048);
2251 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2253 reply_doserror(req, ERRDOS, ERRnomem);
2259 /* set params Size of returned Quota Data 4 bytes*/
2260 /* but set it later when we know it */
2262 /* for each entry push the data */
2265 qt_handle->tmp_list = qt_handle->quota_list;
2268 tmp_list = qt_handle->tmp_list;
2270 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2271 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2273 sid_len = ndr_size_dom_sid(
2274 &tmp_list->quotas->sid, NULL, 0);
2275 entry_len = 40 + sid_len;
2277 /* nextoffset entry 4 bytes */
2278 SIVAL(entry,0,entry_len);
2280 /* then the len of the SID 4 bytes */
2281 SIVAL(entry,4,sid_len);
2283 /* unknown data 8 bytes uint64_t */
2284 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-metze*/
2286 /* the used disk space 8 bytes uint64_t */
2287 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2289 /* the soft quotas 8 bytes uint64_t */
2290 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2292 /* the hard quotas 8 bytes uint64_t */
2293 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2295 /* and now the SID */
2296 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2299 qt_handle->tmp_list = tmp_list;
2301 /* overwrite the offset of the last entry */
2302 SIVAL(entry-entry_len,0,0);
2304 data_len = 4+qt_len;
2305 /* overwrite the params quota_data_len */
2306 SIVAL(params,0,data_len);
2310 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2312 /* unknown 4 bytes IVAL(pdata,0) */
2314 if (data_count < 8) {
2315 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2316 reply_doserror(req, ERRDOS, ERRunknownlevel);
2320 sid_len = IVAL(pdata,4);
2321 /* Ensure this is less than 1mb. */
2322 if (sid_len > (1024*1024)) {
2323 reply_doserror(req, ERRDOS, ERRnomem);
2327 if (data_count < 8+sid_len) {
2328 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2329 reply_doserror(req, ERRDOS, ERRunknownlevel);
2333 data_len = 4+40+sid_len;
2335 if (max_data_count < data_len) {
2336 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2337 max_data_count, data_len));
2339 SIVAL(params,0,data_len);
2341 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2345 sid_parse(pdata+8,sid_len,&sid);
2347 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2350 * we have to return zero's in all fields
2351 * instead of returning an error here
2356 /* Realloc the size of parameters and data we will return */
2358 params = nttrans_realloc(ppparams, param_len);
2359 if(params == NULL) {
2360 reply_doserror(req, ERRDOS, ERRnomem);
2364 pdata = nttrans_realloc(ppdata, data_len);
2366 reply_doserror(req, ERRDOS, ERRnomem);
2372 /* set params Size of returned Quota Data 4 bytes*/
2373 SIVAL(params,0,data_len);
2375 /* nextoffset entry 4 bytes */
2378 /* then the len of the SID 4 bytes */
2379 SIVAL(entry,4,sid_len);
2381 /* unknown data 8 bytes uint64_t */
2382 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-mezte*/
2384 /* the used disk space 8 bytes uint64_t */
2385 SBIG_UINT(entry,16,qt.usedspace);
2387 /* the soft quotas 8 bytes uint64_t */
2388 SBIG_UINT(entry,24,qt.softlim);
2390 /* the hard quotas 8 bytes uint64_t */
2391 SBIG_UINT(entry,32,qt.hardlim);
2393 /* and now the SID */
2394 sid_linearize(entry+40, sid_len, &sid);
2399 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2400 reply_doserror(req, ERRSRV, ERRerror);
2405 send_nt_replies(conn, req, nt_status, params, param_len,
2409 /****************************************************************************
2410 Reply to set user quota
2411 ****************************************************************************/
2413 static void call_nt_transact_set_user_quota(connection_struct *conn,
2414 struct smb_request *req,
2418 uint32 parameter_count,
2421 uint32 max_data_count)
2423 char *params = *ppparams;
2424 char *pdata = *ppdata;
2425 int data_len=0,param_len=0;
2426 SMB_NTQUOTA_STRUCT qt;
2429 files_struct *fsp = NULL;
2434 if (conn->server_info->utok.uid != 0) {
2435 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2436 "[%s]\n", lp_servicename(SNUM(conn)),
2437 conn->server_info->unix_name));
2438 reply_doserror(req, ERRDOS, ERRnoaccess);
2443 * Ensure minimum number of parameters sent.
2446 if (parameter_count < 2) {
2447 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2448 reply_doserror(req, ERRDOS, ERRinvalidparam);
2452 /* maybe we can check the quota_fnum */
2453 fsp = file_fsp(req, SVAL(params,0));
2454 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2455 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2456 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2460 if (data_count < 40) {
2461 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2462 reply_doserror(req, ERRDOS, ERRunknownlevel);
2466 /* offset to next quota record.
2467 * 4 bytes IVAL(pdata,0)
2472 sid_len = IVAL(pdata,4);
2474 if (data_count < 40+sid_len) {
2475 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2476 reply_doserror(req, ERRDOS, ERRunknownlevel);
2480 /* unknown 8 bytes in pdata
2481 * maybe its the change time in NTTIME
2484 /* the used space 8 bytes (uint64_t)*/
2485 qt.usedspace = (uint64_t)IVAL(pdata,16);
2486 #ifdef LARGE_SMB_OFF_T
2487 qt.usedspace |= (((uint64_t)IVAL(pdata,20)) << 32);
2488 #else /* LARGE_SMB_OFF_T */
2489 if ((IVAL(pdata,20) != 0)&&
2490 ((qt.usedspace != 0xFFFFFFFF)||
2491 (IVAL(pdata,20)!=0xFFFFFFFF))) {
2492 /* more than 32 bits? */
2493 reply_doserror(req, ERRDOS, ERRunknownlevel);
2496 #endif /* LARGE_SMB_OFF_T */
2498 /* the soft quotas 8 bytes (uint64_t)*/
2499 qt.softlim = (uint64_t)IVAL(pdata,24);
2500 #ifdef LARGE_SMB_OFF_T
2501 qt.softlim |= (((uint64_t)IVAL(pdata,28)) << 32);
2502 #else /* LARGE_SMB_OFF_T */
2503 if ((IVAL(pdata,28) != 0)&&
2504 ((qt.softlim != 0xFFFFFFFF)||
2505 (IVAL(pdata,28)!=0xFFFFFFFF))) {
2506 /* more than 32 bits? */
2507 reply_doserror(req, ERRDOS, ERRunknownlevel);
2510 #endif /* LARGE_SMB_OFF_T */
2512 /* the hard quotas 8 bytes (uint64_t)*/
2513 qt.hardlim = (uint64_t)IVAL(pdata,32);
2514 #ifdef LARGE_SMB_OFF_T
2515 qt.hardlim |= (((uint64_t)IVAL(pdata,36)) << 32);
2516 #else /* LARGE_SMB_OFF_T */
2517 if ((IVAL(pdata,36) != 0)&&
2518 ((qt.hardlim != 0xFFFFFFFF)||
2519 (IVAL(pdata,36)!=0xFFFFFFFF))) {
2520 /* more than 32 bits? */
2521 reply_doserror(req, ERRDOS, ERRunknownlevel);
2524 #endif /* LARGE_SMB_OFF_T */
2526 sid_parse(pdata+40,sid_len,&sid);
2527 DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid)));
2529 /* 44 unknown bytes left... */
2531 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2532 reply_doserror(req, ERRSRV, ERRerror);
2536 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2539 #endif /* HAVE_SYS_QUOTAS */
2541 static void handle_nttrans(connection_struct *conn,
2542 struct trans_state *state,
2543 struct smb_request *req)
2545 if (Protocol >= PROTOCOL_NT1) {
2546 req->flags2 |= 0x40; /* IS_LONG_NAME */
2547 SSVAL(req->inbuf,smb_flg2,req->flags2);
2551 SMB_PERFCOUNT_SET_SUBOP(&req->pcd, state->call);
2553 /* Now we must call the relevant NT_TRANS function */
2554 switch(state->call) {
2555 case NT_TRANSACT_CREATE:
2557 START_PROFILE(NT_transact_create);
2558 call_nt_transact_create(
2560 &state->setup, state->setup_count,
2561 &state->param, state->total_param,
2562 &state->data, state->total_data,
2563 state->max_data_return);
2564 END_PROFILE(NT_transact_create);
2568 case NT_TRANSACT_IOCTL:
2570 START_PROFILE(NT_transact_ioctl);
2571 call_nt_transact_ioctl(
2573 &state->setup, state->setup_count,
2574 &state->param, state->total_param,
2575 &state->data, state->total_data,
2576 state->max_data_return);
2577 END_PROFILE(NT_transact_ioctl);
2581 case NT_TRANSACT_SET_SECURITY_DESC:
2583 START_PROFILE(NT_transact_set_security_desc);
2584 call_nt_transact_set_security_desc(
2586 &state->setup, state->setup_count,
2587 &state->param, state->total_param,
2588 &state->data, state->total_data,
2589 state->max_data_return);
2590 END_PROFILE(NT_transact_set_security_desc);
2594 case NT_TRANSACT_NOTIFY_CHANGE:
2596 START_PROFILE(NT_transact_notify_change);
2597 call_nt_transact_notify_change(
2599 &state->setup, state->setup_count,
2600 &state->param, state->total_param,
2601 &state->data, state->total_data,
2602 state->max_data_return,
2603 state->max_param_return);
2604 END_PROFILE(NT_transact_notify_change);
2608 case NT_TRANSACT_RENAME:
2610 START_PROFILE(NT_transact_rename);
2611 call_nt_transact_rename(
2613 &state->setup, state->setup_count,
2614 &state->param, state->total_param,
2615 &state->data, state->total_data,
2616 state->max_data_return);
2617 END_PROFILE(NT_transact_rename);
2621 case NT_TRANSACT_QUERY_SECURITY_DESC:
2623 START_PROFILE(NT_transact_query_security_desc);
2624 call_nt_transact_query_security_desc(
2626 &state->setup, state->setup_count,
2627 &state->param, state->total_param,
2628 &state->data, state->total_data,
2629 state->max_data_return);
2630 END_PROFILE(NT_transact_query_security_desc);
2634 #ifdef HAVE_SYS_QUOTAS
2635 case NT_TRANSACT_GET_USER_QUOTA:
2637 START_PROFILE(NT_transact_get_user_quota);
2638 call_nt_transact_get_user_quota(
2640 &state->setup, state->setup_count,
2641 &state->param, state->total_param,
2642 &state->data, state->total_data,
2643 state->max_data_return);
2644 END_PROFILE(NT_transact_get_user_quota);
2648 case NT_TRANSACT_SET_USER_QUOTA:
2650 START_PROFILE(NT_transact_set_user_quota);
2651 call_nt_transact_set_user_quota(
2653 &state->setup, state->setup_count,
2654 &state->param, state->total_param,
2655 &state->data, state->total_data,
2656 state->max_data_return);
2657 END_PROFILE(NT_transact_set_user_quota);
2660 #endif /* HAVE_SYS_QUOTAS */
2663 /* Error in request */
2664 DEBUG(0,("handle_nttrans: Unknown request %d in "
2665 "nttrans call\n", state->call));
2666 reply_doserror(req, ERRSRV, ERRerror);
2672 /****************************************************************************
2673 Reply to a SMBNTtrans.
2674 ****************************************************************************/
2676 void reply_nttrans(struct smb_request *req)
2678 connection_struct *conn = req->conn;
2683 uint16 function_code;
2685 struct trans_state *state;
2687 START_PROFILE(SMBnttrans);
2689 if (req->wct < 19) {
2690 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2691 END_PROFILE(SMBnttrans);
2695 pscnt = IVAL(req->vwv+9, 1);
2696 psoff = IVAL(req->vwv+11, 1);
2697 dscnt = IVAL(req->vwv+13, 1);
2698 dsoff = IVAL(req->vwv+15, 1);
2699 function_code = SVAL(req->vwv+18, 0);
2701 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2702 reply_doserror(req, ERRSRV, ERRaccess);
2703 END_PROFILE(SMBnttrans);
2707 result = allow_new_trans(conn->pending_trans, req->mid);
2708 if (!NT_STATUS_IS_OK(result)) {
2709 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
2710 reply_nterror(req, result);
2711 END_PROFILE(SMBnttrans);
2715 if ((state = TALLOC_P(conn, struct trans_state)) == NULL) {
2716 reply_doserror(req, ERRSRV, ERRaccess);
2717 END_PROFILE(SMBnttrans);
2721 state->cmd = SMBnttrans;
2723 state->mid = req->mid;
2724 state->vuid = req->vuid;
2725 state->total_data = IVAL(req->vwv+3, 1);
2727 state->total_param = IVAL(req->vwv+1, 1);
2728 state->param = NULL;
2729 state->max_data_return = IVAL(req->vwv+7, 1);
2730 state->max_param_return = IVAL(req->vwv+5, 1);
2732 /* setup count is in *words* */
2733 state->setup_count = 2*CVAL(req->vwv+17, 1);
2734 state->setup = NULL;
2735 state->call = function_code;
2737 DEBUG(10, ("num_setup=%u, "
2738 "param_total=%u, this_param=%u, max_param=%u, "
2739 "data_total=%u, this_data=%u, max_data=%u, "
2740 "param_offset=%u, data_offset=%u\n",
2741 (unsigned)state->setup_count,
2742 (unsigned)state->total_param, (unsigned)pscnt,
2743 (unsigned)state->max_param_return,
2744 (unsigned)state->total_data, (unsigned)dscnt,
2745 (unsigned)state->max_data_return,
2746 (unsigned)psoff, (unsigned)dsoff));
2749 * All nttrans messages we handle have smb_wct == 19 +
2750 * state->setup_count. Ensure this is so as a sanity check.
2753 if(req->wct != 19 + (state->setup_count/2)) {
2754 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2755 req->wct, 19 + (state->setup_count/2)));
2759 /* Don't allow more than 128mb for each value. */
2760 if ((state->total_data > (1024*1024*128)) ||
2761 (state->total_param > (1024*1024*128))) {
2762 reply_doserror(req, ERRDOS, ERRnomem);
2763 END_PROFILE(SMBnttrans);
2767 if ((dscnt > state->total_data) || (pscnt > state->total_param))
2770 if (state->total_data) {
2772 if (trans_oob(state->total_data, 0, dscnt)
2773 || trans_oob(smb_len(req->inbuf), dsoff, dscnt)) {
2777 /* Can't use talloc here, the core routines do realloc on the
2778 * params and data. */
2779 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
2780 DEBUG(0,("reply_nttrans: data malloc fail for %u "
2781 "bytes !\n", (unsigned int)state->total_data));
2783 reply_doserror(req, ERRDOS, ERRnomem);
2784 END_PROFILE(SMBnttrans);
2788 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
2791 if (state->total_param) {
2793 if (trans_oob(state->total_param, 0, pscnt)
2794 || trans_oob(smb_len(req->inbuf), psoff, pscnt)) {
2798 /* Can't use talloc here, the core routines do realloc on the
2799 * params and data. */
2800 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
2801 DEBUG(0,("reply_nttrans: param malloc fail for %u "
2802 "bytes !\n", (unsigned int)state->total_param));
2803 SAFE_FREE(state->data);
2805 reply_doserror(req, ERRDOS, ERRnomem);
2806 END_PROFILE(SMBnttrans);
2810 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
2813 state->received_data = dscnt;
2814 state->received_param = pscnt;
2816 if(state->setup_count > 0) {
2817 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2818 state->setup_count));
2821 * No overflow possible here, state->setup_count is an
2822 * unsigned int, being filled by a single byte from
2823 * CVAL(req->vwv+13, 0) above. The cast in the comparison
2824 * below is not necessary, it's here to clarify things. The
2825 * validity of req->vwv and req->wct has been checked in
2826 * init_smb_request already.
2828 if ((state->setup_count/2) + 19 > (unsigned int)req->wct) {
2832 state->setup = (uint16 *)TALLOC(state, state->setup_count);
2833 if (state->setup == NULL) {
2834 DEBUG(0,("reply_nttrans : Out of memory\n"));
2835 SAFE_FREE(state->data);
2836 SAFE_FREE(state->param);
2838 reply_doserror(req, ERRDOS, ERRnomem);
2839 END_PROFILE(SMBnttrans);
2843 memcpy(state->setup, req->vwv+19, state->setup_count);
2844 dump_data(10, (uint8 *)state->setup, state->setup_count);
2847 if ((state->received_data == state->total_data) &&
2848 (state->received_param == state->total_param)) {
2849 handle_nttrans(conn, state, req);
2850 SAFE_FREE(state->param);
2851 SAFE_FREE(state->data);
2853 END_PROFILE(SMBnttrans);
2857 DLIST_ADD(conn->pending_trans, state);
2859 /* We need to send an interim response then receive the rest
2860 of the parameter/data bytes */
2861 reply_outbuf(req, 0, 0);
2862 show_msg((char *)req->outbuf);
2863 END_PROFILE(SMBnttrans);
2868 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
2869 SAFE_FREE(state->data);
2870 SAFE_FREE(state->param);
2872 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2873 END_PROFILE(SMBnttrans);
2877 /****************************************************************************
2878 Reply to a SMBnttranss
2879 ****************************************************************************/
2881 void reply_nttranss(struct smb_request *req)
2883 connection_struct *conn = req->conn;
2884 uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
2885 struct trans_state *state;
2887 START_PROFILE(SMBnttranss);
2889 show_msg((char *)req->inbuf);
2891 if (req->wct < 18) {
2892 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2893 END_PROFILE(SMBnttranss);
2897 for (state = conn->pending_trans; state != NULL;
2898 state = state->next) {
2899 if (state->mid == req->mid) {
2904 if ((state == NULL) || (state->cmd != SMBnttrans)) {
2905 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2906 END_PROFILE(SMBnttranss);
2910 /* Revise state->total_param and state->total_data in case they have
2911 changed downwards */
2912 if (IVAL(req->vwv+1, 1) < state->total_param) {
2913 state->total_param = IVAL(req->vwv+1, 1);
2915 if (IVAL(req->vwv+3, 1) < state->total_data) {
2916 state->total_data = IVAL(req->vwv+3, 1);
2919 pcnt = IVAL(req->vwv+5, 1);
2920 poff = IVAL(req->vwv+7, 1);
2921 pdisp = IVAL(req->vwv+9, 1);
2923 dcnt = IVAL(req->vwv+11, 1);
2924 doff = IVAL(req->vwv+13, 1);
2925 ddisp = IVAL(req->vwv+15, 1);
2927 state->received_param += pcnt;
2928 state->received_data += dcnt;
2930 if ((state->received_data > state->total_data) ||
2931 (state->received_param > state->total_param))
2935 if (trans_oob(state->total_param, pdisp, pcnt)
2936 || trans_oob(smb_len(req->inbuf), poff, pcnt)) {
2939 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,pcnt);
2943 if (trans_oob(state->total_data, ddisp, dcnt)
2944 || trans_oob(smb_len(req->inbuf), doff, dcnt)) {
2947 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
2950 if ((state->received_param < state->total_param) ||
2951 (state->received_data < state->total_data)) {
2952 END_PROFILE(SMBnttranss);
2956 handle_nttrans(conn, state, req);
2958 DLIST_REMOVE(conn->pending_trans, state);
2959 SAFE_FREE(state->data);
2960 SAFE_FREE(state->param);
2962 END_PROFILE(SMBnttranss);
2967 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
2968 DLIST_REMOVE(conn->pending_trans, state);
2969 SAFE_FREE(state->data);
2970 SAFE_FREE(state->param);
2972 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2973 END_PROFILE(SMBnttranss);