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 show_msg((char *)req->outbuf);
76 * When sending params and data ensure that both are nicely aligned.
77 * Only do this alignment when there is also data to send - else
78 * can cause NT redirector problems.
81 if (((params_to_send % 4) != 0) && (data_to_send != 0)) {
82 data_alignment_offset = 4 - (params_to_send % 4);
86 * Space is bufsize minus Netbios over TCP header minus SMB header.
87 * The alignment_offset is to align the param bytes on a four byte
88 * boundary (2 bytes for data len, one byte pad).
89 * NT needs this to work correctly.
92 useable_space = max_send - (smb_size
95 + data_alignment_offset);
97 if (useable_space < 0) {
98 char *msg = talloc_asprintf(
100 "send_nt_replies failed sanity useable_space = %d!!!",
102 DEBUG(0, ("%s\n", msg));
103 exit_server_cleanly(msg);
106 while (params_to_send || data_to_send) {
109 * Calculate whether we will totally or partially fill this packet.
112 total_sent_thistime = params_to_send + data_to_send;
115 * We can never send more than useable_space.
118 total_sent_thistime = MIN(total_sent_thistime, useable_space);
120 reply_outbuf(req, 18,
121 total_sent_thistime + alignment_offset
122 + data_alignment_offset);
125 * We might have had SMBnttranss in req->inbuf, fix that.
127 SCVAL(req->outbuf, smb_com, SMBnttrans);
130 * Set total params and data to be sent.
133 SIVAL(req->outbuf,smb_ntr_TotalParameterCount,paramsize);
134 SIVAL(req->outbuf,smb_ntr_TotalDataCount,datasize);
137 * Calculate how many parameters and data we can fit into
138 * this packet. Parameters get precedence.
141 params_sent_thistime = MIN(params_to_send,useable_space);
142 data_sent_thistime = useable_space - params_sent_thistime;
143 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
145 SIVAL(req->outbuf, smb_ntr_ParameterCount,
146 params_sent_thistime);
148 if(params_sent_thistime == 0) {
149 SIVAL(req->outbuf,smb_ntr_ParameterOffset,0);
150 SIVAL(req->outbuf,smb_ntr_ParameterDisplacement,0);
153 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
154 * parameter bytes, however the first 4 bytes of outbuf are
155 * the Netbios over TCP header. Thus use smb_base() to subtract
156 * them from the calculation.
159 SIVAL(req->outbuf,smb_ntr_ParameterOffset,
160 ((smb_buf(req->outbuf)+alignment_offset)
161 - smb_base(req->outbuf)));
163 * Absolute displacement of param bytes sent in this packet.
166 SIVAL(req->outbuf, smb_ntr_ParameterDisplacement,
171 * Deal with the data portion.
174 SIVAL(req->outbuf, smb_ntr_DataCount, data_sent_thistime);
176 if(data_sent_thistime == 0) {
177 SIVAL(req->outbuf,smb_ntr_DataOffset,0);
178 SIVAL(req->outbuf,smb_ntr_DataDisplacement, 0);
181 * The offset of the data bytes is the offset of the
182 * parameter bytes plus the number of parameters being sent this time.
185 SIVAL(req->outbuf, smb_ntr_DataOffset,
186 ((smb_buf(req->outbuf)+alignment_offset) -
187 smb_base(req->outbuf))
188 + params_sent_thistime + data_alignment_offset);
189 SIVAL(req->outbuf,smb_ntr_DataDisplacement, pd - pdata);
193 * Copy the param bytes into the packet.
196 if(params_sent_thistime) {
197 if (alignment_offset != 0) {
198 memset(smb_buf(req->outbuf), 0,
201 memcpy((smb_buf(req->outbuf)+alignment_offset), pp,
202 params_sent_thistime);
206 * Copy in the data bytes
209 if(data_sent_thistime) {
210 if (data_alignment_offset != 0) {
211 memset((smb_buf(req->outbuf)+alignment_offset+
212 params_sent_thistime), 0,
213 data_alignment_offset);
215 memcpy(smb_buf(req->outbuf)+alignment_offset
216 +params_sent_thistime+data_alignment_offset,
217 pd,data_sent_thistime);
220 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
221 params_sent_thistime, data_sent_thistime, useable_space));
222 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
223 params_to_send, data_to_send, paramsize, datasize));
225 if (NT_STATUS_V(nt_error)) {
226 error_packet_set((char *)req->outbuf,
231 /* Send the packet */
232 show_msg((char *)req->outbuf);
233 if (!srv_send_smb(smbd_server_fd(),
236 IS_CONN_ENCRYPTED(conn),
238 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
241 TALLOC_FREE(req->outbuf);
243 pp += params_sent_thistime;
244 pd += data_sent_thistime;
246 params_to_send -= params_sent_thistime;
247 data_to_send -= data_sent_thistime;
253 if(params_to_send < 0 || data_to_send < 0) {
254 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
255 params_to_send, data_to_send));
256 exit_server_cleanly("send_nt_replies: internal error");
261 /****************************************************************************
262 Is it an NTFS stream name ?
263 An NTFS file name is <path>.<extention>:<stream name>:<stream type>
264 $DATA can be used as both a stream name and a stream type. A missing stream
265 name or type implies $DATA.
267 Both Windows stream names and POSIX files can contain the ':' character.
268 This function first checks for the existence of a colon in the last component
269 of the given name. If the name contains a colon we differentiate between a
270 stream and POSIX file by checking if the latter exists through a POSIX stat.
272 Function assumes we've already chdir() to the "root" directory of fname.
273 ****************************************************************************/
275 bool is_ntfs_stream_name(const char *fname)
277 const char *lastcomp;
278 SMB_STRUCT_STAT sbuf;
280 /* If all pathnames are treated as POSIX we ignore streams. */
281 if (lp_posix_pathnames()) {
285 /* Find the last component of the name. */
286 if ((lastcomp = strrchr_m(fname, '/')) != NULL)
291 /* If there is no colon in the last component, it's not a stream. */
292 if (strchr_m(lastcomp, ':') == NULL)
296 * If file already exists on disk, it's not a stream. The stat must
297 * bypass the vfs layer so streams modules don't intefere.
299 if (sys_stat(fname, &sbuf) == 0) {
300 DEBUG(5, ("is_ntfs_stream_name: file %s contains a ':' but is "
301 "not a stream\n", fname));
308 /****************************************************************************
309 Simple check to determine if the filename is a stream.
310 ***************************************************************************/
311 bool is_ntfs_stream_smb_fname(const struct smb_filename *smb_fname)
313 if (lp_posix_pathnames()) {
317 return smb_fname->stream_name;
320 /****************************************************************************
321 Returns true if the filename's stream == "::$DATA"
322 ***************************************************************************/
323 bool is_ntfs_default_stream_smb_fname(const struct smb_filename *smb_fname)
325 if (lp_posix_pathnames()) {
329 if (!smb_fname->stream_name) {
333 return StrCaseCmp(smb_fname->stream_name, "::$DATA") == 0;
336 /****************************************************************************
337 Reply to an NT create and X call on a pipe
338 ****************************************************************************/
340 static void nt_open_pipe(char *fname, connection_struct *conn,
341 struct smb_request *req, int *ppnum)
346 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
348 /* Strip \\ off the name. */
351 status = open_np_file(req, fname, &fsp);
352 if (!NT_STATUS_IS_OK(status)) {
353 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
354 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
358 reply_nterror(req, status);
366 /****************************************************************************
367 Reply to an NT create and X call for pipes.
368 ****************************************************************************/
370 static void do_ntcreate_pipe_open(connection_struct *conn,
371 struct smb_request *req)
376 uint32 flags = IVAL(req->vwv+3, 1);
377 TALLOC_CTX *ctx = talloc_tos();
379 srvstr_pull_req_talloc(ctx, req, &fname, req->buf, STR_TERMINATE);
382 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
386 nt_open_pipe(fname, conn, req, &pnum);
394 * Deal with pipe return.
397 if (flags & EXTENDED_RESPONSE_REQUIRED) {
398 /* This is very strange. We
399 * return 50 words, but only set
400 * the wcnt to 42 ? It's definately
401 * what happens on the wire....
403 reply_outbuf(req, 50, 0);
404 SCVAL(req->outbuf,smb_wct,42);
406 reply_outbuf(req, 34, 0);
409 p = (char *)req->outbuf + smb_vwv2;
413 SIVAL(p,0,FILE_WAS_OPENED);
416 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
419 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
421 SSVAL(p,2, 0x5FF); /* ? */
424 if (flags & EXTENDED_RESPONSE_REQUIRED) {
426 SIVAL(p,0,FILE_GENERIC_ALL);
428 * For pipes W2K3 seems to return
430 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
432 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
435 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
440 /****************************************************************************
441 Reply to an NT create and X call.
442 ****************************************************************************/
444 void reply_ntcreate_and_X(struct smb_request *req)
446 connection_struct *conn = req->conn;
447 struct smb_filename *smb_fname = NULL;
451 uint32 file_attributes;
453 uint32 create_disposition;
454 uint32 create_options;
456 uint64_t allocation_size;
457 /* Breakout the oplock request bits so we can set the
458 reply bits separately. */
460 SMB_OFF_T file_len = 0;
462 files_struct *fsp = NULL;
464 struct timespec c_timespec;
465 struct timespec a_timespec;
466 struct timespec m_timespec;
469 uint8_t oplock_granted = NO_OPLOCK_RETURN;
470 TALLOC_CTX *ctx = talloc_tos();
472 START_PROFILE(SMBntcreateX);
475 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
479 flags = IVAL(req->vwv+3, 1);
480 access_mask = IVAL(req->vwv+7, 1);
481 file_attributes = IVAL(req->vwv+13, 1);
482 share_access = IVAL(req->vwv+15, 1);
483 create_disposition = IVAL(req->vwv+17, 1);
484 create_options = IVAL(req->vwv+19, 1);
485 root_dir_fid = (uint16)IVAL(req->vwv+5, 1);
487 allocation_size = (uint64_t)IVAL(req->vwv+9, 1);
488 #ifdef LARGE_SMB_OFF_T
489 allocation_size |= (((uint64_t)IVAL(req->vwv+11, 1)) << 32);
492 srvstr_get_path_req(ctx, req, &fname, (const char *)req->buf,
493 STR_TERMINATE, &status);
495 if (!NT_STATUS_IS_OK(status)) {
496 reply_nterror(req, status);
500 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
501 "file_attributes = 0x%x, share_access = 0x%x, "
502 "create_disposition = 0x%x create_options = 0x%x "
503 "root_dir_fid = 0x%x, fname = %s\n",
505 (unsigned int)access_mask,
506 (unsigned int)file_attributes,
507 (unsigned int)share_access,
508 (unsigned int)create_disposition,
509 (unsigned int)create_options,
510 (unsigned int)root_dir_fid,
514 * we need to remove ignored bits when they come directly from the client
515 * because we reuse some of them for internal stuff
517 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
520 * If it's an IPC, use the pipe handler.
524 if (lp_nt_pipe_support()) {
525 do_ntcreate_pipe_open(conn, req);
528 reply_doserror(req, ERRDOS, ERRnoaccess);
532 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
533 if (oplock_request) {
534 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
538 status = filename_convert(ctx,
540 req->flags2 & FLAGS2_DFS_PATHNAMES,
545 if (!NT_STATUS_IS_OK(status)) {
546 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
548 NT_STATUS_PATH_NOT_COVERED,
552 reply_nterror(req, status);
556 status = SMB_VFS_CREATE_FILE(
559 root_dir_fid, /* root_dir_fid */
560 smb_fname, /* fname */
561 access_mask, /* access_mask */
562 share_access, /* share_access */
563 create_disposition, /* create_disposition*/
564 create_options, /* create_options */
565 file_attributes, /* file_attributes */
566 oplock_request, /* oplock_request */
567 allocation_size, /* allocation_size */
573 if (!NT_STATUS_IS_OK(status)) {
574 if (open_was_deferred(req->mid)) {
575 /* We have re-scheduled this call, no error. */
578 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
579 reply_botherror(req, status, ERRDOS, ERRfilexists);
582 reply_nterror(req, status);
588 * If the caller set the extended oplock request bit
589 * and we granted one (by whatever means) - set the
590 * correct bit for extended oplock reply.
593 if (oplock_request &&
594 (lp_fake_oplocks(SNUM(conn))
595 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
598 * Exclusive oplock granted
601 if (flags & REQUEST_BATCH_OPLOCK) {
602 oplock_granted = BATCH_OPLOCK_RETURN;
604 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
606 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
607 oplock_granted = LEVEL_II_OPLOCK_RETURN;
609 oplock_granted = NO_OPLOCK_RETURN;
612 file_len = smb_fname->st.st_ex_size;
613 fattr = dos_mode(conn,fsp->fsp_name,&smb_fname->st);
615 fattr = FILE_ATTRIBUTE_NORMAL;
618 if (flags & EXTENDED_RESPONSE_REQUIRED) {
619 /* This is very strange. We
620 * return 50 words, but only set
621 * the wcnt to 42 ? It's definately
622 * what happens on the wire....
624 reply_outbuf(req, 50, 0);
625 SCVAL(req->outbuf,smb_wct,42);
627 reply_outbuf(req, 34, 0);
630 p = (char *)req->outbuf + smb_vwv2;
632 SCVAL(p, 0, oplock_granted);
635 SSVAL(p,0,fsp->fnum);
637 if ((create_disposition == FILE_SUPERSEDE)
638 && (info == FILE_WAS_OVERWRITTEN)) {
639 SIVAL(p,0,FILE_WAS_SUPERSEDED);
646 c_timespec = smb_fname->st.st_ex_btime;
647 a_timespec = smb_fname->st.st_ex_atime;
648 m_timespec = smb_fname->st.st_ex_mtime;
650 if (lp_dos_filetime_resolution(SNUM(conn))) {
651 dos_filetime_timespec(&c_timespec);
652 dos_filetime_timespec(&a_timespec);
653 dos_filetime_timespec(&m_timespec);
656 put_long_date_timespec(p, c_timespec); /* create time. */
658 put_long_date_timespec(p, a_timespec); /* access time */
660 put_long_date_timespec(p, m_timespec); /* write time */
662 put_long_date_timespec(p, m_timespec); /* change time */
664 SIVAL(p,0,fattr); /* File Attributes. */
666 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn,fsp,&smb_fname->st));
668 SOFF_T(p,0,file_len);
670 if (flags & EXTENDED_RESPONSE_REQUIRED) {
674 SCVAL(p,0,fsp->is_directory ? 1 : 0);
676 if (flags & EXTENDED_RESPONSE_REQUIRED) {
679 if (fsp->is_directory ||
680 can_write_to_file(conn, fsp->fsp_name, &smb_fname->st)) {
681 perms = FILE_GENERIC_ALL;
683 perms = FILE_GENERIC_READ|FILE_EXECUTE;
688 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n",
689 fsp->fnum, fsp->fsp_name));
693 TALLOC_FREE(smb_fname);
694 END_PROFILE(SMBntcreateX);
698 /****************************************************************************
699 Reply to a NT_TRANSACT_CREATE call to open a pipe.
700 ****************************************************************************/
702 static void do_nt_transact_create_pipe(connection_struct *conn,
703 struct smb_request *req,
704 uint16 **ppsetup, uint32 setup_count,
705 char **ppparams, uint32 parameter_count,
706 char **ppdata, uint32 data_count)
709 char *params = *ppparams;
715 TALLOC_CTX *ctx = talloc_tos();
718 * Ensure minimum number of parameters sent.
721 if(parameter_count < 54) {
722 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
723 reply_doserror(req, ERRDOS, ERRnoaccess);
727 flags = IVAL(params,0);
729 srvstr_get_path(ctx, params, req->flags2, &fname, params+53,
730 parameter_count-53, STR_TERMINATE,
732 if (!NT_STATUS_IS_OK(status)) {
733 reply_nterror(req, status);
737 nt_open_pipe(fname, conn, req, &pnum);
744 /* Realloc the size of parameters and data we will return */
745 if (flags & EXTENDED_RESPONSE_REQUIRED) {
746 /* Extended response is 32 more byyes. */
751 params = nttrans_realloc(ppparams, param_len);
753 reply_doserror(req, ERRDOS, ERRnomem);
758 SCVAL(p,0,NO_OPLOCK_RETURN);
763 SIVAL(p,0,FILE_WAS_OPENED);
767 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
770 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
772 SSVAL(p,2, 0x5FF); /* ? */
775 if (flags & EXTENDED_RESPONSE_REQUIRED) {
777 SIVAL(p,0,FILE_GENERIC_ALL);
779 * For pipes W2K3 seems to return
781 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
783 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
786 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
788 /* Send the required number of replies */
789 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
794 /****************************************************************************
795 Internal fn to set security descriptors.
796 ****************************************************************************/
798 static NTSTATUS set_sd(files_struct *fsp, uint8 *data, uint32 sd_len,
799 uint32 security_info_sent)
801 SEC_DESC *psd = NULL;
804 if (sd_len == 0 || !lp_nt_acl_support(SNUM(fsp->conn))) {
808 status = unmarshall_sec_desc(talloc_tos(), data, sd_len, &psd);
810 if (!NT_STATUS_IS_OK(status)) {
814 if (psd->owner_sid == NULL) {
815 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
817 if (psd->group_sid == NULL) {
818 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
821 /* Convert all the generic bits. */
822 security_acl_map_generic(psd->dacl, &file_generic_mapping);
823 security_acl_map_generic(psd->sacl, &file_generic_mapping);
825 if (DEBUGLEVEL >= 10) {
826 DEBUG(10,("set_sd for file %s\n", fsp->fsp_name ));
827 NDR_PRINT_DEBUG(security_descriptor, psd);
830 status = SMB_VFS_FSET_NT_ACL(fsp, security_info_sent, psd);
837 /****************************************************************************
838 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
839 ****************************************************************************/
841 static struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
843 struct ea_list *ea_list_head = NULL;
850 while (offset + 4 <= data_size) {
851 size_t next_offset = IVAL(pdata,offset);
852 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
858 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
859 if (next_offset == 0) {
862 offset += next_offset;
868 /****************************************************************************
869 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
870 ****************************************************************************/
872 static void call_nt_transact_create(connection_struct *conn,
873 struct smb_request *req,
874 uint16 **ppsetup, uint32 setup_count,
875 char **ppparams, uint32 parameter_count,
876 char **ppdata, uint32 data_count,
877 uint32 max_data_count)
879 struct smb_filename *smb_fname = NULL;
881 char *params = *ppparams;
882 char *data = *ppdata;
883 /* Breakout the oplock request bits so we can set the reply bits separately. */
885 SMB_OFF_T file_len = 0;
887 files_struct *fsp = NULL;
891 uint32 file_attributes;
893 uint32 create_disposition;
894 uint32 create_options;
896 struct security_descriptor *sd = NULL;
899 struct timespec c_timespec;
900 struct timespec a_timespec;
901 struct timespec m_timespec;
902 struct ea_list *ea_list = NULL;
905 uint64_t allocation_size;
907 uint8_t oplock_granted;
908 TALLOC_CTX *ctx = talloc_tos();
910 DEBUG(5,("call_nt_transact_create\n"));
913 * If it's an IPC, use the pipe handler.
917 if (lp_nt_pipe_support()) {
918 do_nt_transact_create_pipe(
920 ppsetup, setup_count,
921 ppparams, parameter_count,
925 reply_doserror(req, ERRDOS, ERRnoaccess);
930 * Ensure minimum number of parameters sent.
933 if(parameter_count < 54) {
934 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
935 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
939 flags = IVAL(params,0);
940 access_mask = IVAL(params,8);
941 file_attributes = IVAL(params,20);
942 share_access = IVAL(params,24);
943 create_disposition = IVAL(params,28);
944 create_options = IVAL(params,32);
945 sd_len = IVAL(params,36);
946 ea_len = IVAL(params,40);
947 root_dir_fid = (uint16)IVAL(params,4);
948 allocation_size = (uint64_t)IVAL(params,12);
949 #ifdef LARGE_SMB_OFF_T
950 allocation_size |= (((uint64_t)IVAL(params,16)) << 32);
954 * we need to remove ignored bits when they come directly from the client
955 * because we reuse some of them for internal stuff
957 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
959 /* Ensure the data_len is correct for the sd and ea values given. */
960 if ((ea_len + sd_len > data_count)
961 || (ea_len > data_count) || (sd_len > data_count)
962 || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
963 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
964 "%u, data_count = %u\n", (unsigned int)ea_len,
965 (unsigned int)sd_len, (unsigned int)data_count));
966 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
971 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
974 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
976 if (!NT_STATUS_IS_OK(status)) {
977 DEBUG(10, ("call_nt_transact_create: "
978 "unmarshall_sec_desc failed: %s\n",
980 reply_nterror(req, status);
986 if (!lp_ea_support(SNUM(conn))) {
987 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
988 "EA's not supported.\n",
989 (unsigned int)ea_len));
990 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
995 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
996 "too small (should be more than 10)\n",
997 (unsigned int)ea_len ));
998 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1002 /* We have already checked that ea_len <= data_count here. */
1003 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
1005 if (ea_list == NULL) {
1006 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1011 srvstr_get_path(ctx, params, req->flags2, &fname,
1012 params+53, parameter_count-53,
1013 STR_TERMINATE, &status);
1014 if (!NT_STATUS_IS_OK(status)) {
1015 reply_nterror(req, status);
1019 status = filename_convert(ctx,
1021 req->flags2 & FLAGS2_DFS_PATHNAMES,
1026 if (!NT_STATUS_IS_OK(status)) {
1027 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1028 reply_botherror(req,
1029 NT_STATUS_PATH_NOT_COVERED,
1030 ERRSRV, ERRbadpath);
1033 reply_nterror(req, status);
1037 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1038 if (oplock_request) {
1039 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
1043 status = SMB_VFS_CREATE_FILE(
1046 root_dir_fid, /* root_dir_fid */
1047 smb_fname, /* fname */
1048 access_mask, /* access_mask */
1049 share_access, /* share_access */
1050 create_disposition, /* create_disposition*/
1051 create_options, /* create_options */
1052 file_attributes, /* file_attributes */
1053 oplock_request, /* oplock_request */
1054 allocation_size, /* allocation_size */
1056 ea_list, /* ea_list */
1060 if(!NT_STATUS_IS_OK(status)) {
1061 if (open_was_deferred(req->mid)) {
1062 /* We have re-scheduled this call, no error. */
1065 reply_openerror(req, status);
1070 * If the caller set the extended oplock request bit
1071 * and we granted one (by whatever means) - set the
1072 * correct bit for extended oplock reply.
1075 if (oplock_request &&
1076 (lp_fake_oplocks(SNUM(conn))
1077 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
1080 * Exclusive oplock granted
1083 if (flags & REQUEST_BATCH_OPLOCK) {
1084 oplock_granted = BATCH_OPLOCK_RETURN;
1086 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
1088 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1089 oplock_granted = LEVEL_II_OPLOCK_RETURN;
1091 oplock_granted = NO_OPLOCK_RETURN;
1094 file_len = smb_fname->st.st_ex_size;
1095 fattr = dos_mode(conn, fsp->fsp_name, &smb_fname->st);
1097 fattr = FILE_ATTRIBUTE_NORMAL;
1100 /* Realloc the size of parameters and data we will return */
1101 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1102 /* Extended response is 32 more byyes. */
1107 params = nttrans_realloc(ppparams, param_len);
1108 if(params == NULL) {
1109 reply_doserror(req, ERRDOS, ERRnomem);
1114 SCVAL(p, 0, oplock_granted);
1117 SSVAL(p,0,fsp->fnum);
1119 if ((create_disposition == FILE_SUPERSEDE)
1120 && (info == FILE_WAS_OVERWRITTEN)) {
1121 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1128 c_timespec = smb_fname->st.st_ex_btime;
1129 a_timespec = smb_fname->st.st_ex_atime;
1130 m_timespec = smb_fname->st.st_ex_mtime;
1132 if (lp_dos_filetime_resolution(SNUM(conn))) {
1133 dos_filetime_timespec(&c_timespec);
1134 dos_filetime_timespec(&a_timespec);
1135 dos_filetime_timespec(&m_timespec);
1138 put_long_date_timespec(p, c_timespec); /* create time. */
1140 put_long_date_timespec(p, a_timespec); /* access time */
1142 put_long_date_timespec(p, m_timespec); /* write time */
1144 put_long_date_timespec(p, m_timespec); /* change time */
1146 SIVAL(p,0,fattr); /* File Attributes. */
1148 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn, fsp, &smb_fname->st));
1150 SOFF_T(p,0,file_len);
1152 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1156 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1158 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1161 if (fsp->is_directory ||
1162 can_write_to_file(conn, fsp->fsp_name, &smb_fname->st)) {
1163 perms = FILE_GENERIC_ALL;
1165 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1170 DEBUG(5,("call_nt_transact_create: open name = %s\n", fsp->fsp_name));
1172 /* Send the required number of replies */
1173 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1175 TALLOC_FREE(smb_fname);
1179 /****************************************************************************
1180 Reply to a NT CANCEL request.
1181 conn POINTER CAN BE NULL HERE !
1182 ****************************************************************************/
1184 void reply_ntcancel(struct smb_request *req)
1187 * Go through and cancel any pending change notifies.
1190 START_PROFILE(SMBntcancel);
1191 srv_cancel_sign_response(smbd_server_conn);
1192 remove_pending_change_notify_requests_by_mid(req->mid);
1193 remove_pending_lock_requests_by_mid(req->mid);
1195 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", req->mid));
1197 END_PROFILE(SMBntcancel);
1201 /****************************************************************************
1203 ****************************************************************************/
1205 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
1206 connection_struct *conn,
1207 struct smb_request *req,
1208 struct smb_filename *smb_fname_src,
1209 struct smb_filename *smb_fname_dst,
1212 char *oldname = NULL;
1213 char *newname = NULL;
1214 files_struct *fsp1,*fsp2;
1218 NTSTATUS status = NT_STATUS_OK;
1221 if (!CAN_WRITE(conn)) {
1222 status = NT_STATUS_MEDIA_WRITE_PROTECTED;
1226 /* Source must already exist. */
1227 if (!VALID_STAT(smb_fname_src->st)) {
1228 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
1232 status = get_full_smb_filename(ctx, smb_fname_src, &oldname);
1233 if (!NT_STATUS_IS_OK(status)) {
1237 /* Ensure attributes match. */
1238 fattr = dos_mode(conn, oldname, &smb_fname_src->st);
1239 if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
1240 status = NT_STATUS_NO_SUCH_FILE;
1244 /* Disallow if dst file already exists. */
1245 if (VALID_STAT(smb_fname_dst->st)) {
1246 status = NT_STATUS_OBJECT_NAME_COLLISION;
1250 /* No links from a directory. */
1251 if (S_ISDIR(smb_fname_src->st.st_ex_mode)) {
1252 status = NT_STATUS_FILE_IS_A_DIRECTORY;
1256 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1257 smb_fname_str_dbg(smb_fname_src),
1258 smb_fname_str_dbg(smb_fname_dst)));
1260 status = SMB_VFS_CREATE_FILE(
1263 0, /* root_dir_fid */
1264 smb_fname_src, /* fname */
1265 FILE_READ_DATA, /* access_mask */
1266 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1268 FILE_OPEN, /* create_disposition*/
1269 0, /* create_options */
1270 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
1271 NO_OPLOCK, /* oplock_request */
1272 0, /* allocation_size */
1278 if (!NT_STATUS_IS_OK(status)) {
1282 status = SMB_VFS_CREATE_FILE(
1285 0, /* root_dir_fid */
1286 smb_fname_dst, /* fname */
1287 FILE_WRITE_DATA, /* access_mask */
1288 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1290 FILE_CREATE, /* create_disposition*/
1291 0, /* create_options */
1292 fattr, /* file_attributes */
1293 NO_OPLOCK, /* oplock_request */
1294 0, /* allocation_size */
1300 if (!NT_STATUS_IS_OK(status)) {
1301 close_file(NULL, fsp1, ERROR_CLOSE);
1305 if (smb_fname_src->st.st_ex_size) {
1306 ret = vfs_transfer_file(fsp1, fsp2, smb_fname_src->st.st_ex_size);
1310 * As we are opening fsp1 read-only we only expect
1311 * an error on close on fsp2 if we are out of space.
1312 * Thus we don't look at the error return from the
1315 close_file(NULL, fsp1, NORMAL_CLOSE);
1317 /* Ensure the modtime is set correctly on the destination file. */
1318 set_close_write_time(fsp2, smb_fname_src->st.st_ex_mtime);
1320 status = close_file(NULL, fsp2, NORMAL_CLOSE);
1322 status = get_full_smb_filename(ctx, smb_fname_dst, &newname);
1323 if (!NT_STATUS_IS_OK(status)) {
1327 /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
1328 creates the file. This isn't the correct thing to do in the copy
1330 if (!parent_dirname(talloc_tos(), newname, &parent, NULL)) {
1331 status = NT_STATUS_NO_MEMORY;
1334 file_set_dosmode(conn, newname, fattr, &smb_fname_dst->st, parent,
1336 TALLOC_FREE(parent);
1338 if (ret < (SMB_OFF_T)smb_fname_src->st.st_ex_size) {
1339 status = NT_STATUS_DISK_FULL;
1343 if (!NT_STATUS_IS_OK(status)) {
1344 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1345 nt_errstr(status), smb_fname_str_dbg(smb_fname_src),
1346 smb_fname_str_dbg(smb_fname_dst)));
1349 TALLOC_FREE(oldname);
1350 TALLOC_FREE(newname);
1355 /****************************************************************************
1356 Reply to a NT rename request.
1357 ****************************************************************************/
1359 void reply_ntrename(struct smb_request *req)
1361 connection_struct *conn = req->conn;
1362 struct smb_filename *smb_fname_old = NULL;
1363 struct smb_filename *smb_fname_new = NULL;
1364 char *oldname = NULL;
1365 char *newname = NULL;
1368 bool src_has_wcard = False;
1369 bool dest_has_wcard = False;
1372 TALLOC_CTX *ctx = talloc_tos();
1374 START_PROFILE(SMBntrename);
1377 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1378 END_PROFILE(SMBntrename);
1382 attrs = SVAL(req->vwv+0, 0);
1383 rename_type = SVAL(req->vwv+1, 0);
1385 p = (const char *)req->buf + 1;
1386 p += srvstr_get_path_req_wcard(ctx, req, &oldname, p, STR_TERMINATE,
1387 &status, &src_has_wcard);
1388 if (!NT_STATUS_IS_OK(status)) {
1389 reply_nterror(req, status);
1390 END_PROFILE(SMBntrename);
1394 if (ms_has_wild(oldname)) {
1395 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1396 END_PROFILE(SMBntrename);
1401 p += srvstr_get_path_req_wcard(ctx, req, &newname, p, STR_TERMINATE,
1402 &status, &dest_has_wcard);
1403 if (!NT_STATUS_IS_OK(status)) {
1404 reply_nterror(req, status);
1405 END_PROFILE(SMBntrename);
1409 status = filename_convert(ctx, conn,
1410 req->flags2 & FLAGS2_DFS_PATHNAMES,
1414 if (!NT_STATUS_IS_OK(status)) {
1415 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1416 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1417 ERRSRV, ERRbadpath);
1418 END_PROFILE(SMBntrename);
1421 reply_nterror(req, status);
1422 END_PROFILE(SMBntrename);
1426 status = filename_convert(ctx, conn,
1427 req->flags2 & FLAGS2_DFS_PATHNAMES,
1431 if (!NT_STATUS_IS_OK(status)) {
1432 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1433 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1434 ERRSRV, ERRbadpath);
1435 END_PROFILE(SMBntrename);
1438 reply_nterror(req, status);
1439 END_PROFILE(SMBntrename);
1443 /* The new name must begin with a ':' if the old name is a stream. */
1444 if (is_ntfs_stream_name(oldname) && (newname[0] != ':')) {
1445 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1446 END_PROFILE(SMBntrename);
1450 DEBUG(3,("reply_ntrename : %s -> %s\n",oldname,newname));
1452 switch(rename_type) {
1453 case RENAME_FLAG_RENAME:
1454 status = rename_internals(ctx, conn, req, oldname,
1455 newname, attrs, False, src_has_wcard,
1456 dest_has_wcard, DELETE_ACCESS);
1458 case RENAME_FLAG_HARD_LINK:
1459 if (src_has_wcard || dest_has_wcard) {
1461 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1463 status = hardlink_internals(ctx,
1469 case RENAME_FLAG_COPY:
1470 if (src_has_wcard || dest_has_wcard) {
1472 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1474 status = copy_internals(ctx, conn, req,
1480 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1481 status = NT_STATUS_INVALID_PARAMETER;
1484 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1488 if (!NT_STATUS_IS_OK(status)) {
1489 if (open_was_deferred(req->mid)) {
1490 /* We have re-scheduled this call. */
1491 END_PROFILE(SMBntrename);
1495 reply_nterror(req, status);
1496 END_PROFILE(SMBntrename);
1500 reply_outbuf(req, 0, 0);
1502 END_PROFILE(SMBntrename);
1506 /****************************************************************************
1507 Reply to a notify change - queue the request and
1508 don't allow a directory to be opened.
1509 ****************************************************************************/
1511 static void call_nt_transact_notify_change(connection_struct *conn,
1512 struct smb_request *req,
1516 uint32 parameter_count,
1517 char **ppdata, uint32 data_count,
1518 uint32 max_data_count,
1519 uint32 max_param_count)
1521 uint16 *setup = *ppsetup;
1527 if(setup_count < 6) {
1528 reply_doserror(req, ERRDOS, ERRbadfunc);
1532 fsp = file_fsp(req, SVAL(setup,4));
1533 filter = IVAL(setup, 0);
1534 recursive = (SVAL(setup, 6) != 0) ? True : False;
1536 DEBUG(3,("call_nt_transact_notify_change\n"));
1539 reply_doserror(req, ERRDOS, ERRbadfid);
1544 char *filter_string;
1546 if (!(filter_string = notify_filter_string(NULL, filter))) {
1547 reply_nterror(req,NT_STATUS_NO_MEMORY);
1551 DEBUG(3,("call_nt_transact_notify_change: notify change "
1552 "called on %s, filter = %s, recursive = %d\n",
1553 fsp->fsp_name, filter_string, recursive));
1555 TALLOC_FREE(filter_string);
1558 if((!fsp->is_directory) || (conn != fsp->conn)) {
1559 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1563 if (fsp->notify == NULL) {
1565 status = change_notify_create(fsp, filter, recursive);
1567 if (!NT_STATUS_IS_OK(status)) {
1568 DEBUG(10, ("change_notify_create returned %s\n",
1569 nt_errstr(status)));
1570 reply_nterror(req, status);
1575 if (fsp->notify->num_changes != 0) {
1578 * We've got changes pending, respond immediately
1582 * TODO: write a torture test to check the filtering behaviour
1586 change_notify_reply(fsp->conn, req, max_param_count,
1590 * change_notify_reply() above has independently sent its
1597 * No changes pending, queue the request
1600 status = change_notify_add_request(req,
1604 if (!NT_STATUS_IS_OK(status)) {
1605 reply_nterror(req, status);
1610 /****************************************************************************
1611 Reply to an NT transact rename command.
1612 ****************************************************************************/
1614 static void call_nt_transact_rename(connection_struct *conn,
1615 struct smb_request *req,
1616 uint16 **ppsetup, uint32 setup_count,
1617 char **ppparams, uint32 parameter_count,
1618 char **ppdata, uint32 data_count,
1619 uint32 max_data_count)
1621 char *params = *ppparams;
1622 char *new_name = NULL;
1623 files_struct *fsp = NULL;
1624 bool dest_has_wcard = False;
1626 TALLOC_CTX *ctx = talloc_tos();
1628 if(parameter_count < 5) {
1629 reply_doserror(req, ERRDOS, ERRbadfunc);
1633 fsp = file_fsp(req, SVAL(params, 0));
1634 if (!check_fsp(conn, req, fsp)) {
1637 srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
1638 parameter_count - 4,
1639 STR_TERMINATE, &status, &dest_has_wcard);
1640 if (!NT_STATUS_IS_OK(status)) {
1641 reply_nterror(req, status);
1646 * W2K3 ignores this request as the RAW-RENAME test
1647 * demonstrates, so we do.
1649 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1651 DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1652 fsp->fsp_name, new_name));
1657 /******************************************************************************
1658 Fake up a completely empty SD.
1659 *******************************************************************************/
1661 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
1665 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1667 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1668 return NT_STATUS_NO_MEMORY;
1671 return NT_STATUS_OK;
1674 /****************************************************************************
1675 Reply to query a security descriptor.
1676 ****************************************************************************/
1678 static void call_nt_transact_query_security_desc(connection_struct *conn,
1679 struct smb_request *req,
1683 uint32 parameter_count,
1686 uint32 max_data_count)
1688 char *params = *ppparams;
1689 char *data = *ppdata;
1690 SEC_DESC *psd = NULL;
1692 uint32 security_info_wanted;
1693 files_struct *fsp = NULL;
1697 if(parameter_count < 8) {
1698 reply_doserror(req, ERRDOS, ERRbadfunc);
1702 fsp = file_fsp(req, SVAL(params,0));
1704 reply_doserror(req, ERRDOS, ERRbadfid);
1708 security_info_wanted = IVAL(params,4);
1710 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, info_wanted = 0x%x\n", fsp->fsp_name,
1711 (unsigned int)security_info_wanted ));
1713 params = nttrans_realloc(ppparams, 4);
1714 if(params == NULL) {
1715 reply_doserror(req, ERRDOS, ERRnomem);
1720 * Get the permissions to return.
1723 if (!lp_nt_acl_support(SNUM(conn))) {
1724 status = get_null_nt_acl(talloc_tos(), &psd);
1726 status = SMB_VFS_FGET_NT_ACL(
1727 fsp, security_info_wanted, &psd);
1729 if (!NT_STATUS_IS_OK(status)) {
1730 reply_nterror(req, status);
1734 /* If the SACL/DACL is NULL, but was requested, we mark that it is
1735 * present in the reply to match Windows behavior */
1736 if (psd->sacl == NULL &&
1737 security_info_wanted & SACL_SECURITY_INFORMATION)
1738 psd->type |= SEC_DESC_SACL_PRESENT;
1739 if (psd->dacl == NULL &&
1740 security_info_wanted & DACL_SECURITY_INFORMATION)
1741 psd->type |= SEC_DESC_DACL_PRESENT;
1743 sd_size = ndr_size_security_descriptor(psd, NULL, 0);
1745 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
1747 if (DEBUGLEVEL >= 10) {
1748 DEBUG(10,("call_nt_transact_query_security_desc for file %s\n", fsp->fsp_name));
1749 NDR_PRINT_DEBUG(security_descriptor, psd);
1752 SIVAL(params,0,(uint32)sd_size);
1754 if (max_data_count < sd_size) {
1755 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
1756 params, 4, *ppdata, 0);
1761 * Allocate the data we will point this at.
1764 data = nttrans_realloc(ppdata, sd_size);
1766 reply_doserror(req, ERRDOS, ERRnomem);
1770 status = marshall_sec_desc(talloc_tos(), psd,
1771 &blob.data, &blob.length);
1773 if (!NT_STATUS_IS_OK(status)) {
1774 reply_nterror(req, status);
1778 SMB_ASSERT(sd_size == blob.length);
1779 memcpy(data, blob.data, sd_size);
1781 send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
1786 /****************************************************************************
1787 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
1788 ****************************************************************************/
1790 static void call_nt_transact_set_security_desc(connection_struct *conn,
1791 struct smb_request *req,
1795 uint32 parameter_count,
1798 uint32 max_data_count)
1800 char *params= *ppparams;
1801 char *data = *ppdata;
1802 files_struct *fsp = NULL;
1803 uint32 security_info_sent = 0;
1806 if(parameter_count < 8) {
1807 reply_doserror(req, ERRDOS, ERRbadfunc);
1811 if((fsp = file_fsp(req, SVAL(params,0))) == NULL) {
1812 reply_doserror(req, ERRDOS, ERRbadfid);
1816 if(!lp_nt_acl_support(SNUM(conn))) {
1820 security_info_sent = IVAL(params,4);
1822 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
1823 (unsigned int)security_info_sent ));
1825 if (data_count == 0) {
1826 reply_doserror(req, ERRDOS, ERRnoaccess);
1830 status = set_sd(fsp, (uint8 *)data, data_count, security_info_sent);
1832 if (!NT_STATUS_IS_OK(status)) {
1833 reply_nterror(req, status);
1838 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1842 /****************************************************************************
1844 ****************************************************************************/
1846 static void call_nt_transact_ioctl(connection_struct *conn,
1847 struct smb_request *req,
1848 uint16 **ppsetup, uint32 setup_count,
1849 char **ppparams, uint32 parameter_count,
1850 char **ppdata, uint32 data_count,
1851 uint32 max_data_count)
1858 char *pdata = *ppdata;
1860 if (setup_count != 8) {
1861 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
1862 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1866 function = IVAL(*ppsetup, 0);
1867 fidnum = SVAL(*ppsetup, 4);
1868 isFSctl = CVAL(*ppsetup, 6);
1869 compfilter = CVAL(*ppsetup, 7);
1871 DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
1872 function, fidnum, isFSctl, compfilter));
1874 fsp=file_fsp(req, fidnum);
1875 /* this check is done in each implemented function case for now
1876 because I don't want to break anything... --metze
1877 FSP_BELONGS_CONN(fsp,conn);*/
1879 SMB_PERFCOUNT_SET_IOCTL(&req->pcd, function);
1882 case FSCTL_SET_SPARSE:
1883 /* pretend this succeeded - tho strictly we should
1884 mark the file sparse (if the local fs supports it)
1885 so we can know if we need to pre-allocate or not */
1887 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
1888 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1891 case FSCTL_CREATE_OR_GET_OBJECT_ID:
1893 unsigned char objid[16];
1895 /* This should return the object-id on this file.
1896 * I think I'll make this be the inode+dev. JRA.
1899 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
1901 if (!fsp_belongs_conn(conn, req, fsp)) {
1906 pdata = nttrans_realloc(ppdata, data_count);
1907 if (pdata == NULL) {
1908 reply_nterror(req, NT_STATUS_NO_MEMORY);
1912 /* For backwards compatibility only store the dev/inode. */
1913 push_file_id_16(pdata, &fsp->file_id);
1914 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
1915 push_file_id_16(pdata+32, &fsp->file_id);
1916 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
1921 case FSCTL_GET_REPARSE_POINT:
1922 /* pretend this fail - my winXP does it like this
1926 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1927 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
1930 case FSCTL_SET_REPARSE_POINT:
1931 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
1935 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1936 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
1939 case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
1942 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
1943 * and return their volume names. If max_data_count is 16, then it is just
1944 * asking for the number of volumes and length of the combined names.
1946 * pdata is the data allocated by our caller, but that uses
1947 * total_data_count (which is 0 in our case) rather than max_data_count.
1948 * Allocate the correct amount and return the pointer to let
1949 * it be deallocated when we return.
1951 SHADOW_COPY_DATA *shadow_data = NULL;
1952 TALLOC_CTX *shadow_mem_ctx = NULL;
1953 bool labels = False;
1954 uint32 labels_data_count = 0;
1958 if (!fsp_belongs_conn(conn, req, fsp)) {
1962 if (max_data_count < 16) {
1963 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
1965 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1969 if (max_data_count > 16) {
1973 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
1974 if (shadow_mem_ctx == NULL) {
1975 DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
1976 reply_nterror(req, NT_STATUS_NO_MEMORY);
1980 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
1981 if (shadow_data == NULL) {
1982 DEBUG(0,("TALLOC_ZERO() failed!\n"));
1983 talloc_destroy(shadow_mem_ctx);
1984 reply_nterror(req, NT_STATUS_NO_MEMORY);
1988 shadow_data->mem_ctx = shadow_mem_ctx;
1991 * Call the VFS routine to actually do the work.
1993 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
1994 talloc_destroy(shadow_data->mem_ctx);
1995 if (errno == ENOSYS) {
1996 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
1997 conn->connectpath));
1998 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2001 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
2002 conn->connectpath));
2003 reply_nterror(req, NT_STATUS_UNSUCCESSFUL);
2008 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
2013 data_count = 12+labels_data_count+4;
2016 if (max_data_count<data_count) {
2017 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
2018 max_data_count,data_count));
2019 talloc_destroy(shadow_data->mem_ctx);
2020 reply_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
2024 pdata = nttrans_realloc(ppdata, data_count);
2025 if (pdata == NULL) {
2026 talloc_destroy(shadow_data->mem_ctx);
2027 reply_nterror(req, NT_STATUS_NO_MEMORY);
2033 /* num_volumes 4 bytes */
2034 SIVAL(pdata,0,shadow_data->num_volumes);
2037 /* num_labels 4 bytes */
2038 SIVAL(pdata,4,shadow_data->num_volumes);
2041 /* needed_data_count 4 bytes */
2042 SIVAL(pdata,8,labels_data_count);
2046 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
2047 shadow_data->num_volumes,fsp->fsp_name));
2048 if (labels && shadow_data->labels) {
2049 for (i=0;i<shadow_data->num_volumes;i++) {
2050 srvstr_push(pdata, req->flags2,
2051 cur_pdata, shadow_data->labels[i],
2052 2*sizeof(SHADOW_COPY_LABEL),
2053 STR_UNICODE|STR_TERMINATE);
2054 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
2055 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
2059 talloc_destroy(shadow_data->mem_ctx);
2061 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
2067 case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
2069 /* pretend this succeeded -
2071 * we have to send back a list with all files owned by this SID
2073 * but I have to check that --metze
2077 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
2079 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
2081 if (!fsp_belongs_conn(conn, req, fsp)) {
2085 /* unknown 4 bytes: this is not the length of the sid :-( */
2086 /*unknown = IVAL(pdata,0);*/
2088 sid_parse(pdata+4,sid_len,&sid);
2089 DEBUGADD(10, ("for SID: %s\n", sid_string_dbg(&sid)));
2091 if (!sid_to_uid(&sid, &uid)) {
2092 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2093 sid_string_dbg(&sid),
2094 (unsigned long)sid_len));
2098 /* we can take a look at the find source :-)
2100 * find ./ -uid $uid -name '*' is what we need here
2103 * and send 4bytes len and then NULL terminated unicode strings
2106 * but I don't know how to deal with the paged results
2107 * (maybe we can hang the result anywhere in the fsp struct)
2109 * we don't send all files at once
2110 * and at the next we should *not* start from the beginning,
2111 * so we have to cache the result
2116 /* this works for now... */
2117 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2121 if (!logged_ioctl_message) {
2122 logged_ioctl_message = true; /* Only print this once... */
2123 DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
2128 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2132 #ifdef HAVE_SYS_QUOTAS
2133 /****************************************************************************
2134 Reply to get user quota
2135 ****************************************************************************/
2137 static void call_nt_transact_get_user_quota(connection_struct *conn,
2138 struct smb_request *req,
2142 uint32 parameter_count,
2145 uint32 max_data_count)
2147 NTSTATUS nt_status = NT_STATUS_OK;
2148 char *params = *ppparams;
2149 char *pdata = *ppdata;
2151 int data_len=0,param_len=0;
2154 files_struct *fsp = NULL;
2158 bool start_enum = True;
2159 SMB_NTQUOTA_STRUCT qt;
2160 SMB_NTQUOTA_LIST *tmp_list;
2161 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2166 if (conn->server_info->utok.uid != 0) {
2167 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2168 "[%s]\n", lp_servicename(SNUM(conn)),
2169 conn->server_info->unix_name));
2170 reply_doserror(req, ERRDOS, ERRnoaccess);
2175 * Ensure minimum number of parameters sent.
2178 if (parameter_count < 4) {
2179 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2180 reply_doserror(req, ERRDOS, ERRinvalidparam);
2184 /* maybe we can check the quota_fnum */
2185 fsp = file_fsp(req, SVAL(params,0));
2186 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2187 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2188 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2192 /* the NULL pointer checking for fsp->fake_file_handle->pd
2193 * is done by CHECK_NTQUOTA_HANDLE_OK()
2195 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->private_data;
2197 level = SVAL(params,2);
2199 /* unknown 12 bytes leading in params */
2202 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2203 /* seems that we should continue with the enum here --metze */
2205 if (qt_handle->quota_list!=NULL &&
2206 qt_handle->tmp_list==NULL) {
2209 free_ntquota_list(&(qt_handle->quota_list));
2211 /* Realloc the size of parameters and data we will return */
2213 params = nttrans_realloc(ppparams, param_len);
2214 if(params == NULL) {
2215 reply_doserror(req, ERRDOS, ERRnomem);
2220 SIVAL(params,0,data_len);
2227 case TRANSACT_GET_USER_QUOTA_LIST_START:
2229 if (qt_handle->quota_list==NULL &&
2230 qt_handle->tmp_list==NULL) {
2234 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2235 reply_doserror(req, ERRSRV, ERRerror);
2239 /* Realloc the size of parameters and data we will return */
2241 params = nttrans_realloc(ppparams, param_len);
2242 if(params == NULL) {
2243 reply_doserror(req, ERRDOS, ERRnomem);
2247 /* we should not trust the value in max_data_count*/
2248 max_data_count = MIN(max_data_count,2048);
2250 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2252 reply_doserror(req, ERRDOS, ERRnomem);
2258 /* set params Size of returned Quota Data 4 bytes*/
2259 /* but set it later when we know it */
2261 /* for each entry push the data */
2264 qt_handle->tmp_list = qt_handle->quota_list;
2267 tmp_list = qt_handle->tmp_list;
2269 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2270 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2272 sid_len = ndr_size_dom_sid(
2273 &tmp_list->quotas->sid, NULL, 0);
2274 entry_len = 40 + sid_len;
2276 /* nextoffset entry 4 bytes */
2277 SIVAL(entry,0,entry_len);
2279 /* then the len of the SID 4 bytes */
2280 SIVAL(entry,4,sid_len);
2282 /* unknown data 8 bytes uint64_t */
2283 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-metze*/
2285 /* the used disk space 8 bytes uint64_t */
2286 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2288 /* the soft quotas 8 bytes uint64_t */
2289 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2291 /* the hard quotas 8 bytes uint64_t */
2292 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2294 /* and now the SID */
2295 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2298 qt_handle->tmp_list = tmp_list;
2300 /* overwrite the offset of the last entry */
2301 SIVAL(entry-entry_len,0,0);
2303 data_len = 4+qt_len;
2304 /* overwrite the params quota_data_len */
2305 SIVAL(params,0,data_len);
2309 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2311 /* unknown 4 bytes IVAL(pdata,0) */
2313 if (data_count < 8) {
2314 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2315 reply_doserror(req, ERRDOS, ERRunknownlevel);
2319 sid_len = IVAL(pdata,4);
2320 /* Ensure this is less than 1mb. */
2321 if (sid_len > (1024*1024)) {
2322 reply_doserror(req, ERRDOS, ERRnomem);
2326 if (data_count < 8+sid_len) {
2327 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2328 reply_doserror(req, ERRDOS, ERRunknownlevel);
2332 data_len = 4+40+sid_len;
2334 if (max_data_count < data_len) {
2335 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2336 max_data_count, data_len));
2338 SIVAL(params,0,data_len);
2340 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2344 sid_parse(pdata+8,sid_len,&sid);
2346 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2349 * we have to return zero's in all fields
2350 * instead of returning an error here
2355 /* Realloc the size of parameters and data we will return */
2357 params = nttrans_realloc(ppparams, param_len);
2358 if(params == NULL) {
2359 reply_doserror(req, ERRDOS, ERRnomem);
2363 pdata = nttrans_realloc(ppdata, data_len);
2365 reply_doserror(req, ERRDOS, ERRnomem);
2371 /* set params Size of returned Quota Data 4 bytes*/
2372 SIVAL(params,0,data_len);
2374 /* nextoffset entry 4 bytes */
2377 /* then the len of the SID 4 bytes */
2378 SIVAL(entry,4,sid_len);
2380 /* unknown data 8 bytes uint64_t */
2381 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-mezte*/
2383 /* the used disk space 8 bytes uint64_t */
2384 SBIG_UINT(entry,16,qt.usedspace);
2386 /* the soft quotas 8 bytes uint64_t */
2387 SBIG_UINT(entry,24,qt.softlim);
2389 /* the hard quotas 8 bytes uint64_t */
2390 SBIG_UINT(entry,32,qt.hardlim);
2392 /* and now the SID */
2393 sid_linearize(entry+40, sid_len, &sid);
2398 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2399 reply_doserror(req, ERRSRV, ERRerror);
2404 send_nt_replies(conn, req, nt_status, params, param_len,
2408 /****************************************************************************
2409 Reply to set user quota
2410 ****************************************************************************/
2412 static void call_nt_transact_set_user_quota(connection_struct *conn,
2413 struct smb_request *req,
2417 uint32 parameter_count,
2420 uint32 max_data_count)
2422 char *params = *ppparams;
2423 char *pdata = *ppdata;
2424 int data_len=0,param_len=0;
2425 SMB_NTQUOTA_STRUCT qt;
2428 files_struct *fsp = NULL;
2433 if (conn->server_info->utok.uid != 0) {
2434 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2435 "[%s]\n", lp_servicename(SNUM(conn)),
2436 conn->server_info->unix_name));
2437 reply_doserror(req, ERRDOS, ERRnoaccess);
2442 * Ensure minimum number of parameters sent.
2445 if (parameter_count < 2) {
2446 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2447 reply_doserror(req, ERRDOS, ERRinvalidparam);
2451 /* maybe we can check the quota_fnum */
2452 fsp = file_fsp(req, SVAL(params,0));
2453 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2454 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2455 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2459 if (data_count < 40) {
2460 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2461 reply_doserror(req, ERRDOS, ERRunknownlevel);
2465 /* offset to next quota record.
2466 * 4 bytes IVAL(pdata,0)
2471 sid_len = IVAL(pdata,4);
2473 if (data_count < 40+sid_len) {
2474 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2475 reply_doserror(req, ERRDOS, ERRunknownlevel);
2479 /* unknown 8 bytes in pdata
2480 * maybe its the change time in NTTIME
2483 /* the used space 8 bytes (uint64_t)*/
2484 qt.usedspace = (uint64_t)IVAL(pdata,16);
2485 #ifdef LARGE_SMB_OFF_T
2486 qt.usedspace |= (((uint64_t)IVAL(pdata,20)) << 32);
2487 #else /* LARGE_SMB_OFF_T */
2488 if ((IVAL(pdata,20) != 0)&&
2489 ((qt.usedspace != 0xFFFFFFFF)||
2490 (IVAL(pdata,20)!=0xFFFFFFFF))) {
2491 /* more than 32 bits? */
2492 reply_doserror(req, ERRDOS, ERRunknownlevel);
2495 #endif /* LARGE_SMB_OFF_T */
2497 /* the soft quotas 8 bytes (uint64_t)*/
2498 qt.softlim = (uint64_t)IVAL(pdata,24);
2499 #ifdef LARGE_SMB_OFF_T
2500 qt.softlim |= (((uint64_t)IVAL(pdata,28)) << 32);
2501 #else /* LARGE_SMB_OFF_T */
2502 if ((IVAL(pdata,28) != 0)&&
2503 ((qt.softlim != 0xFFFFFFFF)||
2504 (IVAL(pdata,28)!=0xFFFFFFFF))) {
2505 /* more than 32 bits? */
2506 reply_doserror(req, ERRDOS, ERRunknownlevel);
2509 #endif /* LARGE_SMB_OFF_T */
2511 /* the hard quotas 8 bytes (uint64_t)*/
2512 qt.hardlim = (uint64_t)IVAL(pdata,32);
2513 #ifdef LARGE_SMB_OFF_T
2514 qt.hardlim |= (((uint64_t)IVAL(pdata,36)) << 32);
2515 #else /* LARGE_SMB_OFF_T */
2516 if ((IVAL(pdata,36) != 0)&&
2517 ((qt.hardlim != 0xFFFFFFFF)||
2518 (IVAL(pdata,36)!=0xFFFFFFFF))) {
2519 /* more than 32 bits? */
2520 reply_doserror(req, ERRDOS, ERRunknownlevel);
2523 #endif /* LARGE_SMB_OFF_T */
2525 sid_parse(pdata+40,sid_len,&sid);
2526 DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid)));
2528 /* 44 unknown bytes left... */
2530 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2531 reply_doserror(req, ERRSRV, ERRerror);
2535 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2538 #endif /* HAVE_SYS_QUOTAS */
2540 static void handle_nttrans(connection_struct *conn,
2541 struct trans_state *state,
2542 struct smb_request *req)
2544 if (Protocol >= PROTOCOL_NT1) {
2545 req->flags2 |= 0x40; /* IS_LONG_NAME */
2546 SSVAL(req->inbuf,smb_flg2,req->flags2);
2550 SMB_PERFCOUNT_SET_SUBOP(&req->pcd, state->call);
2552 /* Now we must call the relevant NT_TRANS function */
2553 switch(state->call) {
2554 case NT_TRANSACT_CREATE:
2556 START_PROFILE(NT_transact_create);
2557 call_nt_transact_create(
2559 &state->setup, state->setup_count,
2560 &state->param, state->total_param,
2561 &state->data, state->total_data,
2562 state->max_data_return);
2563 END_PROFILE(NT_transact_create);
2567 case NT_TRANSACT_IOCTL:
2569 START_PROFILE(NT_transact_ioctl);
2570 call_nt_transact_ioctl(
2572 &state->setup, state->setup_count,
2573 &state->param, state->total_param,
2574 &state->data, state->total_data,
2575 state->max_data_return);
2576 END_PROFILE(NT_transact_ioctl);
2580 case NT_TRANSACT_SET_SECURITY_DESC:
2582 START_PROFILE(NT_transact_set_security_desc);
2583 call_nt_transact_set_security_desc(
2585 &state->setup, state->setup_count,
2586 &state->param, state->total_param,
2587 &state->data, state->total_data,
2588 state->max_data_return);
2589 END_PROFILE(NT_transact_set_security_desc);
2593 case NT_TRANSACT_NOTIFY_CHANGE:
2595 START_PROFILE(NT_transact_notify_change);
2596 call_nt_transact_notify_change(
2598 &state->setup, state->setup_count,
2599 &state->param, state->total_param,
2600 &state->data, state->total_data,
2601 state->max_data_return,
2602 state->max_param_return);
2603 END_PROFILE(NT_transact_notify_change);
2607 case NT_TRANSACT_RENAME:
2609 START_PROFILE(NT_transact_rename);
2610 call_nt_transact_rename(
2612 &state->setup, state->setup_count,
2613 &state->param, state->total_param,
2614 &state->data, state->total_data,
2615 state->max_data_return);
2616 END_PROFILE(NT_transact_rename);
2620 case NT_TRANSACT_QUERY_SECURITY_DESC:
2622 START_PROFILE(NT_transact_query_security_desc);
2623 call_nt_transact_query_security_desc(
2625 &state->setup, state->setup_count,
2626 &state->param, state->total_param,
2627 &state->data, state->total_data,
2628 state->max_data_return);
2629 END_PROFILE(NT_transact_query_security_desc);
2633 #ifdef HAVE_SYS_QUOTAS
2634 case NT_TRANSACT_GET_USER_QUOTA:
2636 START_PROFILE(NT_transact_get_user_quota);
2637 call_nt_transact_get_user_quota(
2639 &state->setup, state->setup_count,
2640 &state->param, state->total_param,
2641 &state->data, state->total_data,
2642 state->max_data_return);
2643 END_PROFILE(NT_transact_get_user_quota);
2647 case NT_TRANSACT_SET_USER_QUOTA:
2649 START_PROFILE(NT_transact_set_user_quota);
2650 call_nt_transact_set_user_quota(
2652 &state->setup, state->setup_count,
2653 &state->param, state->total_param,
2654 &state->data, state->total_data,
2655 state->max_data_return);
2656 END_PROFILE(NT_transact_set_user_quota);
2659 #endif /* HAVE_SYS_QUOTAS */
2662 /* Error in request */
2663 DEBUG(0,("handle_nttrans: Unknown request %d in "
2664 "nttrans call\n", state->call));
2665 reply_doserror(req, ERRSRV, ERRerror);
2671 /****************************************************************************
2672 Reply to a SMBNTtrans.
2673 ****************************************************************************/
2675 void reply_nttrans(struct smb_request *req)
2677 connection_struct *conn = req->conn;
2682 uint16 function_code;
2684 struct trans_state *state;
2686 START_PROFILE(SMBnttrans);
2688 if (req->wct < 19) {
2689 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2690 END_PROFILE(SMBnttrans);
2694 pscnt = IVAL(req->vwv+9, 1);
2695 psoff = IVAL(req->vwv+11, 1);
2696 dscnt = IVAL(req->vwv+13, 1);
2697 dsoff = IVAL(req->vwv+15, 1);
2698 function_code = SVAL(req->vwv+18, 0);
2700 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2701 reply_doserror(req, ERRSRV, ERRaccess);
2702 END_PROFILE(SMBnttrans);
2706 result = allow_new_trans(conn->pending_trans, req->mid);
2707 if (!NT_STATUS_IS_OK(result)) {
2708 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
2709 reply_nterror(req, result);
2710 END_PROFILE(SMBnttrans);
2714 if ((state = TALLOC_P(conn, struct trans_state)) == NULL) {
2715 reply_doserror(req, ERRSRV, ERRaccess);
2716 END_PROFILE(SMBnttrans);
2720 state->cmd = SMBnttrans;
2722 state->mid = req->mid;
2723 state->vuid = req->vuid;
2724 state->total_data = IVAL(req->vwv+3, 1);
2726 state->total_param = IVAL(req->vwv+1, 1);
2727 state->param = NULL;
2728 state->max_data_return = IVAL(req->vwv+7, 1);
2729 state->max_param_return = IVAL(req->vwv+5, 1);
2731 /* setup count is in *words* */
2732 state->setup_count = 2*CVAL(req->vwv+17, 1);
2733 state->setup = NULL;
2734 state->call = function_code;
2736 DEBUG(10, ("num_setup=%u, "
2737 "param_total=%u, this_param=%u, max_param=%u, "
2738 "data_total=%u, this_data=%u, max_data=%u, "
2739 "param_offset=%u, data_offset=%u\n",
2740 (unsigned)state->setup_count,
2741 (unsigned)state->total_param, (unsigned)pscnt,
2742 (unsigned)state->max_param_return,
2743 (unsigned)state->total_data, (unsigned)dscnt,
2744 (unsigned)state->max_data_return,
2745 (unsigned)psoff, (unsigned)dsoff));
2748 * All nttrans messages we handle have smb_wct == 19 +
2749 * state->setup_count. Ensure this is so as a sanity check.
2752 if(req->wct != 19 + (state->setup_count/2)) {
2753 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2754 req->wct, 19 + (state->setup_count/2)));
2758 /* Don't allow more than 128mb for each value. */
2759 if ((state->total_data > (1024*1024*128)) ||
2760 (state->total_param > (1024*1024*128))) {
2761 reply_doserror(req, ERRDOS, ERRnomem);
2762 END_PROFILE(SMBnttrans);
2766 if ((dscnt > state->total_data) || (pscnt > state->total_param))
2769 if (state->total_data) {
2771 if (trans_oob(state->total_data, 0, dscnt)
2772 || trans_oob(smb_len(req->inbuf), dsoff, dscnt)) {
2776 /* Can't use talloc here, the core routines do realloc on the
2777 * params and data. */
2778 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
2779 DEBUG(0,("reply_nttrans: data malloc fail for %u "
2780 "bytes !\n", (unsigned int)state->total_data));
2782 reply_doserror(req, ERRDOS, ERRnomem);
2783 END_PROFILE(SMBnttrans);
2787 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
2790 if (state->total_param) {
2792 if (trans_oob(state->total_param, 0, pscnt)
2793 || trans_oob(smb_len(req->inbuf), psoff, pscnt)) {
2797 /* Can't use talloc here, the core routines do realloc on the
2798 * params and data. */
2799 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
2800 DEBUG(0,("reply_nttrans: param malloc fail for %u "
2801 "bytes !\n", (unsigned int)state->total_param));
2802 SAFE_FREE(state->data);
2804 reply_doserror(req, ERRDOS, ERRnomem);
2805 END_PROFILE(SMBnttrans);
2809 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
2812 state->received_data = dscnt;
2813 state->received_param = pscnt;
2815 if(state->setup_count > 0) {
2816 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2817 state->setup_count));
2820 * No overflow possible here, state->setup_count is an
2821 * unsigned int, being filled by a single byte from
2822 * CVAL(req->vwv+13, 0) above. The cast in the comparison
2823 * below is not necessary, it's here to clarify things. The
2824 * validity of req->vwv and req->wct has been checked in
2825 * init_smb_request already.
2827 if ((state->setup_count/2) + 19 > (unsigned int)req->wct) {
2831 state->setup = (uint16 *)TALLOC(state, state->setup_count);
2832 if (state->setup == NULL) {
2833 DEBUG(0,("reply_nttrans : Out of memory\n"));
2834 SAFE_FREE(state->data);
2835 SAFE_FREE(state->param);
2837 reply_doserror(req, ERRDOS, ERRnomem);
2838 END_PROFILE(SMBnttrans);
2842 memcpy(state->setup, req->vwv+19, state->setup_count);
2843 dump_data(10, (uint8 *)state->setup, state->setup_count);
2846 if ((state->received_data == state->total_data) &&
2847 (state->received_param == state->total_param)) {
2848 handle_nttrans(conn, state, req);
2849 SAFE_FREE(state->param);
2850 SAFE_FREE(state->data);
2852 END_PROFILE(SMBnttrans);
2856 DLIST_ADD(conn->pending_trans, state);
2858 /* We need to send an interim response then receive the rest
2859 of the parameter/data bytes */
2860 reply_outbuf(req, 0, 0);
2861 show_msg((char *)req->outbuf);
2862 END_PROFILE(SMBnttrans);
2867 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
2868 SAFE_FREE(state->data);
2869 SAFE_FREE(state->param);
2871 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2872 END_PROFILE(SMBnttrans);
2876 /****************************************************************************
2877 Reply to a SMBnttranss
2878 ****************************************************************************/
2880 void reply_nttranss(struct smb_request *req)
2882 connection_struct *conn = req->conn;
2883 uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
2884 struct trans_state *state;
2886 START_PROFILE(SMBnttranss);
2888 show_msg((char *)req->inbuf);
2890 if (req->wct < 18) {
2891 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2892 END_PROFILE(SMBnttranss);
2896 for (state = conn->pending_trans; state != NULL;
2897 state = state->next) {
2898 if (state->mid == req->mid) {
2903 if ((state == NULL) || (state->cmd != SMBnttrans)) {
2904 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2905 END_PROFILE(SMBnttranss);
2909 /* Revise state->total_param and state->total_data in case they have
2910 changed downwards */
2911 if (IVAL(req->vwv+1, 1) < state->total_param) {
2912 state->total_param = IVAL(req->vwv+1, 1);
2914 if (IVAL(req->vwv+3, 1) < state->total_data) {
2915 state->total_data = IVAL(req->vwv+3, 1);
2918 pcnt = IVAL(req->vwv+5, 1);
2919 poff = IVAL(req->vwv+7, 1);
2920 pdisp = IVAL(req->vwv+9, 1);
2922 dcnt = IVAL(req->vwv+11, 1);
2923 doff = IVAL(req->vwv+13, 1);
2924 ddisp = IVAL(req->vwv+15, 1);
2926 state->received_param += pcnt;
2927 state->received_data += dcnt;
2929 if ((state->received_data > state->total_data) ||
2930 (state->received_param > state->total_param))
2934 if (trans_oob(state->total_param, pdisp, pcnt)
2935 || trans_oob(smb_len(req->inbuf), poff, pcnt)) {
2938 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,pcnt);
2942 if (trans_oob(state->total_data, ddisp, dcnt)
2943 || trans_oob(smb_len(req->inbuf), doff, dcnt)) {
2946 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
2949 if ((state->received_param < state->total_param) ||
2950 (state->received_data < state->total_data)) {
2951 END_PROFILE(SMBnttranss);
2955 handle_nttrans(conn, state, req);
2957 DLIST_REMOVE(conn->pending_trans, state);
2958 SAFE_FREE(state->data);
2959 SAFE_FREE(state->param);
2961 END_PROFILE(SMBnttranss);
2966 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
2967 DLIST_REMOVE(conn->pending_trans, state);
2968 SAFE_FREE(state->data);
2969 SAFE_FREE(state->param);
2971 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2972 END_PROFILE(SMBnttranss);