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);
77 if (!srv_send_smb(smbd_server_fd(),
80 IS_CONN_ENCRYPTED(conn),
82 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
84 TALLOC_FREE(req->outbuf);
89 * When sending params and data ensure that both are nicely aligned.
90 * Only do this alignment when there is also data to send - else
91 * can cause NT redirector problems.
94 if (((params_to_send % 4) != 0) && (data_to_send != 0)) {
95 data_alignment_offset = 4 - (params_to_send % 4);
99 * Space is bufsize minus Netbios over TCP header minus SMB header.
100 * The alignment_offset is to align the param bytes on a four byte
101 * boundary (2 bytes for data len, one byte pad).
102 * NT needs this to work correctly.
105 useable_space = max_send - (smb_size
108 + data_alignment_offset);
110 if (useable_space < 0) {
111 char *msg = talloc_asprintf(
113 "send_nt_replies failed sanity useable_space = %d!!!",
115 DEBUG(0, ("%s\n", msg));
116 exit_server_cleanly(msg);
119 while (params_to_send || data_to_send) {
122 * Calculate whether we will totally or partially fill this packet.
125 total_sent_thistime = params_to_send + data_to_send;
128 * We can never send more than useable_space.
131 total_sent_thistime = MIN(total_sent_thistime, useable_space);
133 reply_outbuf(req, 18,
134 total_sent_thistime + alignment_offset
135 + data_alignment_offset);
138 * We might have had SMBnttranss in req->inbuf, fix that.
140 SCVAL(req->outbuf, smb_com, SMBnttrans);
143 * Set total params and data to be sent.
146 SIVAL(req->outbuf,smb_ntr_TotalParameterCount,paramsize);
147 SIVAL(req->outbuf,smb_ntr_TotalDataCount,datasize);
150 * Calculate how many parameters and data we can fit into
151 * this packet. Parameters get precedence.
154 params_sent_thistime = MIN(params_to_send,useable_space);
155 data_sent_thistime = useable_space - params_sent_thistime;
156 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
158 SIVAL(req->outbuf, smb_ntr_ParameterCount,
159 params_sent_thistime);
161 if(params_sent_thistime == 0) {
162 SIVAL(req->outbuf,smb_ntr_ParameterOffset,0);
163 SIVAL(req->outbuf,smb_ntr_ParameterDisplacement,0);
166 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
167 * parameter bytes, however the first 4 bytes of outbuf are
168 * the Netbios over TCP header. Thus use smb_base() to subtract
169 * them from the calculation.
172 SIVAL(req->outbuf,smb_ntr_ParameterOffset,
173 ((smb_buf(req->outbuf)+alignment_offset)
174 - smb_base(req->outbuf)));
176 * Absolute displacement of param bytes sent in this packet.
179 SIVAL(req->outbuf, smb_ntr_ParameterDisplacement,
184 * Deal with the data portion.
187 SIVAL(req->outbuf, smb_ntr_DataCount, data_sent_thistime);
189 if(data_sent_thistime == 0) {
190 SIVAL(req->outbuf,smb_ntr_DataOffset,0);
191 SIVAL(req->outbuf,smb_ntr_DataDisplacement, 0);
194 * The offset of the data bytes is the offset of the
195 * parameter bytes plus the number of parameters being sent this time.
198 SIVAL(req->outbuf, smb_ntr_DataOffset,
199 ((smb_buf(req->outbuf)+alignment_offset) -
200 smb_base(req->outbuf))
201 + params_sent_thistime + data_alignment_offset);
202 SIVAL(req->outbuf,smb_ntr_DataDisplacement, pd - pdata);
206 * Copy the param bytes into the packet.
209 if(params_sent_thistime) {
210 if (alignment_offset != 0) {
211 memset(smb_buf(req->outbuf), 0,
214 memcpy((smb_buf(req->outbuf)+alignment_offset), pp,
215 params_sent_thistime);
219 * Copy in the data bytes
222 if(data_sent_thistime) {
223 if (data_alignment_offset != 0) {
224 memset((smb_buf(req->outbuf)+alignment_offset+
225 params_sent_thistime), 0,
226 data_alignment_offset);
228 memcpy(smb_buf(req->outbuf)+alignment_offset
229 +params_sent_thistime+data_alignment_offset,
230 pd,data_sent_thistime);
233 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
234 params_sent_thistime, data_sent_thistime, useable_space));
235 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
236 params_to_send, data_to_send, paramsize, datasize));
238 if (NT_STATUS_V(nt_error)) {
239 error_packet_set((char *)req->outbuf,
244 /* Send the packet */
245 show_msg((char *)req->outbuf);
246 if (!srv_send_smb(smbd_server_fd(),
249 IS_CONN_ENCRYPTED(conn),
251 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
254 TALLOC_FREE(req->outbuf);
256 pp += params_sent_thistime;
257 pd += data_sent_thistime;
259 params_to_send -= params_sent_thistime;
260 data_to_send -= data_sent_thistime;
266 if(params_to_send < 0 || data_to_send < 0) {
267 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
268 params_to_send, data_to_send));
269 exit_server_cleanly("send_nt_replies: internal error");
274 /****************************************************************************
275 Is it an NTFS stream name ?
276 An NTFS file name is <path>.<extention>:<stream name>:<stream type>
277 $DATA can be used as both a stream name and a stream type. A missing stream
278 name or type implies $DATA.
280 Both Windows stream names and POSIX files can contain the ':' character.
281 This function first checks for the existence of a colon in the last component
282 of the given name. If the name contains a colon we differentiate between a
283 stream and POSIX file by checking if the latter exists through a POSIX stat.
285 Function assumes we've already chdir() to the "root" directory of fname.
286 ****************************************************************************/
288 bool is_ntfs_stream_name(const char *fname)
290 const char *lastcomp;
291 SMB_STRUCT_STAT sbuf;
293 /* If all pathnames are treated as POSIX we ignore streams. */
294 if (lp_posix_pathnames()) {
298 /* Find the last component of the name. */
299 if ((lastcomp = strrchr_m(fname, '/')) != NULL)
304 /* If there is no colon in the last component, it's not a stream. */
305 if (strchr_m(lastcomp, ':') == NULL)
309 * If file already exists on disk, it's not a stream. The stat must
310 * bypass the vfs layer so streams modules don't intefere.
312 if (sys_stat(fname, &sbuf) == 0) {
313 DEBUG(5, ("is_ntfs_stream_name: file %s contains a ':' but is "
314 "not a stream\n", fname));
321 /****************************************************************************
322 Simple check to determine if the filename is a stream.
323 ***************************************************************************/
324 bool is_ntfs_stream_smb_fname(const struct smb_filename *smb_fname)
326 if (lp_posix_pathnames()) {
330 return smb_fname->stream_name;
333 /****************************************************************************
334 Returns true if the filename's stream == "::$DATA"
335 ***************************************************************************/
336 bool is_ntfs_default_stream_smb_fname(const struct smb_filename *smb_fname)
338 if (!is_ntfs_stream_smb_fname(smb_fname)) {
342 return StrCaseCmp(smb_fname->stream_name, "::$DATA") == 0;
345 /****************************************************************************
346 Reply to an NT create and X call on a pipe
347 ****************************************************************************/
349 static void nt_open_pipe(char *fname, connection_struct *conn,
350 struct smb_request *req, int *ppnum)
355 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
357 /* Strip \\ off the name. */
360 status = open_np_file(req, fname, &fsp);
361 if (!NT_STATUS_IS_OK(status)) {
362 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
363 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
367 reply_nterror(req, status);
375 /****************************************************************************
376 Reply to an NT create and X call for pipes.
377 ****************************************************************************/
379 static void do_ntcreate_pipe_open(connection_struct *conn,
380 struct smb_request *req)
385 uint32 flags = IVAL(req->vwv+3, 1);
386 TALLOC_CTX *ctx = talloc_tos();
388 srvstr_pull_req_talloc(ctx, req, &fname, req->buf, STR_TERMINATE);
391 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
395 nt_open_pipe(fname, conn, req, &pnum);
403 * Deal with pipe return.
406 if (flags & EXTENDED_RESPONSE_REQUIRED) {
407 /* This is very strange. We
408 * return 50 words, but only set
409 * the wcnt to 42 ? It's definately
410 * what happens on the wire....
412 reply_outbuf(req, 50, 0);
413 SCVAL(req->outbuf,smb_wct,42);
415 reply_outbuf(req, 34, 0);
418 p = (char *)req->outbuf + smb_vwv2;
422 SIVAL(p,0,FILE_WAS_OPENED);
425 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
428 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
430 SSVAL(p,2, 0x5FF); /* ? */
433 if (flags & EXTENDED_RESPONSE_REQUIRED) {
435 SIVAL(p,0,FILE_GENERIC_ALL);
437 * For pipes W2K3 seems to return
439 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
441 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
444 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
449 /****************************************************************************
450 Reply to an NT create and X call.
451 ****************************************************************************/
453 void reply_ntcreate_and_X(struct smb_request *req)
455 connection_struct *conn = req->conn;
456 struct smb_filename *smb_fname = NULL;
460 uint32 file_attributes;
462 uint32 create_disposition;
463 uint32 create_options;
465 uint64_t allocation_size;
466 /* Breakout the oplock request bits so we can set the
467 reply bits separately. */
469 SMB_OFF_T file_len = 0;
471 files_struct *fsp = NULL;
473 struct timespec c_timespec;
474 struct timespec a_timespec;
475 struct timespec m_timespec;
478 uint8_t oplock_granted = NO_OPLOCK_RETURN;
479 TALLOC_CTX *ctx = talloc_tos();
481 START_PROFILE(SMBntcreateX);
484 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
488 flags = IVAL(req->vwv+3, 1);
489 access_mask = IVAL(req->vwv+7, 1);
490 file_attributes = IVAL(req->vwv+13, 1);
491 share_access = IVAL(req->vwv+15, 1);
492 create_disposition = IVAL(req->vwv+17, 1);
493 create_options = IVAL(req->vwv+19, 1);
494 root_dir_fid = (uint16)IVAL(req->vwv+5, 1);
496 allocation_size = (uint64_t)IVAL(req->vwv+9, 1);
497 #ifdef LARGE_SMB_OFF_T
498 allocation_size |= (((uint64_t)IVAL(req->vwv+11, 1)) << 32);
501 srvstr_get_path_req(ctx, req, &fname, (const char *)req->buf,
502 STR_TERMINATE, &status);
504 if (!NT_STATUS_IS_OK(status)) {
505 reply_nterror(req, status);
509 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
510 "file_attributes = 0x%x, share_access = 0x%x, "
511 "create_disposition = 0x%x create_options = 0x%x "
512 "root_dir_fid = 0x%x, fname = %s\n",
514 (unsigned int)access_mask,
515 (unsigned int)file_attributes,
516 (unsigned int)share_access,
517 (unsigned int)create_disposition,
518 (unsigned int)create_options,
519 (unsigned int)root_dir_fid,
523 * we need to remove ignored bits when they come directly from the client
524 * because we reuse some of them for internal stuff
526 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
529 * If it's an IPC, use the pipe handler.
533 if (lp_nt_pipe_support()) {
534 do_ntcreate_pipe_open(conn, req);
537 reply_doserror(req, ERRDOS, ERRnoaccess);
541 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
542 if (oplock_request) {
543 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
547 status = filename_convert(ctx,
549 req->flags2 & FLAGS2_DFS_PATHNAMES,
554 if (!NT_STATUS_IS_OK(status)) {
555 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
557 NT_STATUS_PATH_NOT_COVERED,
561 reply_nterror(req, status);
565 status = SMB_VFS_CREATE_FILE(
568 root_dir_fid, /* root_dir_fid */
569 smb_fname, /* fname */
570 access_mask, /* access_mask */
571 share_access, /* share_access */
572 create_disposition, /* create_disposition*/
573 create_options, /* create_options */
574 file_attributes, /* file_attributes */
575 oplock_request, /* oplock_request */
576 allocation_size, /* allocation_size */
582 if (!NT_STATUS_IS_OK(status)) {
583 if (open_was_deferred(req->mid)) {
584 /* We have re-scheduled this call, no error. */
587 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
588 reply_botherror(req, status, ERRDOS, ERRfilexists);
591 reply_nterror(req, status);
597 * If the caller set the extended oplock request bit
598 * and we granted one (by whatever means) - set the
599 * correct bit for extended oplock reply.
602 if (oplock_request &&
603 (lp_fake_oplocks(SNUM(conn))
604 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
607 * Exclusive oplock granted
610 if (flags & REQUEST_BATCH_OPLOCK) {
611 oplock_granted = BATCH_OPLOCK_RETURN;
613 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
615 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
616 oplock_granted = LEVEL_II_OPLOCK_RETURN;
618 oplock_granted = NO_OPLOCK_RETURN;
621 file_len = smb_fname->st.st_ex_size;
622 fattr = dos_mode(conn,fsp->fsp_name,&smb_fname->st);
624 fattr = FILE_ATTRIBUTE_NORMAL;
627 if (flags & EXTENDED_RESPONSE_REQUIRED) {
628 /* This is very strange. We
629 * return 50 words, but only set
630 * the wcnt to 42 ? It's definately
631 * what happens on the wire....
633 reply_outbuf(req, 50, 0);
634 SCVAL(req->outbuf,smb_wct,42);
636 reply_outbuf(req, 34, 0);
639 p = (char *)req->outbuf + smb_vwv2;
641 SCVAL(p, 0, oplock_granted);
644 SSVAL(p,0,fsp->fnum);
646 if ((create_disposition == FILE_SUPERSEDE)
647 && (info == FILE_WAS_OVERWRITTEN)) {
648 SIVAL(p,0,FILE_WAS_SUPERSEDED);
655 c_timespec = smb_fname->st.st_ex_btime;
656 a_timespec = smb_fname->st.st_ex_atime;
657 m_timespec = smb_fname->st.st_ex_mtime;
659 if (lp_dos_filetime_resolution(SNUM(conn))) {
660 dos_filetime_timespec(&c_timespec);
661 dos_filetime_timespec(&a_timespec);
662 dos_filetime_timespec(&m_timespec);
665 put_long_date_timespec(p, c_timespec); /* create time. */
667 put_long_date_timespec(p, a_timespec); /* access time */
669 put_long_date_timespec(p, m_timespec); /* write time */
671 put_long_date_timespec(p, m_timespec); /* change time */
673 SIVAL(p,0,fattr); /* File Attributes. */
675 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn,fsp,&smb_fname->st));
677 SOFF_T(p,0,file_len);
679 if (flags & EXTENDED_RESPONSE_REQUIRED) {
683 SCVAL(p,0,fsp->is_directory ? 1 : 0);
685 if (flags & EXTENDED_RESPONSE_REQUIRED) {
688 if (fsp->is_directory ||
689 can_write_to_file(conn, smb_fname)) {
690 perms = FILE_GENERIC_ALL;
692 perms = FILE_GENERIC_READ|FILE_EXECUTE;
697 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n",
698 fsp->fnum, fsp->fsp_name));
702 TALLOC_FREE(smb_fname);
703 END_PROFILE(SMBntcreateX);
707 /****************************************************************************
708 Reply to a NT_TRANSACT_CREATE call to open a pipe.
709 ****************************************************************************/
711 static void do_nt_transact_create_pipe(connection_struct *conn,
712 struct smb_request *req,
713 uint16 **ppsetup, uint32 setup_count,
714 char **ppparams, uint32 parameter_count,
715 char **ppdata, uint32 data_count)
718 char *params = *ppparams;
724 TALLOC_CTX *ctx = talloc_tos();
727 * Ensure minimum number of parameters sent.
730 if(parameter_count < 54) {
731 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
732 reply_doserror(req, ERRDOS, ERRnoaccess);
736 flags = IVAL(params,0);
738 srvstr_get_path(ctx, params, req->flags2, &fname, params+53,
739 parameter_count-53, STR_TERMINATE,
741 if (!NT_STATUS_IS_OK(status)) {
742 reply_nterror(req, status);
746 nt_open_pipe(fname, conn, req, &pnum);
753 /* Realloc the size of parameters and data we will return */
754 if (flags & EXTENDED_RESPONSE_REQUIRED) {
755 /* Extended response is 32 more byyes. */
760 params = nttrans_realloc(ppparams, param_len);
762 reply_doserror(req, ERRDOS, ERRnomem);
767 SCVAL(p,0,NO_OPLOCK_RETURN);
772 SIVAL(p,0,FILE_WAS_OPENED);
776 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
779 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
781 SSVAL(p,2, 0x5FF); /* ? */
784 if (flags & EXTENDED_RESPONSE_REQUIRED) {
786 SIVAL(p,0,FILE_GENERIC_ALL);
788 * For pipes W2K3 seems to return
790 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
792 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
795 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
797 /* Send the required number of replies */
798 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
803 /****************************************************************************
804 Internal fn to set security descriptors.
805 ****************************************************************************/
807 static NTSTATUS set_sd(files_struct *fsp, uint8 *data, uint32 sd_len,
808 uint32 security_info_sent)
810 SEC_DESC *psd = NULL;
813 if (sd_len == 0 || !lp_nt_acl_support(SNUM(fsp->conn))) {
817 status = unmarshall_sec_desc(talloc_tos(), data, sd_len, &psd);
819 if (!NT_STATUS_IS_OK(status)) {
823 if (psd->owner_sid == NULL) {
824 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
826 if (psd->group_sid == NULL) {
827 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
830 /* Convert all the generic bits. */
831 security_acl_map_generic(psd->dacl, &file_generic_mapping);
832 security_acl_map_generic(psd->sacl, &file_generic_mapping);
834 if (DEBUGLEVEL >= 10) {
835 DEBUG(10,("set_sd for file %s\n", fsp->fsp_name ));
836 NDR_PRINT_DEBUG(security_descriptor, psd);
839 status = SMB_VFS_FSET_NT_ACL(fsp, security_info_sent, psd);
846 /****************************************************************************
847 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
848 ****************************************************************************/
850 static struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
852 struct ea_list *ea_list_head = NULL;
859 while (offset + 4 <= data_size) {
860 size_t next_offset = IVAL(pdata,offset);
861 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
867 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
868 if (next_offset == 0) {
871 offset += next_offset;
877 /****************************************************************************
878 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
879 ****************************************************************************/
881 static void call_nt_transact_create(connection_struct *conn,
882 struct smb_request *req,
883 uint16 **ppsetup, uint32 setup_count,
884 char **ppparams, uint32 parameter_count,
885 char **ppdata, uint32 data_count,
886 uint32 max_data_count)
888 struct smb_filename *smb_fname = NULL;
890 char *params = *ppparams;
891 char *data = *ppdata;
892 /* Breakout the oplock request bits so we can set the reply bits separately. */
894 SMB_OFF_T file_len = 0;
896 files_struct *fsp = NULL;
900 uint32 file_attributes;
902 uint32 create_disposition;
903 uint32 create_options;
905 struct security_descriptor *sd = NULL;
908 struct timespec c_timespec;
909 struct timespec a_timespec;
910 struct timespec m_timespec;
911 struct ea_list *ea_list = NULL;
914 uint64_t allocation_size;
916 uint8_t oplock_granted;
917 TALLOC_CTX *ctx = talloc_tos();
919 DEBUG(5,("call_nt_transact_create\n"));
922 * If it's an IPC, use the pipe handler.
926 if (lp_nt_pipe_support()) {
927 do_nt_transact_create_pipe(
929 ppsetup, setup_count,
930 ppparams, parameter_count,
934 reply_doserror(req, ERRDOS, ERRnoaccess);
939 * Ensure minimum number of parameters sent.
942 if(parameter_count < 54) {
943 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
944 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
948 flags = IVAL(params,0);
949 access_mask = IVAL(params,8);
950 file_attributes = IVAL(params,20);
951 share_access = IVAL(params,24);
952 create_disposition = IVAL(params,28);
953 create_options = IVAL(params,32);
954 sd_len = IVAL(params,36);
955 ea_len = IVAL(params,40);
956 root_dir_fid = (uint16)IVAL(params,4);
957 allocation_size = (uint64_t)IVAL(params,12);
958 #ifdef LARGE_SMB_OFF_T
959 allocation_size |= (((uint64_t)IVAL(params,16)) << 32);
963 * we need to remove ignored bits when they come directly from the client
964 * because we reuse some of them for internal stuff
966 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
968 /* Ensure the data_len is correct for the sd and ea values given. */
969 if ((ea_len + sd_len > data_count)
970 || (ea_len > data_count) || (sd_len > data_count)
971 || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
972 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
973 "%u, data_count = %u\n", (unsigned int)ea_len,
974 (unsigned int)sd_len, (unsigned int)data_count));
975 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
980 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
983 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
985 if (!NT_STATUS_IS_OK(status)) {
986 DEBUG(10, ("call_nt_transact_create: "
987 "unmarshall_sec_desc failed: %s\n",
989 reply_nterror(req, status);
995 if (!lp_ea_support(SNUM(conn))) {
996 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
997 "EA's not supported.\n",
998 (unsigned int)ea_len));
999 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
1004 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
1005 "too small (should be more than 10)\n",
1006 (unsigned int)ea_len ));
1007 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1011 /* We have already checked that ea_len <= data_count here. */
1012 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
1014 if (ea_list == NULL) {
1015 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1020 srvstr_get_path(ctx, params, req->flags2, &fname,
1021 params+53, parameter_count-53,
1022 STR_TERMINATE, &status);
1023 if (!NT_STATUS_IS_OK(status)) {
1024 reply_nterror(req, status);
1028 status = filename_convert(ctx,
1030 req->flags2 & FLAGS2_DFS_PATHNAMES,
1035 if (!NT_STATUS_IS_OK(status)) {
1036 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1037 reply_botherror(req,
1038 NT_STATUS_PATH_NOT_COVERED,
1039 ERRSRV, ERRbadpath);
1042 reply_nterror(req, status);
1046 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1047 if (oplock_request) {
1048 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
1052 status = SMB_VFS_CREATE_FILE(
1055 root_dir_fid, /* root_dir_fid */
1056 smb_fname, /* fname */
1057 access_mask, /* access_mask */
1058 share_access, /* share_access */
1059 create_disposition, /* create_disposition*/
1060 create_options, /* create_options */
1061 file_attributes, /* file_attributes */
1062 oplock_request, /* oplock_request */
1063 allocation_size, /* allocation_size */
1065 ea_list, /* ea_list */
1069 if(!NT_STATUS_IS_OK(status)) {
1070 if (open_was_deferred(req->mid)) {
1071 /* We have re-scheduled this call, no error. */
1074 reply_openerror(req, status);
1079 * If the caller set the extended oplock request bit
1080 * and we granted one (by whatever means) - set the
1081 * correct bit for extended oplock reply.
1084 if (oplock_request &&
1085 (lp_fake_oplocks(SNUM(conn))
1086 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
1089 * Exclusive oplock granted
1092 if (flags & REQUEST_BATCH_OPLOCK) {
1093 oplock_granted = BATCH_OPLOCK_RETURN;
1095 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
1097 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1098 oplock_granted = LEVEL_II_OPLOCK_RETURN;
1100 oplock_granted = NO_OPLOCK_RETURN;
1103 file_len = smb_fname->st.st_ex_size;
1104 fattr = dos_mode(conn, fsp->fsp_name, &smb_fname->st);
1106 fattr = FILE_ATTRIBUTE_NORMAL;
1109 /* Realloc the size of parameters and data we will return */
1110 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1111 /* Extended response is 32 more byyes. */
1116 params = nttrans_realloc(ppparams, param_len);
1117 if(params == NULL) {
1118 reply_doserror(req, ERRDOS, ERRnomem);
1123 SCVAL(p, 0, oplock_granted);
1126 SSVAL(p,0,fsp->fnum);
1128 if ((create_disposition == FILE_SUPERSEDE)
1129 && (info == FILE_WAS_OVERWRITTEN)) {
1130 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1137 c_timespec = smb_fname->st.st_ex_btime;
1138 a_timespec = smb_fname->st.st_ex_atime;
1139 m_timespec = smb_fname->st.st_ex_mtime;
1141 if (lp_dos_filetime_resolution(SNUM(conn))) {
1142 dos_filetime_timespec(&c_timespec);
1143 dos_filetime_timespec(&a_timespec);
1144 dos_filetime_timespec(&m_timespec);
1147 put_long_date_timespec(p, c_timespec); /* create time. */
1149 put_long_date_timespec(p, a_timespec); /* access time */
1151 put_long_date_timespec(p, m_timespec); /* write time */
1153 put_long_date_timespec(p, m_timespec); /* change time */
1155 SIVAL(p,0,fattr); /* File Attributes. */
1157 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn, fsp, &smb_fname->st));
1159 SOFF_T(p,0,file_len);
1161 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1165 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1167 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1170 if (fsp->is_directory ||
1171 can_write_to_file(conn, smb_fname)) {
1172 perms = FILE_GENERIC_ALL;
1174 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1179 DEBUG(5,("call_nt_transact_create: open name = %s\n", fsp->fsp_name));
1181 /* Send the required number of replies */
1182 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1184 TALLOC_FREE(smb_fname);
1188 /****************************************************************************
1189 Reply to a NT CANCEL request.
1190 conn POINTER CAN BE NULL HERE !
1191 ****************************************************************************/
1193 void reply_ntcancel(struct smb_request *req)
1196 * Go through and cancel any pending change notifies.
1199 START_PROFILE(SMBntcancel);
1200 srv_cancel_sign_response(smbd_server_conn);
1201 remove_pending_change_notify_requests_by_mid(req->mid);
1202 remove_pending_lock_requests_by_mid(req->mid);
1204 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", req->mid));
1206 END_PROFILE(SMBntcancel);
1210 /****************************************************************************
1212 ****************************************************************************/
1214 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
1215 connection_struct *conn,
1216 struct smb_request *req,
1217 struct smb_filename *smb_fname_src,
1218 struct smb_filename *smb_fname_dst,
1221 char *oldname = NULL;
1222 char *newname = NULL;
1223 files_struct *fsp1,*fsp2;
1227 NTSTATUS status = NT_STATUS_OK;
1230 if (!CAN_WRITE(conn)) {
1231 status = NT_STATUS_MEDIA_WRITE_PROTECTED;
1235 /* Source must already exist. */
1236 if (!VALID_STAT(smb_fname_src->st)) {
1237 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
1241 status = get_full_smb_filename(ctx, smb_fname_src, &oldname);
1242 if (!NT_STATUS_IS_OK(status)) {
1246 /* Ensure attributes match. */
1247 fattr = dos_mode(conn, oldname, &smb_fname_src->st);
1248 if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
1249 status = NT_STATUS_NO_SUCH_FILE;
1253 /* Disallow if dst file already exists. */
1254 if (VALID_STAT(smb_fname_dst->st)) {
1255 status = NT_STATUS_OBJECT_NAME_COLLISION;
1259 /* No links from a directory. */
1260 if (S_ISDIR(smb_fname_src->st.st_ex_mode)) {
1261 status = NT_STATUS_FILE_IS_A_DIRECTORY;
1265 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1266 smb_fname_str_dbg(smb_fname_src),
1267 smb_fname_str_dbg(smb_fname_dst)));
1269 status = SMB_VFS_CREATE_FILE(
1272 0, /* root_dir_fid */
1273 smb_fname_src, /* fname */
1274 FILE_READ_DATA, /* access_mask */
1275 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1277 FILE_OPEN, /* create_disposition*/
1278 0, /* create_options */
1279 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
1280 NO_OPLOCK, /* oplock_request */
1281 0, /* allocation_size */
1287 if (!NT_STATUS_IS_OK(status)) {
1291 status = SMB_VFS_CREATE_FILE(
1294 0, /* root_dir_fid */
1295 smb_fname_dst, /* fname */
1296 FILE_WRITE_DATA, /* access_mask */
1297 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1299 FILE_CREATE, /* create_disposition*/
1300 0, /* create_options */
1301 fattr, /* file_attributes */
1302 NO_OPLOCK, /* oplock_request */
1303 0, /* allocation_size */
1309 if (!NT_STATUS_IS_OK(status)) {
1310 close_file(NULL, fsp1, ERROR_CLOSE);
1314 if (smb_fname_src->st.st_ex_size) {
1315 ret = vfs_transfer_file(fsp1, fsp2, smb_fname_src->st.st_ex_size);
1319 * As we are opening fsp1 read-only we only expect
1320 * an error on close on fsp2 if we are out of space.
1321 * Thus we don't look at the error return from the
1324 close_file(NULL, fsp1, NORMAL_CLOSE);
1326 /* Ensure the modtime is set correctly on the destination file. */
1327 set_close_write_time(fsp2, smb_fname_src->st.st_ex_mtime);
1329 status = close_file(NULL, fsp2, NORMAL_CLOSE);
1331 status = get_full_smb_filename(ctx, smb_fname_dst, &newname);
1332 if (!NT_STATUS_IS_OK(status)) {
1336 /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
1337 creates the file. This isn't the correct thing to do in the copy
1339 if (!parent_dirname(talloc_tos(), newname, &parent, NULL)) {
1340 status = NT_STATUS_NO_MEMORY;
1343 file_set_dosmode(conn, newname, fattr, &smb_fname_dst->st, parent,
1345 TALLOC_FREE(parent);
1347 if (ret < (SMB_OFF_T)smb_fname_src->st.st_ex_size) {
1348 status = NT_STATUS_DISK_FULL;
1352 if (!NT_STATUS_IS_OK(status)) {
1353 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1354 nt_errstr(status), smb_fname_str_dbg(smb_fname_src),
1355 smb_fname_str_dbg(smb_fname_dst)));
1358 TALLOC_FREE(oldname);
1359 TALLOC_FREE(newname);
1364 /****************************************************************************
1365 Reply to a NT rename request.
1366 ****************************************************************************/
1368 void reply_ntrename(struct smb_request *req)
1370 connection_struct *conn = req->conn;
1371 struct smb_filename *smb_fname_old = NULL;
1372 struct smb_filename *smb_fname_new = NULL;
1373 char *oldname = NULL;
1374 char *newname = NULL;
1377 bool src_has_wcard = False;
1378 bool dest_has_wcard = False;
1381 TALLOC_CTX *ctx = talloc_tos();
1383 START_PROFILE(SMBntrename);
1386 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1387 END_PROFILE(SMBntrename);
1391 attrs = SVAL(req->vwv+0, 0);
1392 rename_type = SVAL(req->vwv+1, 0);
1394 p = (const char *)req->buf + 1;
1395 p += srvstr_get_path_req_wcard(ctx, req, &oldname, p, STR_TERMINATE,
1396 &status, &src_has_wcard);
1397 if (!NT_STATUS_IS_OK(status)) {
1398 reply_nterror(req, status);
1399 END_PROFILE(SMBntrename);
1403 if (ms_has_wild(oldname)) {
1404 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1405 END_PROFILE(SMBntrename);
1410 p += srvstr_get_path_req_wcard(ctx, req, &newname, p, STR_TERMINATE,
1411 &status, &dest_has_wcard);
1412 if (!NT_STATUS_IS_OK(status)) {
1413 reply_nterror(req, status);
1414 END_PROFILE(SMBntrename);
1418 status = filename_convert(ctx, conn,
1419 req->flags2 & FLAGS2_DFS_PATHNAMES,
1423 if (!NT_STATUS_IS_OK(status)) {
1424 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1425 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1426 ERRSRV, ERRbadpath);
1427 END_PROFILE(SMBntrename);
1430 reply_nterror(req, status);
1431 END_PROFILE(SMBntrename);
1435 status = filename_convert(ctx, conn,
1436 req->flags2 & FLAGS2_DFS_PATHNAMES,
1440 if (!NT_STATUS_IS_OK(status)) {
1441 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1442 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1443 ERRSRV, ERRbadpath);
1444 END_PROFILE(SMBntrename);
1447 reply_nterror(req, status);
1448 END_PROFILE(SMBntrename);
1452 /* The new name must begin with a ':' if the old name is a stream. */
1453 if (is_ntfs_stream_name(oldname) && (newname[0] != ':')) {
1454 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1455 END_PROFILE(SMBntrename);
1459 DEBUG(3,("reply_ntrename : %s -> %s\n",oldname,newname));
1461 switch(rename_type) {
1462 case RENAME_FLAG_RENAME:
1463 status = rename_internals(ctx, conn, req, oldname,
1464 newname, attrs, False, src_has_wcard,
1465 dest_has_wcard, DELETE_ACCESS);
1467 case RENAME_FLAG_HARD_LINK:
1468 if (src_has_wcard || dest_has_wcard) {
1470 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1472 status = hardlink_internals(ctx,
1478 case RENAME_FLAG_COPY:
1479 if (src_has_wcard || dest_has_wcard) {
1481 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1483 status = copy_internals(ctx, conn, req,
1489 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1490 status = NT_STATUS_INVALID_PARAMETER;
1493 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1497 if (!NT_STATUS_IS_OK(status)) {
1498 if (open_was_deferred(req->mid)) {
1499 /* We have re-scheduled this call. */
1500 END_PROFILE(SMBntrename);
1504 reply_nterror(req, status);
1505 END_PROFILE(SMBntrename);
1509 reply_outbuf(req, 0, 0);
1511 END_PROFILE(SMBntrename);
1515 /****************************************************************************
1516 Reply to a notify change - queue the request and
1517 don't allow a directory to be opened.
1518 ****************************************************************************/
1520 static void call_nt_transact_notify_change(connection_struct *conn,
1521 struct smb_request *req,
1525 uint32 parameter_count,
1526 char **ppdata, uint32 data_count,
1527 uint32 max_data_count,
1528 uint32 max_param_count)
1530 uint16 *setup = *ppsetup;
1536 if(setup_count < 6) {
1537 reply_doserror(req, ERRDOS, ERRbadfunc);
1541 fsp = file_fsp(req, SVAL(setup,4));
1542 filter = IVAL(setup, 0);
1543 recursive = (SVAL(setup, 6) != 0) ? True : False;
1545 DEBUG(3,("call_nt_transact_notify_change\n"));
1548 reply_doserror(req, ERRDOS, ERRbadfid);
1553 char *filter_string;
1555 if (!(filter_string = notify_filter_string(NULL, filter))) {
1556 reply_nterror(req,NT_STATUS_NO_MEMORY);
1560 DEBUG(3,("call_nt_transact_notify_change: notify change "
1561 "called on %s, filter = %s, recursive = %d\n",
1562 fsp->fsp_name, filter_string, recursive));
1564 TALLOC_FREE(filter_string);
1567 if((!fsp->is_directory) || (conn != fsp->conn)) {
1568 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1572 if (fsp->notify == NULL) {
1574 status = change_notify_create(fsp, filter, recursive);
1576 if (!NT_STATUS_IS_OK(status)) {
1577 DEBUG(10, ("change_notify_create returned %s\n",
1578 nt_errstr(status)));
1579 reply_nterror(req, status);
1584 if (fsp->notify->num_changes != 0) {
1587 * We've got changes pending, respond immediately
1591 * TODO: write a torture test to check the filtering behaviour
1595 change_notify_reply(fsp->conn, req,
1601 * change_notify_reply() above has independently sent its
1608 * No changes pending, queue the request
1611 status = change_notify_add_request(req,
1615 if (!NT_STATUS_IS_OK(status)) {
1616 reply_nterror(req, status);
1621 /****************************************************************************
1622 Reply to an NT transact rename command.
1623 ****************************************************************************/
1625 static void call_nt_transact_rename(connection_struct *conn,
1626 struct smb_request *req,
1627 uint16 **ppsetup, uint32 setup_count,
1628 char **ppparams, uint32 parameter_count,
1629 char **ppdata, uint32 data_count,
1630 uint32 max_data_count)
1632 char *params = *ppparams;
1633 char *new_name = NULL;
1634 files_struct *fsp = NULL;
1635 bool dest_has_wcard = False;
1637 TALLOC_CTX *ctx = talloc_tos();
1639 if(parameter_count < 5) {
1640 reply_doserror(req, ERRDOS, ERRbadfunc);
1644 fsp = file_fsp(req, SVAL(params, 0));
1645 if (!check_fsp(conn, req, fsp)) {
1648 srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
1649 parameter_count - 4,
1650 STR_TERMINATE, &status, &dest_has_wcard);
1651 if (!NT_STATUS_IS_OK(status)) {
1652 reply_nterror(req, status);
1657 * W2K3 ignores this request as the RAW-RENAME test
1658 * demonstrates, so we do.
1660 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1662 DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1663 fsp->fsp_name, new_name));
1668 /******************************************************************************
1669 Fake up a completely empty SD.
1670 *******************************************************************************/
1672 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
1676 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1678 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1679 return NT_STATUS_NO_MEMORY;
1682 return NT_STATUS_OK;
1685 /****************************************************************************
1686 Reply to query a security descriptor.
1687 ****************************************************************************/
1689 static void call_nt_transact_query_security_desc(connection_struct *conn,
1690 struct smb_request *req,
1694 uint32 parameter_count,
1697 uint32 max_data_count)
1699 char *params = *ppparams;
1700 char *data = *ppdata;
1701 SEC_DESC *psd = NULL;
1703 uint32 security_info_wanted;
1704 files_struct *fsp = NULL;
1708 if(parameter_count < 8) {
1709 reply_doserror(req, ERRDOS, ERRbadfunc);
1713 fsp = file_fsp(req, SVAL(params,0));
1715 reply_doserror(req, ERRDOS, ERRbadfid);
1719 security_info_wanted = IVAL(params,4);
1721 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, info_wanted = 0x%x\n", fsp->fsp_name,
1722 (unsigned int)security_info_wanted ));
1724 params = nttrans_realloc(ppparams, 4);
1725 if(params == NULL) {
1726 reply_doserror(req, ERRDOS, ERRnomem);
1731 * Get the permissions to return.
1734 if (!lp_nt_acl_support(SNUM(conn))) {
1735 status = get_null_nt_acl(talloc_tos(), &psd);
1737 status = SMB_VFS_FGET_NT_ACL(
1738 fsp, security_info_wanted, &psd);
1740 if (!NT_STATUS_IS_OK(status)) {
1741 reply_nterror(req, status);
1745 /* If the SACL/DACL is NULL, but was requested, we mark that it is
1746 * present in the reply to match Windows behavior */
1747 if (psd->sacl == NULL &&
1748 security_info_wanted & SACL_SECURITY_INFORMATION)
1749 psd->type |= SEC_DESC_SACL_PRESENT;
1750 if (psd->dacl == NULL &&
1751 security_info_wanted & DACL_SECURITY_INFORMATION)
1752 psd->type |= SEC_DESC_DACL_PRESENT;
1754 sd_size = ndr_size_security_descriptor(psd, NULL, 0);
1756 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
1758 if (DEBUGLEVEL >= 10) {
1759 DEBUG(10,("call_nt_transact_query_security_desc for file %s\n", fsp->fsp_name));
1760 NDR_PRINT_DEBUG(security_descriptor, psd);
1763 SIVAL(params,0,(uint32)sd_size);
1765 if (max_data_count < sd_size) {
1766 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
1767 params, 4, *ppdata, 0);
1772 * Allocate the data we will point this at.
1775 data = nttrans_realloc(ppdata, sd_size);
1777 reply_doserror(req, ERRDOS, ERRnomem);
1781 status = marshall_sec_desc(talloc_tos(), psd,
1782 &blob.data, &blob.length);
1784 if (!NT_STATUS_IS_OK(status)) {
1785 reply_nterror(req, status);
1789 SMB_ASSERT(sd_size == blob.length);
1790 memcpy(data, blob.data, sd_size);
1792 send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
1797 /****************************************************************************
1798 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
1799 ****************************************************************************/
1801 static void call_nt_transact_set_security_desc(connection_struct *conn,
1802 struct smb_request *req,
1806 uint32 parameter_count,
1809 uint32 max_data_count)
1811 char *params= *ppparams;
1812 char *data = *ppdata;
1813 files_struct *fsp = NULL;
1814 uint32 security_info_sent = 0;
1817 if(parameter_count < 8) {
1818 reply_doserror(req, ERRDOS, ERRbadfunc);
1822 if((fsp = file_fsp(req, SVAL(params,0))) == NULL) {
1823 reply_doserror(req, ERRDOS, ERRbadfid);
1827 if(!lp_nt_acl_support(SNUM(conn))) {
1831 security_info_sent = IVAL(params,4);
1833 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
1834 (unsigned int)security_info_sent ));
1836 if (data_count == 0) {
1837 reply_doserror(req, ERRDOS, ERRnoaccess);
1841 status = set_sd(fsp, (uint8 *)data, data_count, security_info_sent);
1843 if (!NT_STATUS_IS_OK(status)) {
1844 reply_nterror(req, status);
1849 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1853 /****************************************************************************
1855 ****************************************************************************/
1857 static void call_nt_transact_ioctl(connection_struct *conn,
1858 struct smb_request *req,
1859 uint16 **ppsetup, uint32 setup_count,
1860 char **ppparams, uint32 parameter_count,
1861 char **ppdata, uint32 data_count,
1862 uint32 max_data_count)
1869 char *pdata = *ppdata;
1871 if (setup_count != 8) {
1872 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
1873 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1877 function = IVAL(*ppsetup, 0);
1878 fidnum = SVAL(*ppsetup, 4);
1879 isFSctl = CVAL(*ppsetup, 6);
1880 compfilter = CVAL(*ppsetup, 7);
1882 DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
1883 function, fidnum, isFSctl, compfilter));
1885 fsp=file_fsp(req, fidnum);
1886 /* this check is done in each implemented function case for now
1887 because I don't want to break anything... --metze
1888 FSP_BELONGS_CONN(fsp,conn);*/
1890 SMB_PERFCOUNT_SET_IOCTL(&req->pcd, function);
1893 case FSCTL_SET_SPARSE:
1894 /* pretend this succeeded - tho strictly we should
1895 mark the file sparse (if the local fs supports it)
1896 so we can know if we need to pre-allocate or not */
1898 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
1899 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1902 case FSCTL_CREATE_OR_GET_OBJECT_ID:
1904 unsigned char objid[16];
1906 /* This should return the object-id on this file.
1907 * I think I'll make this be the inode+dev. JRA.
1910 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
1912 if (!fsp_belongs_conn(conn, req, fsp)) {
1917 pdata = nttrans_realloc(ppdata, data_count);
1918 if (pdata == NULL) {
1919 reply_nterror(req, NT_STATUS_NO_MEMORY);
1923 /* For backwards compatibility only store the dev/inode. */
1924 push_file_id_16(pdata, &fsp->file_id);
1925 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
1926 push_file_id_16(pdata+32, &fsp->file_id);
1927 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
1932 case FSCTL_GET_REPARSE_POINT:
1933 /* pretend this fail - my winXP does it like this
1937 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1938 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
1941 case FSCTL_SET_REPARSE_POINT:
1942 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
1946 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1947 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
1950 case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
1953 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
1954 * and return their volume names. If max_data_count is 16, then it is just
1955 * asking for the number of volumes and length of the combined names.
1957 * pdata is the data allocated by our caller, but that uses
1958 * total_data_count (which is 0 in our case) rather than max_data_count.
1959 * Allocate the correct amount and return the pointer to let
1960 * it be deallocated when we return.
1962 SHADOW_COPY_DATA *shadow_data = NULL;
1963 TALLOC_CTX *shadow_mem_ctx = NULL;
1964 bool labels = False;
1965 uint32 labels_data_count = 0;
1969 if (!fsp_belongs_conn(conn, req, fsp)) {
1973 if (max_data_count < 16) {
1974 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
1976 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1980 if (max_data_count > 16) {
1984 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
1985 if (shadow_mem_ctx == NULL) {
1986 DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
1987 reply_nterror(req, NT_STATUS_NO_MEMORY);
1991 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
1992 if (shadow_data == NULL) {
1993 DEBUG(0,("TALLOC_ZERO() failed!\n"));
1994 talloc_destroy(shadow_mem_ctx);
1995 reply_nterror(req, NT_STATUS_NO_MEMORY);
1999 shadow_data->mem_ctx = shadow_mem_ctx;
2002 * Call the VFS routine to actually do the work.
2004 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
2005 talloc_destroy(shadow_data->mem_ctx);
2006 if (errno == ENOSYS) {
2007 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
2008 conn->connectpath));
2009 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2012 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
2013 conn->connectpath));
2014 reply_nterror(req, NT_STATUS_UNSUCCESSFUL);
2019 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
2024 data_count = 12+labels_data_count+4;
2027 if (max_data_count<data_count) {
2028 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
2029 max_data_count,data_count));
2030 talloc_destroy(shadow_data->mem_ctx);
2031 reply_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
2035 pdata = nttrans_realloc(ppdata, data_count);
2036 if (pdata == NULL) {
2037 talloc_destroy(shadow_data->mem_ctx);
2038 reply_nterror(req, NT_STATUS_NO_MEMORY);
2044 /* num_volumes 4 bytes */
2045 SIVAL(pdata,0,shadow_data->num_volumes);
2048 /* num_labels 4 bytes */
2049 SIVAL(pdata,4,shadow_data->num_volumes);
2052 /* needed_data_count 4 bytes */
2053 SIVAL(pdata,8,labels_data_count);
2057 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
2058 shadow_data->num_volumes,fsp->fsp_name));
2059 if (labels && shadow_data->labels) {
2060 for (i=0;i<shadow_data->num_volumes;i++) {
2061 srvstr_push(pdata, req->flags2,
2062 cur_pdata, shadow_data->labels[i],
2063 2*sizeof(SHADOW_COPY_LABEL),
2064 STR_UNICODE|STR_TERMINATE);
2065 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
2066 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
2070 talloc_destroy(shadow_data->mem_ctx);
2072 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
2078 case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
2080 /* pretend this succeeded -
2082 * we have to send back a list with all files owned by this SID
2084 * but I have to check that --metze
2088 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
2090 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
2092 if (!fsp_belongs_conn(conn, req, fsp)) {
2096 /* unknown 4 bytes: this is not the length of the sid :-( */
2097 /*unknown = IVAL(pdata,0);*/
2099 sid_parse(pdata+4,sid_len,&sid);
2100 DEBUGADD(10, ("for SID: %s\n", sid_string_dbg(&sid)));
2102 if (!sid_to_uid(&sid, &uid)) {
2103 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2104 sid_string_dbg(&sid),
2105 (unsigned long)sid_len));
2109 /* we can take a look at the find source :-)
2111 * find ./ -uid $uid -name '*' is what we need here
2114 * and send 4bytes len and then NULL terminated unicode strings
2117 * but I don't know how to deal with the paged results
2118 * (maybe we can hang the result anywhere in the fsp struct)
2120 * we don't send all files at once
2121 * and at the next we should *not* start from the beginning,
2122 * so we have to cache the result
2127 /* this works for now... */
2128 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2132 if (!logged_ioctl_message) {
2133 logged_ioctl_message = true; /* Only print this once... */
2134 DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
2139 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2143 #ifdef HAVE_SYS_QUOTAS
2144 /****************************************************************************
2145 Reply to get user quota
2146 ****************************************************************************/
2148 static void call_nt_transact_get_user_quota(connection_struct *conn,
2149 struct smb_request *req,
2153 uint32 parameter_count,
2156 uint32 max_data_count)
2158 NTSTATUS nt_status = NT_STATUS_OK;
2159 char *params = *ppparams;
2160 char *pdata = *ppdata;
2162 int data_len=0,param_len=0;
2165 files_struct *fsp = NULL;
2169 bool start_enum = True;
2170 SMB_NTQUOTA_STRUCT qt;
2171 SMB_NTQUOTA_LIST *tmp_list;
2172 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2177 if (conn->server_info->utok.uid != 0) {
2178 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2179 "[%s]\n", lp_servicename(SNUM(conn)),
2180 conn->server_info->unix_name));
2181 reply_doserror(req, ERRDOS, ERRnoaccess);
2186 * Ensure minimum number of parameters sent.
2189 if (parameter_count < 4) {
2190 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2191 reply_doserror(req, ERRDOS, ERRinvalidparam);
2195 /* maybe we can check the quota_fnum */
2196 fsp = file_fsp(req, SVAL(params,0));
2197 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2198 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2199 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2203 /* the NULL pointer checking for fsp->fake_file_handle->pd
2204 * is done by CHECK_NTQUOTA_HANDLE_OK()
2206 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->private_data;
2208 level = SVAL(params,2);
2210 /* unknown 12 bytes leading in params */
2213 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2214 /* seems that we should continue with the enum here --metze */
2216 if (qt_handle->quota_list!=NULL &&
2217 qt_handle->tmp_list==NULL) {
2220 free_ntquota_list(&(qt_handle->quota_list));
2222 /* Realloc the size of parameters and data we will return */
2224 params = nttrans_realloc(ppparams, param_len);
2225 if(params == NULL) {
2226 reply_doserror(req, ERRDOS, ERRnomem);
2231 SIVAL(params,0,data_len);
2238 case TRANSACT_GET_USER_QUOTA_LIST_START:
2240 if (qt_handle->quota_list==NULL &&
2241 qt_handle->tmp_list==NULL) {
2245 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2246 reply_doserror(req, ERRSRV, ERRerror);
2250 /* Realloc the size of parameters and data we will return */
2252 params = nttrans_realloc(ppparams, param_len);
2253 if(params == NULL) {
2254 reply_doserror(req, ERRDOS, ERRnomem);
2258 /* we should not trust the value in max_data_count*/
2259 max_data_count = MIN(max_data_count,2048);
2261 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2263 reply_doserror(req, ERRDOS, ERRnomem);
2269 /* set params Size of returned Quota Data 4 bytes*/
2270 /* but set it later when we know it */
2272 /* for each entry push the data */
2275 qt_handle->tmp_list = qt_handle->quota_list;
2278 tmp_list = qt_handle->tmp_list;
2280 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2281 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2283 sid_len = ndr_size_dom_sid(
2284 &tmp_list->quotas->sid, NULL, 0);
2285 entry_len = 40 + sid_len;
2287 /* nextoffset entry 4 bytes */
2288 SIVAL(entry,0,entry_len);
2290 /* then the len of the SID 4 bytes */
2291 SIVAL(entry,4,sid_len);
2293 /* unknown data 8 bytes uint64_t */
2294 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-metze*/
2296 /* the used disk space 8 bytes uint64_t */
2297 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2299 /* the soft quotas 8 bytes uint64_t */
2300 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2302 /* the hard quotas 8 bytes uint64_t */
2303 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2305 /* and now the SID */
2306 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2309 qt_handle->tmp_list = tmp_list;
2311 /* overwrite the offset of the last entry */
2312 SIVAL(entry-entry_len,0,0);
2314 data_len = 4+qt_len;
2315 /* overwrite the params quota_data_len */
2316 SIVAL(params,0,data_len);
2320 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2322 /* unknown 4 bytes IVAL(pdata,0) */
2324 if (data_count < 8) {
2325 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2326 reply_doserror(req, ERRDOS, ERRunknownlevel);
2330 sid_len = IVAL(pdata,4);
2331 /* Ensure this is less than 1mb. */
2332 if (sid_len > (1024*1024)) {
2333 reply_doserror(req, ERRDOS, ERRnomem);
2337 if (data_count < 8+sid_len) {
2338 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2339 reply_doserror(req, ERRDOS, ERRunknownlevel);
2343 data_len = 4+40+sid_len;
2345 if (max_data_count < data_len) {
2346 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2347 max_data_count, data_len));
2349 SIVAL(params,0,data_len);
2351 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2355 sid_parse(pdata+8,sid_len,&sid);
2357 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2360 * we have to return zero's in all fields
2361 * instead of returning an error here
2366 /* Realloc the size of parameters and data we will return */
2368 params = nttrans_realloc(ppparams, param_len);
2369 if(params == NULL) {
2370 reply_doserror(req, ERRDOS, ERRnomem);
2374 pdata = nttrans_realloc(ppdata, data_len);
2376 reply_doserror(req, ERRDOS, ERRnomem);
2382 /* set params Size of returned Quota Data 4 bytes*/
2383 SIVAL(params,0,data_len);
2385 /* nextoffset entry 4 bytes */
2388 /* then the len of the SID 4 bytes */
2389 SIVAL(entry,4,sid_len);
2391 /* unknown data 8 bytes uint64_t */
2392 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-mezte*/
2394 /* the used disk space 8 bytes uint64_t */
2395 SBIG_UINT(entry,16,qt.usedspace);
2397 /* the soft quotas 8 bytes uint64_t */
2398 SBIG_UINT(entry,24,qt.softlim);
2400 /* the hard quotas 8 bytes uint64_t */
2401 SBIG_UINT(entry,32,qt.hardlim);
2403 /* and now the SID */
2404 sid_linearize(entry+40, sid_len, &sid);
2409 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2410 reply_doserror(req, ERRSRV, ERRerror);
2415 send_nt_replies(conn, req, nt_status, params, param_len,
2419 /****************************************************************************
2420 Reply to set user quota
2421 ****************************************************************************/
2423 static void call_nt_transact_set_user_quota(connection_struct *conn,
2424 struct smb_request *req,
2428 uint32 parameter_count,
2431 uint32 max_data_count)
2433 char *params = *ppparams;
2434 char *pdata = *ppdata;
2435 int data_len=0,param_len=0;
2436 SMB_NTQUOTA_STRUCT qt;
2439 files_struct *fsp = NULL;
2444 if (conn->server_info->utok.uid != 0) {
2445 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2446 "[%s]\n", lp_servicename(SNUM(conn)),
2447 conn->server_info->unix_name));
2448 reply_doserror(req, ERRDOS, ERRnoaccess);
2453 * Ensure minimum number of parameters sent.
2456 if (parameter_count < 2) {
2457 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2458 reply_doserror(req, ERRDOS, ERRinvalidparam);
2462 /* maybe we can check the quota_fnum */
2463 fsp = file_fsp(req, SVAL(params,0));
2464 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2465 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2466 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2470 if (data_count < 40) {
2471 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2472 reply_doserror(req, ERRDOS, ERRunknownlevel);
2476 /* offset to next quota record.
2477 * 4 bytes IVAL(pdata,0)
2482 sid_len = IVAL(pdata,4);
2484 if (data_count < 40+sid_len) {
2485 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2486 reply_doserror(req, ERRDOS, ERRunknownlevel);
2490 /* unknown 8 bytes in pdata
2491 * maybe its the change time in NTTIME
2494 /* the used space 8 bytes (uint64_t)*/
2495 qt.usedspace = (uint64_t)IVAL(pdata,16);
2496 #ifdef LARGE_SMB_OFF_T
2497 qt.usedspace |= (((uint64_t)IVAL(pdata,20)) << 32);
2498 #else /* LARGE_SMB_OFF_T */
2499 if ((IVAL(pdata,20) != 0)&&
2500 ((qt.usedspace != 0xFFFFFFFF)||
2501 (IVAL(pdata,20)!=0xFFFFFFFF))) {
2502 /* more than 32 bits? */
2503 reply_doserror(req, ERRDOS, ERRunknownlevel);
2506 #endif /* LARGE_SMB_OFF_T */
2508 /* the soft quotas 8 bytes (uint64_t)*/
2509 qt.softlim = (uint64_t)IVAL(pdata,24);
2510 #ifdef LARGE_SMB_OFF_T
2511 qt.softlim |= (((uint64_t)IVAL(pdata,28)) << 32);
2512 #else /* LARGE_SMB_OFF_T */
2513 if ((IVAL(pdata,28) != 0)&&
2514 ((qt.softlim != 0xFFFFFFFF)||
2515 (IVAL(pdata,28)!=0xFFFFFFFF))) {
2516 /* more than 32 bits? */
2517 reply_doserror(req, ERRDOS, ERRunknownlevel);
2520 #endif /* LARGE_SMB_OFF_T */
2522 /* the hard quotas 8 bytes (uint64_t)*/
2523 qt.hardlim = (uint64_t)IVAL(pdata,32);
2524 #ifdef LARGE_SMB_OFF_T
2525 qt.hardlim |= (((uint64_t)IVAL(pdata,36)) << 32);
2526 #else /* LARGE_SMB_OFF_T */
2527 if ((IVAL(pdata,36) != 0)&&
2528 ((qt.hardlim != 0xFFFFFFFF)||
2529 (IVAL(pdata,36)!=0xFFFFFFFF))) {
2530 /* more than 32 bits? */
2531 reply_doserror(req, ERRDOS, ERRunknownlevel);
2534 #endif /* LARGE_SMB_OFF_T */
2536 sid_parse(pdata+40,sid_len,&sid);
2537 DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid)));
2539 /* 44 unknown bytes left... */
2541 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2542 reply_doserror(req, ERRSRV, ERRerror);
2546 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2549 #endif /* HAVE_SYS_QUOTAS */
2551 static void handle_nttrans(connection_struct *conn,
2552 struct trans_state *state,
2553 struct smb_request *req)
2555 if (Protocol >= PROTOCOL_NT1) {
2556 req->flags2 |= 0x40; /* IS_LONG_NAME */
2557 SSVAL(req->inbuf,smb_flg2,req->flags2);
2561 SMB_PERFCOUNT_SET_SUBOP(&req->pcd, state->call);
2563 /* Now we must call the relevant NT_TRANS function */
2564 switch(state->call) {
2565 case NT_TRANSACT_CREATE:
2567 START_PROFILE(NT_transact_create);
2568 call_nt_transact_create(
2570 &state->setup, state->setup_count,
2571 &state->param, state->total_param,
2572 &state->data, state->total_data,
2573 state->max_data_return);
2574 END_PROFILE(NT_transact_create);
2578 case NT_TRANSACT_IOCTL:
2580 START_PROFILE(NT_transact_ioctl);
2581 call_nt_transact_ioctl(
2583 &state->setup, state->setup_count,
2584 &state->param, state->total_param,
2585 &state->data, state->total_data,
2586 state->max_data_return);
2587 END_PROFILE(NT_transact_ioctl);
2591 case NT_TRANSACT_SET_SECURITY_DESC:
2593 START_PROFILE(NT_transact_set_security_desc);
2594 call_nt_transact_set_security_desc(
2596 &state->setup, state->setup_count,
2597 &state->param, state->total_param,
2598 &state->data, state->total_data,
2599 state->max_data_return);
2600 END_PROFILE(NT_transact_set_security_desc);
2604 case NT_TRANSACT_NOTIFY_CHANGE:
2606 START_PROFILE(NT_transact_notify_change);
2607 call_nt_transact_notify_change(
2609 &state->setup, state->setup_count,
2610 &state->param, state->total_param,
2611 &state->data, state->total_data,
2612 state->max_data_return,
2613 state->max_param_return);
2614 END_PROFILE(NT_transact_notify_change);
2618 case NT_TRANSACT_RENAME:
2620 START_PROFILE(NT_transact_rename);
2621 call_nt_transact_rename(
2623 &state->setup, state->setup_count,
2624 &state->param, state->total_param,
2625 &state->data, state->total_data,
2626 state->max_data_return);
2627 END_PROFILE(NT_transact_rename);
2631 case NT_TRANSACT_QUERY_SECURITY_DESC:
2633 START_PROFILE(NT_transact_query_security_desc);
2634 call_nt_transact_query_security_desc(
2636 &state->setup, state->setup_count,
2637 &state->param, state->total_param,
2638 &state->data, state->total_data,
2639 state->max_data_return);
2640 END_PROFILE(NT_transact_query_security_desc);
2644 #ifdef HAVE_SYS_QUOTAS
2645 case NT_TRANSACT_GET_USER_QUOTA:
2647 START_PROFILE(NT_transact_get_user_quota);
2648 call_nt_transact_get_user_quota(
2650 &state->setup, state->setup_count,
2651 &state->param, state->total_param,
2652 &state->data, state->total_data,
2653 state->max_data_return);
2654 END_PROFILE(NT_transact_get_user_quota);
2658 case NT_TRANSACT_SET_USER_QUOTA:
2660 START_PROFILE(NT_transact_set_user_quota);
2661 call_nt_transact_set_user_quota(
2663 &state->setup, state->setup_count,
2664 &state->param, state->total_param,
2665 &state->data, state->total_data,
2666 state->max_data_return);
2667 END_PROFILE(NT_transact_set_user_quota);
2670 #endif /* HAVE_SYS_QUOTAS */
2673 /* Error in request */
2674 DEBUG(0,("handle_nttrans: Unknown request %d in "
2675 "nttrans call\n", state->call));
2676 reply_doserror(req, ERRSRV, ERRerror);
2682 /****************************************************************************
2683 Reply to a SMBNTtrans.
2684 ****************************************************************************/
2686 void reply_nttrans(struct smb_request *req)
2688 connection_struct *conn = req->conn;
2693 uint16 function_code;
2695 struct trans_state *state;
2697 START_PROFILE(SMBnttrans);
2699 if (req->wct < 19) {
2700 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2701 END_PROFILE(SMBnttrans);
2705 pscnt = IVAL(req->vwv+9, 1);
2706 psoff = IVAL(req->vwv+11, 1);
2707 dscnt = IVAL(req->vwv+13, 1);
2708 dsoff = IVAL(req->vwv+15, 1);
2709 function_code = SVAL(req->vwv+18, 0);
2711 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2712 reply_doserror(req, ERRSRV, ERRaccess);
2713 END_PROFILE(SMBnttrans);
2717 result = allow_new_trans(conn->pending_trans, req->mid);
2718 if (!NT_STATUS_IS_OK(result)) {
2719 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
2720 reply_nterror(req, result);
2721 END_PROFILE(SMBnttrans);
2725 if ((state = TALLOC_P(conn, struct trans_state)) == NULL) {
2726 reply_doserror(req, ERRSRV, ERRaccess);
2727 END_PROFILE(SMBnttrans);
2731 state->cmd = SMBnttrans;
2733 state->mid = req->mid;
2734 state->vuid = req->vuid;
2735 state->total_data = IVAL(req->vwv+3, 1);
2737 state->total_param = IVAL(req->vwv+1, 1);
2738 state->param = NULL;
2739 state->max_data_return = IVAL(req->vwv+7, 1);
2740 state->max_param_return = IVAL(req->vwv+5, 1);
2742 /* setup count is in *words* */
2743 state->setup_count = 2*CVAL(req->vwv+17, 1);
2744 state->setup = NULL;
2745 state->call = function_code;
2747 DEBUG(10, ("num_setup=%u, "
2748 "param_total=%u, this_param=%u, max_param=%u, "
2749 "data_total=%u, this_data=%u, max_data=%u, "
2750 "param_offset=%u, data_offset=%u\n",
2751 (unsigned)state->setup_count,
2752 (unsigned)state->total_param, (unsigned)pscnt,
2753 (unsigned)state->max_param_return,
2754 (unsigned)state->total_data, (unsigned)dscnt,
2755 (unsigned)state->max_data_return,
2756 (unsigned)psoff, (unsigned)dsoff));
2759 * All nttrans messages we handle have smb_wct == 19 +
2760 * state->setup_count. Ensure this is so as a sanity check.
2763 if(req->wct != 19 + (state->setup_count/2)) {
2764 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2765 req->wct, 19 + (state->setup_count/2)));
2769 /* Don't allow more than 128mb for each value. */
2770 if ((state->total_data > (1024*1024*128)) ||
2771 (state->total_param > (1024*1024*128))) {
2772 reply_doserror(req, ERRDOS, ERRnomem);
2773 END_PROFILE(SMBnttrans);
2777 if ((dscnt > state->total_data) || (pscnt > state->total_param))
2780 if (state->total_data) {
2782 if (trans_oob(state->total_data, 0, dscnt)
2783 || trans_oob(smb_len(req->inbuf), dsoff, dscnt)) {
2787 /* Can't use talloc here, the core routines do realloc on the
2788 * params and data. */
2789 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
2790 DEBUG(0,("reply_nttrans: data malloc fail for %u "
2791 "bytes !\n", (unsigned int)state->total_data));
2793 reply_doserror(req, ERRDOS, ERRnomem);
2794 END_PROFILE(SMBnttrans);
2798 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
2801 if (state->total_param) {
2803 if (trans_oob(state->total_param, 0, pscnt)
2804 || trans_oob(smb_len(req->inbuf), psoff, pscnt)) {
2808 /* Can't use talloc here, the core routines do realloc on the
2809 * params and data. */
2810 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
2811 DEBUG(0,("reply_nttrans: param malloc fail for %u "
2812 "bytes !\n", (unsigned int)state->total_param));
2813 SAFE_FREE(state->data);
2815 reply_doserror(req, ERRDOS, ERRnomem);
2816 END_PROFILE(SMBnttrans);
2820 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
2823 state->received_data = dscnt;
2824 state->received_param = pscnt;
2826 if(state->setup_count > 0) {
2827 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2828 state->setup_count));
2831 * No overflow possible here, state->setup_count is an
2832 * unsigned int, being filled by a single byte from
2833 * CVAL(req->vwv+13, 0) above. The cast in the comparison
2834 * below is not necessary, it's here to clarify things. The
2835 * validity of req->vwv and req->wct has been checked in
2836 * init_smb_request already.
2838 if ((state->setup_count/2) + 19 > (unsigned int)req->wct) {
2842 state->setup = (uint16 *)TALLOC(state, state->setup_count);
2843 if (state->setup == NULL) {
2844 DEBUG(0,("reply_nttrans : Out of memory\n"));
2845 SAFE_FREE(state->data);
2846 SAFE_FREE(state->param);
2848 reply_doserror(req, ERRDOS, ERRnomem);
2849 END_PROFILE(SMBnttrans);
2853 memcpy(state->setup, req->vwv+19, state->setup_count);
2854 dump_data(10, (uint8 *)state->setup, state->setup_count);
2857 if ((state->received_data == state->total_data) &&
2858 (state->received_param == state->total_param)) {
2859 handle_nttrans(conn, state, req);
2860 SAFE_FREE(state->param);
2861 SAFE_FREE(state->data);
2863 END_PROFILE(SMBnttrans);
2867 DLIST_ADD(conn->pending_trans, state);
2869 /* We need to send an interim response then receive the rest
2870 of the parameter/data bytes */
2871 reply_outbuf(req, 0, 0);
2872 show_msg((char *)req->outbuf);
2873 END_PROFILE(SMBnttrans);
2878 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
2879 SAFE_FREE(state->data);
2880 SAFE_FREE(state->param);
2882 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2883 END_PROFILE(SMBnttrans);
2887 /****************************************************************************
2888 Reply to a SMBnttranss
2889 ****************************************************************************/
2891 void reply_nttranss(struct smb_request *req)
2893 connection_struct *conn = req->conn;
2894 uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
2895 struct trans_state *state;
2897 START_PROFILE(SMBnttranss);
2899 show_msg((char *)req->inbuf);
2901 if (req->wct < 18) {
2902 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2903 END_PROFILE(SMBnttranss);
2907 for (state = conn->pending_trans; state != NULL;
2908 state = state->next) {
2909 if (state->mid == req->mid) {
2914 if ((state == NULL) || (state->cmd != SMBnttrans)) {
2915 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2916 END_PROFILE(SMBnttranss);
2920 /* Revise state->total_param and state->total_data in case they have
2921 changed downwards */
2922 if (IVAL(req->vwv+1, 1) < state->total_param) {
2923 state->total_param = IVAL(req->vwv+1, 1);
2925 if (IVAL(req->vwv+3, 1) < state->total_data) {
2926 state->total_data = IVAL(req->vwv+3, 1);
2929 pcnt = IVAL(req->vwv+5, 1);
2930 poff = IVAL(req->vwv+7, 1);
2931 pdisp = IVAL(req->vwv+9, 1);
2933 dcnt = IVAL(req->vwv+11, 1);
2934 doff = IVAL(req->vwv+13, 1);
2935 ddisp = IVAL(req->vwv+15, 1);
2937 state->received_param += pcnt;
2938 state->received_data += dcnt;
2940 if ((state->received_data > state->total_data) ||
2941 (state->received_param > state->total_param))
2945 if (trans_oob(state->total_param, pdisp, pcnt)
2946 || trans_oob(smb_len(req->inbuf), poff, pcnt)) {
2949 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,pcnt);
2953 if (trans_oob(state->total_data, ddisp, dcnt)
2954 || trans_oob(smb_len(req->inbuf), doff, dcnt)) {
2957 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
2960 if ((state->received_param < state->total_param) ||
2961 (state->received_data < state->total_data)) {
2962 END_PROFILE(SMBnttranss);
2966 handle_nttrans(conn, state, req);
2968 DLIST_REMOVE(conn->pending_trans, state);
2969 SAFE_FREE(state->data);
2970 SAFE_FREE(state->param);
2972 END_PROFILE(SMBnttranss);
2977 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
2978 DLIST_REMOVE(conn->pending_trans, state);
2979 SAFE_FREE(state->data);
2980 SAFE_FREE(state->param);
2982 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2983 END_PROFILE(SMBnttranss);