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 smbd_smb1_notify_reply(struct smb_request *req,
1521 NTSTATUS error_code,
1522 uint8_t *buf, size_t len)
1524 send_nt_replies(req->conn, req, error_code, (char *)buf, len, NULL, 0);
1527 static void call_nt_transact_notify_change(connection_struct *conn,
1528 struct smb_request *req,
1532 uint32 parameter_count,
1533 char **ppdata, uint32 data_count,
1534 uint32 max_data_count,
1535 uint32 max_param_count)
1537 uint16 *setup = *ppsetup;
1543 if(setup_count < 6) {
1544 reply_doserror(req, ERRDOS, ERRbadfunc);
1548 fsp = file_fsp(req, SVAL(setup,4));
1549 filter = IVAL(setup, 0);
1550 recursive = (SVAL(setup, 6) != 0) ? True : False;
1552 DEBUG(3,("call_nt_transact_notify_change\n"));
1555 reply_doserror(req, ERRDOS, ERRbadfid);
1560 char *filter_string;
1562 if (!(filter_string = notify_filter_string(NULL, filter))) {
1563 reply_nterror(req,NT_STATUS_NO_MEMORY);
1567 DEBUG(3,("call_nt_transact_notify_change: notify change "
1568 "called on %s, filter = %s, recursive = %d\n",
1569 fsp->fsp_name, filter_string, recursive));
1571 TALLOC_FREE(filter_string);
1574 if((!fsp->is_directory) || (conn != fsp->conn)) {
1575 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1579 if (fsp->notify == NULL) {
1581 status = change_notify_create(fsp, filter, recursive);
1583 if (!NT_STATUS_IS_OK(status)) {
1584 DEBUG(10, ("change_notify_create returned %s\n",
1585 nt_errstr(status)));
1586 reply_nterror(req, status);
1591 if (fsp->notify->num_changes != 0) {
1594 * We've got changes pending, respond immediately
1598 * TODO: write a torture test to check the filtering behaviour
1602 change_notify_reply(fsp->conn, req,
1606 smbd_smb1_notify_reply);
1609 * change_notify_reply() above has independently sent its
1616 * No changes pending, queue the request
1619 status = change_notify_add_request(req,
1623 smbd_smb1_notify_reply);
1624 if (!NT_STATUS_IS_OK(status)) {
1625 reply_nterror(req, status);
1630 /****************************************************************************
1631 Reply to an NT transact rename command.
1632 ****************************************************************************/
1634 static void call_nt_transact_rename(connection_struct *conn,
1635 struct smb_request *req,
1636 uint16 **ppsetup, uint32 setup_count,
1637 char **ppparams, uint32 parameter_count,
1638 char **ppdata, uint32 data_count,
1639 uint32 max_data_count)
1641 char *params = *ppparams;
1642 char *new_name = NULL;
1643 files_struct *fsp = NULL;
1644 bool dest_has_wcard = False;
1646 TALLOC_CTX *ctx = talloc_tos();
1648 if(parameter_count < 5) {
1649 reply_doserror(req, ERRDOS, ERRbadfunc);
1653 fsp = file_fsp(req, SVAL(params, 0));
1654 if (!check_fsp(conn, req, fsp)) {
1657 srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
1658 parameter_count - 4,
1659 STR_TERMINATE, &status, &dest_has_wcard);
1660 if (!NT_STATUS_IS_OK(status)) {
1661 reply_nterror(req, status);
1666 * W2K3 ignores this request as the RAW-RENAME test
1667 * demonstrates, so we do.
1669 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1671 DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1672 fsp->fsp_name, new_name));
1677 /******************************************************************************
1678 Fake up a completely empty SD.
1679 *******************************************************************************/
1681 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
1685 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1687 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1688 return NT_STATUS_NO_MEMORY;
1691 return NT_STATUS_OK;
1694 /****************************************************************************
1695 Reply to query a security descriptor.
1696 ****************************************************************************/
1698 static void call_nt_transact_query_security_desc(connection_struct *conn,
1699 struct smb_request *req,
1703 uint32 parameter_count,
1706 uint32 max_data_count)
1708 char *params = *ppparams;
1709 char *data = *ppdata;
1710 SEC_DESC *psd = NULL;
1712 uint32 security_info_wanted;
1713 files_struct *fsp = NULL;
1717 if(parameter_count < 8) {
1718 reply_doserror(req, ERRDOS, ERRbadfunc);
1722 fsp = file_fsp(req, SVAL(params,0));
1724 reply_doserror(req, ERRDOS, ERRbadfid);
1728 security_info_wanted = IVAL(params,4);
1730 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, info_wanted = 0x%x\n", fsp->fsp_name,
1731 (unsigned int)security_info_wanted ));
1733 params = nttrans_realloc(ppparams, 4);
1734 if(params == NULL) {
1735 reply_doserror(req, ERRDOS, ERRnomem);
1740 * Get the permissions to return.
1743 if (!lp_nt_acl_support(SNUM(conn))) {
1744 status = get_null_nt_acl(talloc_tos(), &psd);
1746 status = SMB_VFS_FGET_NT_ACL(
1747 fsp, security_info_wanted, &psd);
1749 if (!NT_STATUS_IS_OK(status)) {
1750 reply_nterror(req, status);
1754 /* If the SACL/DACL is NULL, but was requested, we mark that it is
1755 * present in the reply to match Windows behavior */
1756 if (psd->sacl == NULL &&
1757 security_info_wanted & SACL_SECURITY_INFORMATION)
1758 psd->type |= SEC_DESC_SACL_PRESENT;
1759 if (psd->dacl == NULL &&
1760 security_info_wanted & DACL_SECURITY_INFORMATION)
1761 psd->type |= SEC_DESC_DACL_PRESENT;
1763 sd_size = ndr_size_security_descriptor(psd, NULL, 0);
1765 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
1767 if (DEBUGLEVEL >= 10) {
1768 DEBUG(10,("call_nt_transact_query_security_desc for file %s\n", fsp->fsp_name));
1769 NDR_PRINT_DEBUG(security_descriptor, psd);
1772 SIVAL(params,0,(uint32)sd_size);
1774 if (max_data_count < sd_size) {
1775 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
1776 params, 4, *ppdata, 0);
1781 * Allocate the data we will point this at.
1784 data = nttrans_realloc(ppdata, sd_size);
1786 reply_doserror(req, ERRDOS, ERRnomem);
1790 status = marshall_sec_desc(talloc_tos(), psd,
1791 &blob.data, &blob.length);
1793 if (!NT_STATUS_IS_OK(status)) {
1794 reply_nterror(req, status);
1798 SMB_ASSERT(sd_size == blob.length);
1799 memcpy(data, blob.data, sd_size);
1801 send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
1806 /****************************************************************************
1807 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
1808 ****************************************************************************/
1810 static void call_nt_transact_set_security_desc(connection_struct *conn,
1811 struct smb_request *req,
1815 uint32 parameter_count,
1818 uint32 max_data_count)
1820 char *params= *ppparams;
1821 char *data = *ppdata;
1822 files_struct *fsp = NULL;
1823 uint32 security_info_sent = 0;
1826 if(parameter_count < 8) {
1827 reply_doserror(req, ERRDOS, ERRbadfunc);
1831 if((fsp = file_fsp(req, SVAL(params,0))) == NULL) {
1832 reply_doserror(req, ERRDOS, ERRbadfid);
1836 if(!lp_nt_acl_support(SNUM(conn))) {
1840 security_info_sent = IVAL(params,4);
1842 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
1843 (unsigned int)security_info_sent ));
1845 if (data_count == 0) {
1846 reply_doserror(req, ERRDOS, ERRnoaccess);
1850 status = set_sd(fsp, (uint8 *)data, data_count, security_info_sent);
1852 if (!NT_STATUS_IS_OK(status)) {
1853 reply_nterror(req, status);
1858 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1862 /****************************************************************************
1864 ****************************************************************************/
1866 static void call_nt_transact_ioctl(connection_struct *conn,
1867 struct smb_request *req,
1868 uint16 **ppsetup, uint32 setup_count,
1869 char **ppparams, uint32 parameter_count,
1870 char **ppdata, uint32 data_count,
1871 uint32 max_data_count)
1878 char *pdata = *ppdata;
1880 if (setup_count != 8) {
1881 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
1882 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1886 function = IVAL(*ppsetup, 0);
1887 fidnum = SVAL(*ppsetup, 4);
1888 isFSctl = CVAL(*ppsetup, 6);
1889 compfilter = CVAL(*ppsetup, 7);
1891 DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
1892 function, fidnum, isFSctl, compfilter));
1894 fsp=file_fsp(req, fidnum);
1895 /* this check is done in each implemented function case for now
1896 because I don't want to break anything... --metze
1897 FSP_BELONGS_CONN(fsp,conn);*/
1899 SMB_PERFCOUNT_SET_IOCTL(&req->pcd, function);
1902 case FSCTL_SET_SPARSE:
1903 /* pretend this succeeded - tho strictly we should
1904 mark the file sparse (if the local fs supports it)
1905 so we can know if we need to pre-allocate or not */
1907 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
1908 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1911 case FSCTL_CREATE_OR_GET_OBJECT_ID:
1913 unsigned char objid[16];
1915 /* This should return the object-id on this file.
1916 * I think I'll make this be the inode+dev. JRA.
1919 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
1921 if (!fsp_belongs_conn(conn, req, fsp)) {
1926 pdata = nttrans_realloc(ppdata, data_count);
1927 if (pdata == NULL) {
1928 reply_nterror(req, NT_STATUS_NO_MEMORY);
1932 /* For backwards compatibility only store the dev/inode. */
1933 push_file_id_16(pdata, &fsp->file_id);
1934 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
1935 push_file_id_16(pdata+32, &fsp->file_id);
1936 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
1941 case FSCTL_GET_REPARSE_POINT:
1942 /* pretend this fail - my winXP does it like this
1946 DEBUG(10,("FSCTL_GET_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_SET_REPARSE_POINT:
1951 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
1955 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1956 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
1959 case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
1962 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
1963 * and return their volume names. If max_data_count is 16, then it is just
1964 * asking for the number of volumes and length of the combined names.
1966 * pdata is the data allocated by our caller, but that uses
1967 * total_data_count (which is 0 in our case) rather than max_data_count.
1968 * Allocate the correct amount and return the pointer to let
1969 * it be deallocated when we return.
1971 SHADOW_COPY_DATA *shadow_data = NULL;
1972 TALLOC_CTX *shadow_mem_ctx = NULL;
1973 bool labels = False;
1974 uint32 labels_data_count = 0;
1978 if (!fsp_belongs_conn(conn, req, fsp)) {
1982 if (max_data_count < 16) {
1983 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
1985 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1989 if (max_data_count > 16) {
1993 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
1994 if (shadow_mem_ctx == NULL) {
1995 DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
1996 reply_nterror(req, NT_STATUS_NO_MEMORY);
2000 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
2001 if (shadow_data == NULL) {
2002 DEBUG(0,("TALLOC_ZERO() failed!\n"));
2003 talloc_destroy(shadow_mem_ctx);
2004 reply_nterror(req, NT_STATUS_NO_MEMORY);
2008 shadow_data->mem_ctx = shadow_mem_ctx;
2011 * Call the VFS routine to actually do the work.
2013 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
2014 talloc_destroy(shadow_data->mem_ctx);
2015 if (errno == ENOSYS) {
2016 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
2017 conn->connectpath));
2018 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2021 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
2022 conn->connectpath));
2023 reply_nterror(req, NT_STATUS_UNSUCCESSFUL);
2028 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
2033 data_count = 12+labels_data_count+4;
2036 if (max_data_count<data_count) {
2037 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
2038 max_data_count,data_count));
2039 talloc_destroy(shadow_data->mem_ctx);
2040 reply_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
2044 pdata = nttrans_realloc(ppdata, data_count);
2045 if (pdata == NULL) {
2046 talloc_destroy(shadow_data->mem_ctx);
2047 reply_nterror(req, NT_STATUS_NO_MEMORY);
2053 /* num_volumes 4 bytes */
2054 SIVAL(pdata,0,shadow_data->num_volumes);
2057 /* num_labels 4 bytes */
2058 SIVAL(pdata,4,shadow_data->num_volumes);
2061 /* needed_data_count 4 bytes */
2062 SIVAL(pdata,8,labels_data_count);
2066 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
2067 shadow_data->num_volumes,fsp->fsp_name));
2068 if (labels && shadow_data->labels) {
2069 for (i=0;i<shadow_data->num_volumes;i++) {
2070 srvstr_push(pdata, req->flags2,
2071 cur_pdata, shadow_data->labels[i],
2072 2*sizeof(SHADOW_COPY_LABEL),
2073 STR_UNICODE|STR_TERMINATE);
2074 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
2075 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
2079 talloc_destroy(shadow_data->mem_ctx);
2081 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
2087 case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
2089 /* pretend this succeeded -
2091 * we have to send back a list with all files owned by this SID
2093 * but I have to check that --metze
2097 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
2099 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
2101 if (!fsp_belongs_conn(conn, req, fsp)) {
2105 /* unknown 4 bytes: this is not the length of the sid :-( */
2106 /*unknown = IVAL(pdata,0);*/
2108 sid_parse(pdata+4,sid_len,&sid);
2109 DEBUGADD(10, ("for SID: %s\n", sid_string_dbg(&sid)));
2111 if (!sid_to_uid(&sid, &uid)) {
2112 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2113 sid_string_dbg(&sid),
2114 (unsigned long)sid_len));
2118 /* we can take a look at the find source :-)
2120 * find ./ -uid $uid -name '*' is what we need here
2123 * and send 4bytes len and then NULL terminated unicode strings
2126 * but I don't know how to deal with the paged results
2127 * (maybe we can hang the result anywhere in the fsp struct)
2129 * we don't send all files at once
2130 * and at the next we should *not* start from the beginning,
2131 * so we have to cache the result
2136 /* this works for now... */
2137 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2141 if (!logged_ioctl_message) {
2142 logged_ioctl_message = true; /* Only print this once... */
2143 DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
2148 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2152 #ifdef HAVE_SYS_QUOTAS
2153 /****************************************************************************
2154 Reply to get user quota
2155 ****************************************************************************/
2157 static void call_nt_transact_get_user_quota(connection_struct *conn,
2158 struct smb_request *req,
2162 uint32 parameter_count,
2165 uint32 max_data_count)
2167 NTSTATUS nt_status = NT_STATUS_OK;
2168 char *params = *ppparams;
2169 char *pdata = *ppdata;
2171 int data_len=0,param_len=0;
2174 files_struct *fsp = NULL;
2178 bool start_enum = True;
2179 SMB_NTQUOTA_STRUCT qt;
2180 SMB_NTQUOTA_LIST *tmp_list;
2181 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2186 if (conn->server_info->utok.uid != 0) {
2187 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2188 "[%s]\n", lp_servicename(SNUM(conn)),
2189 conn->server_info->unix_name));
2190 reply_doserror(req, ERRDOS, ERRnoaccess);
2195 * Ensure minimum number of parameters sent.
2198 if (parameter_count < 4) {
2199 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2200 reply_doserror(req, ERRDOS, ERRinvalidparam);
2204 /* maybe we can check the quota_fnum */
2205 fsp = file_fsp(req, SVAL(params,0));
2206 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2207 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2208 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2212 /* the NULL pointer checking for fsp->fake_file_handle->pd
2213 * is done by CHECK_NTQUOTA_HANDLE_OK()
2215 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->private_data;
2217 level = SVAL(params,2);
2219 /* unknown 12 bytes leading in params */
2222 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2223 /* seems that we should continue with the enum here --metze */
2225 if (qt_handle->quota_list!=NULL &&
2226 qt_handle->tmp_list==NULL) {
2229 free_ntquota_list(&(qt_handle->quota_list));
2231 /* Realloc the size of parameters and data we will return */
2233 params = nttrans_realloc(ppparams, param_len);
2234 if(params == NULL) {
2235 reply_doserror(req, ERRDOS, ERRnomem);
2240 SIVAL(params,0,data_len);
2247 case TRANSACT_GET_USER_QUOTA_LIST_START:
2249 if (qt_handle->quota_list==NULL &&
2250 qt_handle->tmp_list==NULL) {
2254 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2255 reply_doserror(req, ERRSRV, ERRerror);
2259 /* Realloc the size of parameters and data we will return */
2261 params = nttrans_realloc(ppparams, param_len);
2262 if(params == NULL) {
2263 reply_doserror(req, ERRDOS, ERRnomem);
2267 /* we should not trust the value in max_data_count*/
2268 max_data_count = MIN(max_data_count,2048);
2270 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2272 reply_doserror(req, ERRDOS, ERRnomem);
2278 /* set params Size of returned Quota Data 4 bytes*/
2279 /* but set it later when we know it */
2281 /* for each entry push the data */
2284 qt_handle->tmp_list = qt_handle->quota_list;
2287 tmp_list = qt_handle->tmp_list;
2289 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2290 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2292 sid_len = ndr_size_dom_sid(
2293 &tmp_list->quotas->sid, NULL, 0);
2294 entry_len = 40 + sid_len;
2296 /* nextoffset entry 4 bytes */
2297 SIVAL(entry,0,entry_len);
2299 /* then the len of the SID 4 bytes */
2300 SIVAL(entry,4,sid_len);
2302 /* unknown data 8 bytes uint64_t */
2303 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-metze*/
2305 /* the used disk space 8 bytes uint64_t */
2306 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2308 /* the soft quotas 8 bytes uint64_t */
2309 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2311 /* the hard quotas 8 bytes uint64_t */
2312 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2314 /* and now the SID */
2315 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2318 qt_handle->tmp_list = tmp_list;
2320 /* overwrite the offset of the last entry */
2321 SIVAL(entry-entry_len,0,0);
2323 data_len = 4+qt_len;
2324 /* overwrite the params quota_data_len */
2325 SIVAL(params,0,data_len);
2329 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2331 /* unknown 4 bytes IVAL(pdata,0) */
2333 if (data_count < 8) {
2334 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2335 reply_doserror(req, ERRDOS, ERRunknownlevel);
2339 sid_len = IVAL(pdata,4);
2340 /* Ensure this is less than 1mb. */
2341 if (sid_len > (1024*1024)) {
2342 reply_doserror(req, ERRDOS, ERRnomem);
2346 if (data_count < 8+sid_len) {
2347 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2348 reply_doserror(req, ERRDOS, ERRunknownlevel);
2352 data_len = 4+40+sid_len;
2354 if (max_data_count < data_len) {
2355 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2356 max_data_count, data_len));
2358 SIVAL(params,0,data_len);
2360 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2364 sid_parse(pdata+8,sid_len,&sid);
2366 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2369 * we have to return zero's in all fields
2370 * instead of returning an error here
2375 /* Realloc the size of parameters and data we will return */
2377 params = nttrans_realloc(ppparams, param_len);
2378 if(params == NULL) {
2379 reply_doserror(req, ERRDOS, ERRnomem);
2383 pdata = nttrans_realloc(ppdata, data_len);
2385 reply_doserror(req, ERRDOS, ERRnomem);
2391 /* set params Size of returned Quota Data 4 bytes*/
2392 SIVAL(params,0,data_len);
2394 /* nextoffset entry 4 bytes */
2397 /* then the len of the SID 4 bytes */
2398 SIVAL(entry,4,sid_len);
2400 /* unknown data 8 bytes uint64_t */
2401 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-mezte*/
2403 /* the used disk space 8 bytes uint64_t */
2404 SBIG_UINT(entry,16,qt.usedspace);
2406 /* the soft quotas 8 bytes uint64_t */
2407 SBIG_UINT(entry,24,qt.softlim);
2409 /* the hard quotas 8 bytes uint64_t */
2410 SBIG_UINT(entry,32,qt.hardlim);
2412 /* and now the SID */
2413 sid_linearize(entry+40, sid_len, &sid);
2418 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2419 reply_doserror(req, ERRSRV, ERRerror);
2424 send_nt_replies(conn, req, nt_status, params, param_len,
2428 /****************************************************************************
2429 Reply to set user quota
2430 ****************************************************************************/
2432 static void call_nt_transact_set_user_quota(connection_struct *conn,
2433 struct smb_request *req,
2437 uint32 parameter_count,
2440 uint32 max_data_count)
2442 char *params = *ppparams;
2443 char *pdata = *ppdata;
2444 int data_len=0,param_len=0;
2445 SMB_NTQUOTA_STRUCT qt;
2448 files_struct *fsp = NULL;
2453 if (conn->server_info->utok.uid != 0) {
2454 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2455 "[%s]\n", lp_servicename(SNUM(conn)),
2456 conn->server_info->unix_name));
2457 reply_doserror(req, ERRDOS, ERRnoaccess);
2462 * Ensure minimum number of parameters sent.
2465 if (parameter_count < 2) {
2466 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2467 reply_doserror(req, ERRDOS, ERRinvalidparam);
2471 /* maybe we can check the quota_fnum */
2472 fsp = file_fsp(req, SVAL(params,0));
2473 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2474 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2475 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2479 if (data_count < 40) {
2480 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2481 reply_doserror(req, ERRDOS, ERRunknownlevel);
2485 /* offset to next quota record.
2486 * 4 bytes IVAL(pdata,0)
2491 sid_len = IVAL(pdata,4);
2493 if (data_count < 40+sid_len) {
2494 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2495 reply_doserror(req, ERRDOS, ERRunknownlevel);
2499 /* unknown 8 bytes in pdata
2500 * maybe its the change time in NTTIME
2503 /* the used space 8 bytes (uint64_t)*/
2504 qt.usedspace = (uint64_t)IVAL(pdata,16);
2505 #ifdef LARGE_SMB_OFF_T
2506 qt.usedspace |= (((uint64_t)IVAL(pdata,20)) << 32);
2507 #else /* LARGE_SMB_OFF_T */
2508 if ((IVAL(pdata,20) != 0)&&
2509 ((qt.usedspace != 0xFFFFFFFF)||
2510 (IVAL(pdata,20)!=0xFFFFFFFF))) {
2511 /* more than 32 bits? */
2512 reply_doserror(req, ERRDOS, ERRunknownlevel);
2515 #endif /* LARGE_SMB_OFF_T */
2517 /* the soft quotas 8 bytes (uint64_t)*/
2518 qt.softlim = (uint64_t)IVAL(pdata,24);
2519 #ifdef LARGE_SMB_OFF_T
2520 qt.softlim |= (((uint64_t)IVAL(pdata,28)) << 32);
2521 #else /* LARGE_SMB_OFF_T */
2522 if ((IVAL(pdata,28) != 0)&&
2523 ((qt.softlim != 0xFFFFFFFF)||
2524 (IVAL(pdata,28)!=0xFFFFFFFF))) {
2525 /* more than 32 bits? */
2526 reply_doserror(req, ERRDOS, ERRunknownlevel);
2529 #endif /* LARGE_SMB_OFF_T */
2531 /* the hard quotas 8 bytes (uint64_t)*/
2532 qt.hardlim = (uint64_t)IVAL(pdata,32);
2533 #ifdef LARGE_SMB_OFF_T
2534 qt.hardlim |= (((uint64_t)IVAL(pdata,36)) << 32);
2535 #else /* LARGE_SMB_OFF_T */
2536 if ((IVAL(pdata,36) != 0)&&
2537 ((qt.hardlim != 0xFFFFFFFF)||
2538 (IVAL(pdata,36)!=0xFFFFFFFF))) {
2539 /* more than 32 bits? */
2540 reply_doserror(req, ERRDOS, ERRunknownlevel);
2543 #endif /* LARGE_SMB_OFF_T */
2545 sid_parse(pdata+40,sid_len,&sid);
2546 DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid)));
2548 /* 44 unknown bytes left... */
2550 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2551 reply_doserror(req, ERRSRV, ERRerror);
2555 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2558 #endif /* HAVE_SYS_QUOTAS */
2560 static void handle_nttrans(connection_struct *conn,
2561 struct trans_state *state,
2562 struct smb_request *req)
2564 if (Protocol >= PROTOCOL_NT1) {
2565 req->flags2 |= 0x40; /* IS_LONG_NAME */
2566 SSVAL(req->inbuf,smb_flg2,req->flags2);
2570 SMB_PERFCOUNT_SET_SUBOP(&req->pcd, state->call);
2572 /* Now we must call the relevant NT_TRANS function */
2573 switch(state->call) {
2574 case NT_TRANSACT_CREATE:
2576 START_PROFILE(NT_transact_create);
2577 call_nt_transact_create(
2579 &state->setup, state->setup_count,
2580 &state->param, state->total_param,
2581 &state->data, state->total_data,
2582 state->max_data_return);
2583 END_PROFILE(NT_transact_create);
2587 case NT_TRANSACT_IOCTL:
2589 START_PROFILE(NT_transact_ioctl);
2590 call_nt_transact_ioctl(
2592 &state->setup, state->setup_count,
2593 &state->param, state->total_param,
2594 &state->data, state->total_data,
2595 state->max_data_return);
2596 END_PROFILE(NT_transact_ioctl);
2600 case NT_TRANSACT_SET_SECURITY_DESC:
2602 START_PROFILE(NT_transact_set_security_desc);
2603 call_nt_transact_set_security_desc(
2605 &state->setup, state->setup_count,
2606 &state->param, state->total_param,
2607 &state->data, state->total_data,
2608 state->max_data_return);
2609 END_PROFILE(NT_transact_set_security_desc);
2613 case NT_TRANSACT_NOTIFY_CHANGE:
2615 START_PROFILE(NT_transact_notify_change);
2616 call_nt_transact_notify_change(
2618 &state->setup, state->setup_count,
2619 &state->param, state->total_param,
2620 &state->data, state->total_data,
2621 state->max_data_return,
2622 state->max_param_return);
2623 END_PROFILE(NT_transact_notify_change);
2627 case NT_TRANSACT_RENAME:
2629 START_PROFILE(NT_transact_rename);
2630 call_nt_transact_rename(
2632 &state->setup, state->setup_count,
2633 &state->param, state->total_param,
2634 &state->data, state->total_data,
2635 state->max_data_return);
2636 END_PROFILE(NT_transact_rename);
2640 case NT_TRANSACT_QUERY_SECURITY_DESC:
2642 START_PROFILE(NT_transact_query_security_desc);
2643 call_nt_transact_query_security_desc(
2645 &state->setup, state->setup_count,
2646 &state->param, state->total_param,
2647 &state->data, state->total_data,
2648 state->max_data_return);
2649 END_PROFILE(NT_transact_query_security_desc);
2653 #ifdef HAVE_SYS_QUOTAS
2654 case NT_TRANSACT_GET_USER_QUOTA:
2656 START_PROFILE(NT_transact_get_user_quota);
2657 call_nt_transact_get_user_quota(
2659 &state->setup, state->setup_count,
2660 &state->param, state->total_param,
2661 &state->data, state->total_data,
2662 state->max_data_return);
2663 END_PROFILE(NT_transact_get_user_quota);
2667 case NT_TRANSACT_SET_USER_QUOTA:
2669 START_PROFILE(NT_transact_set_user_quota);
2670 call_nt_transact_set_user_quota(
2672 &state->setup, state->setup_count,
2673 &state->param, state->total_param,
2674 &state->data, state->total_data,
2675 state->max_data_return);
2676 END_PROFILE(NT_transact_set_user_quota);
2679 #endif /* HAVE_SYS_QUOTAS */
2682 /* Error in request */
2683 DEBUG(0,("handle_nttrans: Unknown request %d in "
2684 "nttrans call\n", state->call));
2685 reply_doserror(req, ERRSRV, ERRerror);
2691 /****************************************************************************
2692 Reply to a SMBNTtrans.
2693 ****************************************************************************/
2695 void reply_nttrans(struct smb_request *req)
2697 connection_struct *conn = req->conn;
2702 uint16 function_code;
2704 struct trans_state *state;
2706 START_PROFILE(SMBnttrans);
2708 if (req->wct < 19) {
2709 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2710 END_PROFILE(SMBnttrans);
2714 pscnt = IVAL(req->vwv+9, 1);
2715 psoff = IVAL(req->vwv+11, 1);
2716 dscnt = IVAL(req->vwv+13, 1);
2717 dsoff = IVAL(req->vwv+15, 1);
2718 function_code = SVAL(req->vwv+18, 0);
2720 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2721 reply_doserror(req, ERRSRV, ERRaccess);
2722 END_PROFILE(SMBnttrans);
2726 result = allow_new_trans(conn->pending_trans, req->mid);
2727 if (!NT_STATUS_IS_OK(result)) {
2728 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
2729 reply_nterror(req, result);
2730 END_PROFILE(SMBnttrans);
2734 if ((state = TALLOC_P(conn, struct trans_state)) == NULL) {
2735 reply_doserror(req, ERRSRV, ERRaccess);
2736 END_PROFILE(SMBnttrans);
2740 state->cmd = SMBnttrans;
2742 state->mid = req->mid;
2743 state->vuid = req->vuid;
2744 state->total_data = IVAL(req->vwv+3, 1);
2746 state->total_param = IVAL(req->vwv+1, 1);
2747 state->param = NULL;
2748 state->max_data_return = IVAL(req->vwv+7, 1);
2749 state->max_param_return = IVAL(req->vwv+5, 1);
2751 /* setup count is in *words* */
2752 state->setup_count = 2*CVAL(req->vwv+17, 1);
2753 state->setup = NULL;
2754 state->call = function_code;
2756 DEBUG(10, ("num_setup=%u, "
2757 "param_total=%u, this_param=%u, max_param=%u, "
2758 "data_total=%u, this_data=%u, max_data=%u, "
2759 "param_offset=%u, data_offset=%u\n",
2760 (unsigned)state->setup_count,
2761 (unsigned)state->total_param, (unsigned)pscnt,
2762 (unsigned)state->max_param_return,
2763 (unsigned)state->total_data, (unsigned)dscnt,
2764 (unsigned)state->max_data_return,
2765 (unsigned)psoff, (unsigned)dsoff));
2768 * All nttrans messages we handle have smb_wct == 19 +
2769 * state->setup_count. Ensure this is so as a sanity check.
2772 if(req->wct != 19 + (state->setup_count/2)) {
2773 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2774 req->wct, 19 + (state->setup_count/2)));
2778 /* Don't allow more than 128mb for each value. */
2779 if ((state->total_data > (1024*1024*128)) ||
2780 (state->total_param > (1024*1024*128))) {
2781 reply_doserror(req, ERRDOS, ERRnomem);
2782 END_PROFILE(SMBnttrans);
2786 if ((dscnt > state->total_data) || (pscnt > state->total_param))
2789 if (state->total_data) {
2791 if (trans_oob(state->total_data, 0, dscnt)
2792 || trans_oob(smb_len(req->inbuf), dsoff, dscnt)) {
2796 /* Can't use talloc here, the core routines do realloc on the
2797 * params and data. */
2798 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
2799 DEBUG(0,("reply_nttrans: data malloc fail for %u "
2800 "bytes !\n", (unsigned int)state->total_data));
2802 reply_doserror(req, ERRDOS, ERRnomem);
2803 END_PROFILE(SMBnttrans);
2807 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
2810 if (state->total_param) {
2812 if (trans_oob(state->total_param, 0, pscnt)
2813 || trans_oob(smb_len(req->inbuf), psoff, pscnt)) {
2817 /* Can't use talloc here, the core routines do realloc on the
2818 * params and data. */
2819 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
2820 DEBUG(0,("reply_nttrans: param malloc fail for %u "
2821 "bytes !\n", (unsigned int)state->total_param));
2822 SAFE_FREE(state->data);
2824 reply_doserror(req, ERRDOS, ERRnomem);
2825 END_PROFILE(SMBnttrans);
2829 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
2832 state->received_data = dscnt;
2833 state->received_param = pscnt;
2835 if(state->setup_count > 0) {
2836 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2837 state->setup_count));
2840 * No overflow possible here, state->setup_count is an
2841 * unsigned int, being filled by a single byte from
2842 * CVAL(req->vwv+13, 0) above. The cast in the comparison
2843 * below is not necessary, it's here to clarify things. The
2844 * validity of req->vwv and req->wct has been checked in
2845 * init_smb_request already.
2847 if ((state->setup_count/2) + 19 > (unsigned int)req->wct) {
2851 state->setup = (uint16 *)TALLOC(state, state->setup_count);
2852 if (state->setup == NULL) {
2853 DEBUG(0,("reply_nttrans : Out of memory\n"));
2854 SAFE_FREE(state->data);
2855 SAFE_FREE(state->param);
2857 reply_doserror(req, ERRDOS, ERRnomem);
2858 END_PROFILE(SMBnttrans);
2862 memcpy(state->setup, req->vwv+19, state->setup_count);
2863 dump_data(10, (uint8 *)state->setup, state->setup_count);
2866 if ((state->received_data == state->total_data) &&
2867 (state->received_param == state->total_param)) {
2868 handle_nttrans(conn, state, req);
2869 SAFE_FREE(state->param);
2870 SAFE_FREE(state->data);
2872 END_PROFILE(SMBnttrans);
2876 DLIST_ADD(conn->pending_trans, state);
2878 /* We need to send an interim response then receive the rest
2879 of the parameter/data bytes */
2880 reply_outbuf(req, 0, 0);
2881 show_msg((char *)req->outbuf);
2882 END_PROFILE(SMBnttrans);
2887 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
2888 SAFE_FREE(state->data);
2889 SAFE_FREE(state->param);
2891 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2892 END_PROFILE(SMBnttrans);
2896 /****************************************************************************
2897 Reply to a SMBnttranss
2898 ****************************************************************************/
2900 void reply_nttranss(struct smb_request *req)
2902 connection_struct *conn = req->conn;
2903 uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
2904 struct trans_state *state;
2906 START_PROFILE(SMBnttranss);
2908 show_msg((char *)req->inbuf);
2910 if (req->wct < 18) {
2911 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2912 END_PROFILE(SMBnttranss);
2916 for (state = conn->pending_trans; state != NULL;
2917 state = state->next) {
2918 if (state->mid == req->mid) {
2923 if ((state == NULL) || (state->cmd != SMBnttrans)) {
2924 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2925 END_PROFILE(SMBnttranss);
2929 /* Revise state->total_param and state->total_data in case they have
2930 changed downwards */
2931 if (IVAL(req->vwv+1, 1) < state->total_param) {
2932 state->total_param = IVAL(req->vwv+1, 1);
2934 if (IVAL(req->vwv+3, 1) < state->total_data) {
2935 state->total_data = IVAL(req->vwv+3, 1);
2938 pcnt = IVAL(req->vwv+5, 1);
2939 poff = IVAL(req->vwv+7, 1);
2940 pdisp = IVAL(req->vwv+9, 1);
2942 dcnt = IVAL(req->vwv+11, 1);
2943 doff = IVAL(req->vwv+13, 1);
2944 ddisp = IVAL(req->vwv+15, 1);
2946 state->received_param += pcnt;
2947 state->received_data += dcnt;
2949 if ((state->received_data > state->total_data) ||
2950 (state->received_param > state->total_param))
2954 if (trans_oob(state->total_param, pdisp, pcnt)
2955 || trans_oob(smb_len(req->inbuf), poff, pcnt)) {
2958 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,pcnt);
2962 if (trans_oob(state->total_data, ddisp, dcnt)
2963 || trans_oob(smb_len(req->inbuf), doff, dcnt)) {
2966 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
2969 if ((state->received_param < state->total_param) ||
2970 (state->received_data < state->total_data)) {
2971 END_PROFILE(SMBnttranss);
2975 handle_nttrans(conn, state, req);
2977 DLIST_REMOVE(conn->pending_trans, state);
2978 SAFE_FREE(state->data);
2979 SAFE_FREE(state->param);
2981 END_PROFILE(SMBnttranss);
2986 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
2987 DLIST_REMOVE(conn->pending_trans, state);
2988 SAFE_FREE(state->data);
2989 SAFE_FREE(state->param);
2991 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2992 END_PROFILE(SMBnttranss);