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/>.
24 extern enum protocol_types Protocol;
25 extern struct current_user current_user;
27 static const char *known_nt_pipes[] = {
48 static char *nttrans_realloc(char **ptr, size_t size)
51 smb_panic("nttrans_realloc() called with NULL ptr");
54 *ptr = (char *)SMB_REALLOC(*ptr, size);
58 memset(*ptr,'\0',size);
62 /****************************************************************************
63 Send the required number of replies back.
64 We assume all fields other than the data fields are
65 set correctly for the type of call.
66 HACK ! Always assumes smb_setup field is zero.
67 ****************************************************************************/
69 void send_nt_replies(connection_struct *conn,
70 struct smb_request *req, NTSTATUS nt_error,
71 char *params, int paramsize,
72 char *pdata, int datasize)
74 int data_to_send = datasize;
75 int params_to_send = paramsize;
79 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
80 int alignment_offset = 3;
81 int data_alignment_offset = 0;
84 * If there genuinely are no parameters or data to send just send
88 if(params_to_send == 0 && data_to_send == 0) {
89 reply_outbuf(req, 18, 0);
90 show_msg((char *)req->outbuf);
95 * When sending params and data ensure that both are nicely aligned.
96 * Only do this alignment when there is also data to send - else
97 * can cause NT redirector problems.
100 if (((params_to_send % 4) != 0) && (data_to_send != 0)) {
101 data_alignment_offset = 4 - (params_to_send % 4);
105 * Space is bufsize minus Netbios over TCP header minus SMB header.
106 * The alignment_offset is to align the param bytes on a four byte
107 * boundary (2 bytes for data len, one byte pad).
108 * NT needs this to work correctly.
111 useable_space = max_send - (smb_size
114 + data_alignment_offset);
117 * useable_space can never be more than max_send minus the
121 useable_space = MIN(useable_space,
122 max_send - (alignment_offset+data_alignment_offset));
125 while (params_to_send || data_to_send) {
128 * Calculate whether we will totally or partially fill this packet.
131 total_sent_thistime = params_to_send + data_to_send +
132 alignment_offset + data_alignment_offset;
135 * We can never send more than useable_space.
138 total_sent_thistime = MIN(total_sent_thistime, useable_space);
140 reply_outbuf(req, 18, total_sent_thistime);
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(),
248 IS_CONN_ENCRYPTED(conn))) {
249 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
252 TALLOC_FREE(req->outbuf);
254 pp += params_sent_thistime;
255 pd += data_sent_thistime;
257 params_to_send -= params_sent_thistime;
258 data_to_send -= data_sent_thistime;
264 if(params_to_send < 0 || data_to_send < 0) {
265 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
266 params_to_send, data_to_send));
272 /****************************************************************************
273 Is it an NTFS stream name ?
274 ****************************************************************************/
276 bool is_ntfs_stream_name(const char *fname)
278 if (lp_posix_pathnames()) {
281 return (strchr_m(fname, ':') != NULL) ? True : False;
284 /****************************************************************************
285 Reply to an NT create and X call on a pipe
286 ****************************************************************************/
288 static void nt_open_pipe(char *fname, connection_struct *conn,
289 struct smb_request *req, int *ppnum)
291 smb_np_struct *p = NULL;
294 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
296 /* See if it is one we want to handle. */
298 if (lp_disable_spoolss() && strequal(fname, "\\spoolss")) {
299 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
304 for( i = 0; known_nt_pipes[i]; i++ ) {
305 if( strequal(fname,known_nt_pipes[i])) {
310 if ( known_nt_pipes[i] == NULL ) {
311 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
316 /* Strip \\ off the name. */
319 DEBUG(3,("nt_open_pipe: Known pipe %s opening.\n", fname));
321 p = open_rpc_pipe_p(fname, conn, req->vuid);
323 reply_doserror(req, ERRSRV, ERRnofids);
327 /* TODO: Add pipe to db */
329 if ( !store_pipe_opendb( p ) ) {
330 DEBUG(3,("nt_open_pipe: failed to store %s pipe open.\n", fname));
337 /****************************************************************************
338 Reply to an NT create and X call for pipes.
339 ****************************************************************************/
341 static void do_ntcreate_pipe_open(connection_struct *conn,
342 struct smb_request *req)
347 uint32 flags = IVAL(req->inbuf,smb_ntcreate_Flags);
348 TALLOC_CTX *ctx = talloc_tos();
350 srvstr_pull_buf_talloc(ctx, (char *)req->inbuf, req->flags2, &fname,
351 smb_buf(req->inbuf), STR_TERMINATE);
354 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
358 nt_open_pipe(fname, conn, req, &pnum);
366 * Deal with pipe return.
369 if (flags & EXTENDED_RESPONSE_REQUIRED) {
370 /* This is very strange. We
371 * return 50 words, but only set
372 * the wcnt to 42 ? It's definately
373 * what happens on the wire....
375 reply_outbuf(req, 50, 0);
376 SCVAL(req->outbuf,smb_wct,42);
378 reply_outbuf(req, 34, 0);
381 p = (char *)req->outbuf + smb_vwv2;
385 SIVAL(p,0,FILE_WAS_OPENED);
388 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
391 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
393 SSVAL(p,2, 0x5FF); /* ? */
396 if (flags & EXTENDED_RESPONSE_REQUIRED) {
398 SIVAL(p,0,FILE_GENERIC_ALL);
400 * For pipes W2K3 seems to return
402 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
404 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
407 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
412 /****************************************************************************
413 Reply to an NT create and X call.
414 ****************************************************************************/
416 void reply_ntcreate_and_X(connection_struct *conn, struct smb_request *req)
421 uint32 file_attributes;
423 uint32 create_disposition;
424 uint32 create_options;
426 SMB_BIG_UINT allocation_size;
427 /* Breakout the oplock request bits so we can set the
428 reply bits separately. */
430 SMB_OFF_T file_len = 0;
431 SMB_STRUCT_STAT sbuf;
433 files_struct *fsp = NULL;
435 struct timespec c_timespec;
436 struct timespec a_timespec;
437 struct timespec m_timespec;
440 uint8_t oplock_granted = NO_OPLOCK_RETURN;
441 TALLOC_CTX *ctx = talloc_tos();
443 START_PROFILE(SMBntcreateX);
446 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
450 flags = IVAL(req->inbuf,smb_ntcreate_Flags);
451 access_mask = IVAL(req->inbuf,smb_ntcreate_DesiredAccess);
452 file_attributes = IVAL(req->inbuf,smb_ntcreate_FileAttributes);
453 share_access = IVAL(req->inbuf,smb_ntcreate_ShareAccess);
454 create_disposition = IVAL(req->inbuf,smb_ntcreate_CreateDisposition);
455 create_options = IVAL(req->inbuf,smb_ntcreate_CreateOptions);
456 root_dir_fid = (uint16)IVAL(req->inbuf,smb_ntcreate_RootDirectoryFid);
458 allocation_size = (SMB_BIG_UINT)IVAL(req->inbuf,
459 smb_ntcreate_AllocationSize);
460 #ifdef LARGE_SMB_OFF_T
461 allocation_size |= (((SMB_BIG_UINT)IVAL(
463 smb_ntcreate_AllocationSize + 4)) << 32);
466 srvstr_get_path(ctx, (char *)req->inbuf, req->flags2, &fname,
467 smb_buf(req->inbuf), 0, STR_TERMINATE, &status);
469 if (!NT_STATUS_IS_OK(status)) {
470 reply_nterror(req, status);
471 END_PROFILE(SMBntcreateX);
475 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
476 "file_attributes = 0x%x, share_access = 0x%x, "
477 "create_disposition = 0x%x create_options = 0x%x "
478 "root_dir_fid = 0x%x, fname = %s\n",
480 (unsigned int)access_mask,
481 (unsigned int)file_attributes,
482 (unsigned int)share_access,
483 (unsigned int)create_disposition,
484 (unsigned int)create_options,
485 (unsigned int)root_dir_fid,
489 * If it's an IPC, use the pipe handler.
493 if (lp_nt_pipe_support()) {
494 do_ntcreate_pipe_open(conn, req);
495 END_PROFILE(SMBntcreateX);
498 reply_doserror(req, ERRDOS, ERRnoaccess);
499 END_PROFILE(SMBntcreateX);
504 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
505 if (oplock_request) {
506 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
510 status = create_file(conn, req, root_dir_fid, fname,
511 access_mask, share_access, create_disposition,
512 create_options, file_attributes, oplock_request,
513 allocation_size, NULL, NULL, &fsp, &info, &sbuf);
515 if (!NT_STATUS_IS_OK(status)) {
516 if (open_was_deferred(req->mid)) {
517 /* We have re-scheduled this call, no error. */
518 END_PROFILE(SMBntcreateX);
521 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
522 reply_botherror(req, status, ERRDOS, ERRfilexists);
525 reply_nterror(req, status);
527 END_PROFILE(SMBntcreateX);
532 * If the caller set the extended oplock request bit
533 * and we granted one (by whatever means) - set the
534 * correct bit for extended oplock reply.
537 if (oplock_request &&
538 (lp_fake_oplocks(SNUM(conn))
539 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
542 * Exclusive oplock granted
545 if (flags & REQUEST_BATCH_OPLOCK) {
546 oplock_granted = BATCH_OPLOCK_RETURN;
548 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
550 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
551 oplock_granted = LEVEL_II_OPLOCK_RETURN;
553 oplock_granted = NO_OPLOCK_RETURN;
556 file_len = sbuf.st_size;
557 fattr = dos_mode(conn,fname,&sbuf);
559 fattr = FILE_ATTRIBUTE_NORMAL;
562 if (flags & EXTENDED_RESPONSE_REQUIRED) {
563 /* This is very strange. We
564 * return 50 words, but only set
565 * the wcnt to 42 ? It's definately
566 * what happens on the wire....
568 reply_outbuf(req, 50, 0);
569 SCVAL(req->outbuf,smb_wct,42);
571 reply_outbuf(req, 34, 0);
574 p = (char *)req->outbuf + smb_vwv2;
576 SCVAL(p, 0, oplock_granted);
579 SSVAL(p,0,fsp->fnum);
581 if ((create_disposition == FILE_SUPERSEDE)
582 && (info == FILE_WAS_OVERWRITTEN)) {
583 SIVAL(p,0,FILE_WAS_SUPERSEDED);
590 c_timespec = get_create_timespec(
591 &sbuf,lp_fake_dir_create_times(SNUM(conn)));
592 a_timespec = get_atimespec(&sbuf);
593 m_timespec = get_mtimespec(&sbuf);
595 if (lp_dos_filetime_resolution(SNUM(conn))) {
596 dos_filetime_timespec(&c_timespec);
597 dos_filetime_timespec(&a_timespec);
598 dos_filetime_timespec(&m_timespec);
601 put_long_date_timespec(p, c_timespec); /* create time. */
603 put_long_date_timespec(p, a_timespec); /* access time */
605 put_long_date_timespec(p, m_timespec); /* write time */
607 put_long_date_timespec(p, m_timespec); /* change time */
609 SIVAL(p,0,fattr); /* File Attributes. */
611 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
613 SOFF_T(p,0,file_len);
615 if (flags & EXTENDED_RESPONSE_REQUIRED) {
619 SCVAL(p,0,fsp->is_directory ? 1 : 0);
621 if (flags & EXTENDED_RESPONSE_REQUIRED) {
624 if (fsp->is_directory
625 || can_write_to_file(conn, fname, &sbuf)) {
626 perms = FILE_GENERIC_ALL;
628 perms = FILE_GENERIC_READ|FILE_EXECUTE;
633 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n",
634 fsp->fnum, fsp->fsp_name));
637 END_PROFILE(SMBntcreateX);
641 /****************************************************************************
642 Reply to a NT_TRANSACT_CREATE call to open a pipe.
643 ****************************************************************************/
645 static void do_nt_transact_create_pipe(connection_struct *conn,
646 struct smb_request *req,
647 uint16 **ppsetup, uint32 setup_count,
648 char **ppparams, uint32 parameter_count,
649 char **ppdata, uint32 data_count)
652 char *params = *ppparams;
658 TALLOC_CTX *ctx = talloc_tos();
661 * Ensure minimum number of parameters sent.
664 if(parameter_count < 54) {
665 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
666 reply_doserror(req, ERRDOS, ERRnoaccess);
670 flags = IVAL(params,0);
672 srvstr_get_path(ctx, params, req->flags2, &fname, params+53,
673 parameter_count-53, STR_TERMINATE,
675 if (!NT_STATUS_IS_OK(status)) {
676 reply_nterror(req, status);
680 nt_open_pipe(fname, conn, req, &pnum);
687 /* Realloc the size of parameters and data we will return */
688 if (flags & EXTENDED_RESPONSE_REQUIRED) {
689 /* Extended response is 32 more byyes. */
694 params = nttrans_realloc(ppparams, param_len);
696 reply_doserror(req, ERRDOS, ERRnomem);
701 SCVAL(p,0,NO_OPLOCK_RETURN);
706 SIVAL(p,0,FILE_WAS_OPENED);
710 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
713 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
715 SSVAL(p,2, 0x5FF); /* ? */
718 if (flags & EXTENDED_RESPONSE_REQUIRED) {
720 SIVAL(p,0,FILE_GENERIC_ALL);
722 * For pipes W2K3 seems to return
724 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
726 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
729 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
731 /* Send the required number of replies */
732 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
737 /****************************************************************************
738 Internal fn to set security descriptors.
739 ****************************************************************************/
741 static NTSTATUS set_sd(files_struct *fsp, uint8 *data, uint32 sd_len,
742 uint32 security_info_sent)
744 SEC_DESC *psd = NULL;
747 if (sd_len == 0 || !lp_nt_acl_support(SNUM(fsp->conn))) {
751 status = unmarshall_sec_desc(talloc_tos(), data, sd_len, &psd);
753 if (!NT_STATUS_IS_OK(status)) {
757 if (psd->owner_sid==0) {
758 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
760 if (psd->group_sid==0) {
761 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
764 security_info_sent &= ~SACL_SECURITY_INFORMATION;
767 security_info_sent &= ~DACL_SECURITY_INFORMATION;
770 if (fsp->fh->fd != -1) {
771 status = SMB_VFS_FSET_NT_ACL(fsp, fsp->fh->fd,
772 security_info_sent, psd);
775 status = SMB_VFS_SET_NT_ACL(fsp, fsp->fsp_name,
776 security_info_sent, psd);
784 /****************************************************************************
785 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
786 ****************************************************************************/
788 static struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
790 struct ea_list *ea_list_head = NULL;
797 while (offset + 4 <= data_size) {
798 size_t next_offset = IVAL(pdata,offset);
799 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
805 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
806 if (next_offset == 0) {
809 offset += next_offset;
815 /****************************************************************************
816 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
817 ****************************************************************************/
819 static void call_nt_transact_create(connection_struct *conn,
820 struct smb_request *req,
821 uint16 **ppsetup, uint32 setup_count,
822 char **ppparams, uint32 parameter_count,
823 char **ppdata, uint32 data_count,
824 uint32 max_data_count)
827 char *params = *ppparams;
828 char *data = *ppdata;
829 /* Breakout the oplock request bits so we can set the reply bits separately. */
831 SMB_OFF_T file_len = 0;
832 SMB_STRUCT_STAT sbuf;
834 files_struct *fsp = NULL;
838 uint32 file_attributes;
840 uint32 create_disposition;
841 uint32 create_options;
843 struct security_descriptor *sd = NULL;
846 struct timespec c_timespec;
847 struct timespec a_timespec;
848 struct timespec m_timespec;
849 struct ea_list *ea_list = NULL;
852 SMB_BIG_UINT allocation_size;
854 uint8_t oplock_granted;
855 TALLOC_CTX *ctx = talloc_tos();
857 DEBUG(5,("call_nt_transact_create\n"));
860 * If it's an IPC, use the pipe handler.
864 if (lp_nt_pipe_support()) {
865 do_nt_transact_create_pipe(
867 ppsetup, setup_count,
868 ppparams, parameter_count,
872 reply_doserror(req, ERRDOS, ERRnoaccess);
878 * Ensure minimum number of parameters sent.
881 if(parameter_count < 54) {
882 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
883 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
887 flags = IVAL(params,0);
888 access_mask = IVAL(params,8);
889 file_attributes = IVAL(params,20);
890 share_access = IVAL(params,24);
891 create_disposition = IVAL(params,28);
892 create_options = IVAL(params,32);
893 sd_len = IVAL(params,36);
894 ea_len = IVAL(params,40);
895 root_dir_fid = (uint16)IVAL(params,4);
896 allocation_size = (SMB_BIG_UINT)IVAL(params,12);
897 #ifdef LARGE_SMB_OFF_T
898 allocation_size |= (((SMB_BIG_UINT)IVAL(params,16)) << 32);
901 /* Ensure the data_len is correct for the sd and ea values given. */
902 if ((ea_len + sd_len > data_count)
903 || (ea_len > data_count) || (sd_len > data_count)
904 || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
905 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
906 "%u, data_count = %u\n", (unsigned int)ea_len,
907 (unsigned int)sd_len, (unsigned int)data_count));
908 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
913 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
916 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
918 if (!NT_STATUS_IS_OK(status)) {
919 DEBUG(10, ("call_nt_transact_create: "
920 "unmarshall_sec_desc failed: %s\n",
922 reply_nterror(req, status);
928 if (!lp_ea_support(SNUM(conn))) {
929 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
930 "EA's not supported.\n",
931 (unsigned int)ea_len));
932 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
937 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
938 "too small (should be more than 10)\n",
939 (unsigned int)ea_len ));
940 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
944 /* We have already checked that ea_len <= data_count here. */
945 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
947 if (ea_list == NULL) {
948 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
953 srvstr_get_path(ctx, params, req->flags2, &fname,
954 params+53, parameter_count-53,
955 STR_TERMINATE, &status);
956 if (!NT_STATUS_IS_OK(status)) {
957 reply_nterror(req, status);
961 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
962 if (oplock_request) {
963 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
967 status = create_file(conn, req, root_dir_fid, fname,
968 access_mask, share_access, create_disposition,
969 create_options, file_attributes, oplock_request,
970 allocation_size, sd, ea_list, &fsp, &info, &sbuf);
972 if(!NT_STATUS_IS_OK(status)) {
973 if (open_was_deferred(req->mid)) {
974 /* We have re-scheduled this call, no error. */
977 reply_openerror(req, status);
982 * If the caller set the extended oplock request bit
983 * and we granted one (by whatever means) - set the
984 * correct bit for extended oplock reply.
987 if (oplock_request &&
988 (lp_fake_oplocks(SNUM(conn))
989 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
992 * Exclusive oplock granted
995 if (flags & REQUEST_BATCH_OPLOCK) {
996 oplock_granted = BATCH_OPLOCK_RETURN;
998 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
1000 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1001 oplock_granted = LEVEL_II_OPLOCK_RETURN;
1003 oplock_granted = NO_OPLOCK_RETURN;
1006 file_len = sbuf.st_size;
1007 fattr = dos_mode(conn,fname,&sbuf);
1009 fattr = FILE_ATTRIBUTE_NORMAL;
1012 /* Realloc the size of parameters and data we will return */
1013 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1014 /* Extended response is 32 more byyes. */
1019 params = nttrans_realloc(ppparams, param_len);
1020 if(params == NULL) {
1021 reply_doserror(req, ERRDOS, ERRnomem);
1026 SCVAL(p, 0, oplock_granted);
1029 SSVAL(p,0,fsp->fnum);
1031 if ((create_disposition == FILE_SUPERSEDE)
1032 && (info == FILE_WAS_OVERWRITTEN)) {
1033 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1040 c_timespec = get_create_timespec(
1041 &sbuf,lp_fake_dir_create_times(SNUM(conn)));
1042 a_timespec = get_atimespec(&sbuf);
1043 m_timespec = get_mtimespec(&sbuf);
1045 if (lp_dos_filetime_resolution(SNUM(conn))) {
1046 dos_filetime_timespec(&c_timespec);
1047 dos_filetime_timespec(&a_timespec);
1048 dos_filetime_timespec(&m_timespec);
1051 put_long_date_timespec(p, c_timespec); /* create time. */
1053 put_long_date_timespec(p, a_timespec); /* access time */
1055 put_long_date_timespec(p, m_timespec); /* write time */
1057 put_long_date_timespec(p, m_timespec); /* change time */
1059 SIVAL(p,0,fattr); /* File Attributes. */
1061 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
1063 SOFF_T(p,0,file_len);
1065 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1069 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1071 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1074 if (fsp->is_directory
1075 || can_write_to_file(conn, fname, &sbuf)) {
1076 perms = FILE_GENERIC_ALL;
1078 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1083 DEBUG(5,("call_nt_transact_create: open name = %s\n", fname));
1085 /* Send the required number of replies */
1086 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1091 /****************************************************************************
1092 Reply to a NT CANCEL request.
1093 conn POINTER CAN BE NULL HERE !
1094 ****************************************************************************/
1096 void reply_ntcancel(connection_struct *conn, struct smb_request *req)
1099 * Go through and cancel any pending change notifies.
1102 START_PROFILE(SMBntcancel);
1103 remove_pending_change_notify_requests_by_mid(req->mid);
1104 remove_pending_lock_requests_by_mid(req->mid);
1105 srv_cancel_sign_response(req->mid);
1107 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", req->mid));
1109 END_PROFILE(SMBntcancel);
1113 /****************************************************************************
1115 ****************************************************************************/
1117 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
1118 connection_struct *conn,
1119 struct smb_request *req,
1120 const char *oldname_in,
1121 const char *newname_in,
1124 SMB_STRUCT_STAT sbuf1, sbuf2;
1125 char *oldname = NULL;
1126 char *newname = NULL;
1127 char *last_component_oldname = NULL;
1128 char *last_component_newname = NULL;
1129 files_struct *fsp1,*fsp2;
1133 NTSTATUS status = NT_STATUS_OK;
1138 if (!CAN_WRITE(conn)) {
1139 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1142 status = unix_convert(ctx, conn, oldname_in, False, &oldname,
1143 &last_component_oldname, &sbuf1);
1144 if (!NT_STATUS_IS_OK(status)) {
1148 status = check_name(conn, oldname);
1149 if (!NT_STATUS_IS_OK(status)) {
1153 /* Source must already exist. */
1154 if (!VALID_STAT(sbuf1)) {
1155 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1157 /* Ensure attributes match. */
1158 fattr = dos_mode(conn,oldname,&sbuf1);
1159 if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
1160 return NT_STATUS_NO_SUCH_FILE;
1163 status = unix_convert(ctx, conn, newname_in, False, &newname,
1164 &last_component_newname, &sbuf2);
1165 if (!NT_STATUS_IS_OK(status)) {
1169 status = check_name(conn, newname);
1170 if (!NT_STATUS_IS_OK(status)) {
1174 /* Disallow if newname already exists. */
1175 if (VALID_STAT(sbuf2)) {
1176 return NT_STATUS_OBJECT_NAME_COLLISION;
1179 /* No links from a directory. */
1180 if (S_ISDIR(sbuf1.st_mode)) {
1181 return NT_STATUS_FILE_IS_A_DIRECTORY;
1184 /* Ensure this is within the share. */
1185 status = check_reduced_name(conn, oldname);
1186 if (!NT_STATUS_IS_OK(status)) {
1190 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1193 status = open_file_ntcreate(conn, req, oldname, &sbuf1,
1194 FILE_READ_DATA, /* Read-only. */
1195 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1197 0, /* No create options. */
1198 FILE_ATTRIBUTE_NORMAL,
1202 if (!NT_STATUS_IS_OK(status)) {
1206 status = open_file_ntcreate(conn, req, newname, &sbuf2,
1207 FILE_WRITE_DATA, /* Read-only. */
1208 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1210 0, /* No create options. */
1215 if (!NT_STATUS_IS_OK(status)) {
1216 close_file(fsp1,ERROR_CLOSE);
1220 if (sbuf1.st_size) {
1221 ret = vfs_transfer_file(fsp1, fsp2, sbuf1.st_size);
1225 * As we are opening fsp1 read-only we only expect
1226 * an error on close on fsp2 if we are out of space.
1227 * Thus we don't look at the error return from the
1230 close_file(fsp1,NORMAL_CLOSE);
1232 /* Ensure the modtime is set correctly on the destination file. */
1233 fsp_set_pending_modtime(fsp2, get_mtimespec(&sbuf1));
1235 status = close_file(fsp2,NORMAL_CLOSE);
1237 /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
1238 creates the file. This isn't the correct thing to do in the copy
1240 file_set_dosmode(conn, newname, fattr, &sbuf2,
1241 parent_dirname(newname),false);
1243 if (ret < (SMB_OFF_T)sbuf1.st_size) {
1244 return NT_STATUS_DISK_FULL;
1247 if (!NT_STATUS_IS_OK(status)) {
1248 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1249 nt_errstr(status), oldname, newname));
1254 /****************************************************************************
1255 Reply to a NT rename request.
1256 ****************************************************************************/
1258 void reply_ntrename(connection_struct *conn, struct smb_request *req)
1260 char *oldname = NULL;
1261 char *newname = NULL;
1264 bool src_has_wcard = False;
1265 bool dest_has_wcard = False;
1268 TALLOC_CTX *ctx = talloc_tos();
1270 START_PROFILE(SMBntrename);
1273 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1274 END_PROFILE(SMBntrename);
1278 attrs = SVAL(req->inbuf,smb_vwv0);
1279 rename_type = SVAL(req->inbuf,smb_vwv1);
1281 p = smb_buf(req->inbuf) + 1;
1282 p += srvstr_get_path_wcard(ctx, (char *)req->inbuf, req->flags2, &oldname, p,
1283 0, STR_TERMINATE, &status,
1285 if (!NT_STATUS_IS_OK(status)) {
1286 reply_nterror(req, status);
1287 END_PROFILE(SMBntrename);
1291 if( is_ntfs_stream_name(oldname)) {
1292 /* Can't rename a stream. */
1293 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1294 END_PROFILE(SMBntrename);
1298 if (ms_has_wild(oldname)) {
1299 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1300 END_PROFILE(SMBntrename);
1305 p += srvstr_get_path_wcard(ctx, (char *)req->inbuf, req->flags2, &newname, p,
1306 0, STR_TERMINATE, &status,
1308 if (!NT_STATUS_IS_OK(status)) {
1309 reply_nterror(req, status);
1310 END_PROFILE(SMBntrename);
1314 status = resolve_dfspath(ctx, conn,
1315 req->flags2 & FLAGS2_DFS_PATHNAMES,
1318 if (!NT_STATUS_IS_OK(status)) {
1319 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1320 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1321 ERRSRV, ERRbadpath);
1322 END_PROFILE(SMBntrename);
1325 reply_nterror(req, status);
1326 END_PROFILE(SMBntrename);
1330 status = resolve_dfspath(ctx, conn,
1331 req->flags2 & FLAGS2_DFS_PATHNAMES,
1334 if (!NT_STATUS_IS_OK(status)) {
1335 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1336 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1337 ERRSRV, ERRbadpath);
1338 END_PROFILE(SMBntrename);
1341 reply_nterror(req, status);
1342 END_PROFILE(SMBntrename);
1346 DEBUG(3,("reply_ntrename : %s -> %s\n",oldname,newname));
1348 switch(rename_type) {
1349 case RENAME_FLAG_RENAME:
1350 status = rename_internals(ctx, conn, req, oldname,
1351 newname, attrs, False, src_has_wcard,
1354 case RENAME_FLAG_HARD_LINK:
1355 if (src_has_wcard || dest_has_wcard) {
1357 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1359 status = hardlink_internals(ctx,
1365 case RENAME_FLAG_COPY:
1366 if (src_has_wcard || dest_has_wcard) {
1368 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1370 status = copy_internals(ctx, conn, req, oldname,
1374 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1375 status = NT_STATUS_INVALID_PARAMETER;
1378 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1382 if (!NT_STATUS_IS_OK(status)) {
1383 if (open_was_deferred(req->mid)) {
1384 /* We have re-scheduled this call. */
1385 END_PROFILE(SMBntrename);
1389 reply_nterror(req, status);
1390 END_PROFILE(SMBntrename);
1394 reply_outbuf(req, 0, 0);
1396 END_PROFILE(SMBntrename);
1400 /****************************************************************************
1401 Reply to a notify change - queue the request and
1402 don't allow a directory to be opened.
1403 ****************************************************************************/
1405 static void call_nt_transact_notify_change(connection_struct *conn,
1406 struct smb_request *req,
1410 uint32 parameter_count,
1411 char **ppdata, uint32 data_count,
1412 uint32 max_data_count,
1413 uint32 max_param_count)
1415 uint16 *setup = *ppsetup;
1421 if(setup_count < 6) {
1422 reply_doserror(req, ERRDOS, ERRbadfunc);
1426 fsp = file_fsp(SVAL(setup,4));
1427 filter = IVAL(setup, 0);
1428 recursive = (SVAL(setup, 6) != 0) ? True : False;
1430 DEBUG(3,("call_nt_transact_notify_change\n"));
1433 reply_doserror(req, ERRDOS, ERRbadfid);
1438 char *filter_string;
1440 if (!(filter_string = notify_filter_string(NULL, filter))) {
1441 reply_nterror(req,NT_STATUS_NO_MEMORY);
1445 DEBUG(3,("call_nt_transact_notify_change: notify change "
1446 "called on %s, filter = %s, recursive = %d\n",
1447 fsp->fsp_name, filter_string, recursive));
1449 TALLOC_FREE(filter_string);
1452 if((!fsp->is_directory) || (conn != fsp->conn)) {
1453 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1457 if (fsp->notify == NULL) {
1459 status = change_notify_create(fsp, filter, recursive);
1461 if (!NT_STATUS_IS_OK(status)) {
1462 DEBUG(10, ("change_notify_create returned %s\n",
1463 nt_errstr(status)));
1464 reply_nterror(req, status);
1469 if (fsp->notify->num_changes != 0) {
1472 * We've got changes pending, respond immediately
1476 * TODO: write a torture test to check the filtering behaviour
1480 change_notify_reply(fsp->conn, req->inbuf, max_param_count, fsp->notify);
1483 * change_notify_reply() above has independently sent its
1490 * No changes pending, queue the request
1493 status = change_notify_add_request(req,
1497 if (!NT_STATUS_IS_OK(status)) {
1498 reply_nterror(req, status);
1503 /****************************************************************************
1504 Reply to an NT transact rename command.
1505 ****************************************************************************/
1507 static void call_nt_transact_rename(connection_struct *conn,
1508 struct smb_request *req,
1509 uint16 **ppsetup, uint32 setup_count,
1510 char **ppparams, uint32 parameter_count,
1511 char **ppdata, uint32 data_count,
1512 uint32 max_data_count)
1514 char *params = *ppparams;
1515 char *new_name = NULL;
1516 files_struct *fsp = NULL;
1517 bool replace_if_exists = False;
1518 bool dest_has_wcard = False;
1520 TALLOC_CTX *ctx = talloc_tos();
1522 if(parameter_count < 5) {
1523 reply_doserror(req, ERRDOS, ERRbadfunc);
1527 fsp = file_fsp(SVAL(params, 0));
1528 replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
1529 if (!check_fsp(conn, req, fsp, ¤t_user)) {
1532 srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
1533 parameter_count - 4,
1534 STR_TERMINATE, &status, &dest_has_wcard);
1535 if (!NT_STATUS_IS_OK(status)) {
1536 reply_nterror(req, status);
1540 status = rename_internals(ctx,
1550 if (!NT_STATUS_IS_OK(status)) {
1551 if (open_was_deferred(req->mid)) {
1552 /* We have re-scheduled this call. */
1555 reply_nterror(req, status);
1560 * Rename was successful.
1562 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1564 DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n",
1565 fsp->fsp_name, new_name));
1570 /******************************************************************************
1571 Fake up a completely empty SD.
1572 *******************************************************************************/
1574 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
1578 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1580 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1581 return NT_STATUS_NO_MEMORY;
1584 return NT_STATUS_OK;
1587 /****************************************************************************
1588 Reply to query a security descriptor.
1589 ****************************************************************************/
1591 static void call_nt_transact_query_security_desc(connection_struct *conn,
1592 struct smb_request *req,
1596 uint32 parameter_count,
1599 uint32 max_data_count)
1601 char *params = *ppparams;
1602 char *data = *ppdata;
1603 SEC_DESC *psd = NULL;
1605 uint32 security_info_wanted;
1607 files_struct *fsp = NULL;
1611 if(parameter_count < 8) {
1612 reply_doserror(req, ERRDOS, ERRbadfunc);
1616 fsp = file_fsp(SVAL(params,0));
1618 reply_doserror(req, ERRDOS, ERRbadfid);
1622 security_info_wanted = IVAL(params,4);
1624 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, info_wanted = 0x%x\n", fsp->fsp_name,
1625 (unsigned int)security_info_wanted ));
1627 params = nttrans_realloc(ppparams, 4);
1628 if(params == NULL) {
1629 reply_doserror(req, ERRDOS, ERRnomem);
1633 frame = talloc_stackframe();
1636 * Get the permissions to return.
1639 if (!lp_nt_acl_support(SNUM(conn))) {
1640 status = get_null_nt_acl(talloc_tos(), &psd);
1642 if (fsp->fh->fd != -1) {
1643 status = SMB_VFS_FGET_NT_ACL(
1644 fsp, fsp->fh->fd, security_info_wanted, &psd);
1647 status = SMB_VFS_GET_NT_ACL(
1648 conn, fsp->fsp_name, security_info_wanted, &psd);
1652 if (!NT_STATUS_IS_OK(status)) {
1654 reply_nterror(req, status);
1658 sd_size = ndr_size_security_descriptor(psd, 0);
1660 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
1662 SIVAL(params,0,(uint32)sd_size);
1664 if (max_data_count < sd_size) {
1665 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
1666 params, 4, *ppdata, 0);
1672 * Allocate the data we will point this at.
1675 data = nttrans_realloc(ppdata, sd_size);
1678 reply_doserror(req, ERRDOS, ERRnomem);
1682 status = marshall_sec_desc(talloc_tos(), psd,
1683 &blob.data, &blob.length);
1685 if (!NT_STATUS_IS_OK(status)) {
1687 reply_nterror(req, status);
1691 SMB_ASSERT(sd_size == blob.length);
1692 memcpy(data, blob.data, sd_size);
1694 send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
1700 /****************************************************************************
1701 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
1702 ****************************************************************************/
1704 static void call_nt_transact_set_security_desc(connection_struct *conn,
1705 struct smb_request *req,
1709 uint32 parameter_count,
1712 uint32 max_data_count)
1714 char *params= *ppparams;
1715 char *data = *ppdata;
1716 files_struct *fsp = NULL;
1717 uint32 security_info_sent = 0;
1720 if(parameter_count < 8) {
1721 reply_doserror(req, ERRDOS, ERRbadfunc);
1725 if((fsp = file_fsp(SVAL(params,0))) == NULL) {
1726 reply_doserror(req, ERRDOS, ERRbadfid);
1730 if(!lp_nt_acl_support(SNUM(conn))) {
1734 security_info_sent = IVAL(params,4);
1736 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
1737 (unsigned int)security_info_sent ));
1739 if (data_count == 0) {
1740 reply_doserror(req, ERRDOS, ERRnoaccess);
1744 status = set_sd(fsp, (uint8 *)data, data_count, security_info_sent);
1746 if (!NT_STATUS_IS_OK(status)) {
1747 reply_nterror(req, status);
1752 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1756 /****************************************************************************
1758 ****************************************************************************/
1760 static void call_nt_transact_ioctl(connection_struct *conn,
1761 struct smb_request *req,
1762 uint16 **ppsetup, uint32 setup_count,
1763 char **ppparams, uint32 parameter_count,
1764 char **ppdata, uint32 data_count,
1765 uint32 max_data_count)
1772 static bool logged_message;
1773 char *pdata = *ppdata;
1775 if (setup_count != 8) {
1776 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
1777 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1781 function = IVAL(*ppsetup, 0);
1782 fidnum = SVAL(*ppsetup, 4);
1783 isFSctl = CVAL(*ppsetup, 6);
1784 compfilter = CVAL(*ppsetup, 7);
1786 DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
1787 function, fidnum, isFSctl, compfilter));
1789 fsp=file_fsp(fidnum);
1790 /* this check is done in each implemented function case for now
1791 because I don't want to break anything... --metze
1792 FSP_BELONGS_CONN(fsp,conn);*/
1795 case FSCTL_SET_SPARSE:
1796 /* pretend this succeeded - tho strictly we should
1797 mark the file sparse (if the local fs supports it)
1798 so we can know if we need to pre-allocate or not */
1800 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
1801 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1804 case FSCTL_CREATE_OR_GET_OBJECT_ID:
1806 unsigned char objid[16];
1808 /* This should return the object-id on this file.
1809 * I think I'll make this be the inode+dev. JRA.
1812 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
1814 if (!fsp_belongs_conn(conn, req, fsp, ¤t_user)) {
1819 pdata = nttrans_realloc(ppdata, data_count);
1820 if (pdata == NULL) {
1821 reply_nterror(req, NT_STATUS_NO_MEMORY);
1824 push_file_id_16(pdata, &fsp->file_id);
1825 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
1826 push_file_id_16(pdata+32, &fsp->file_id);
1827 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
1832 case FSCTL_GET_REPARSE_POINT:
1833 /* pretend this fail - my winXP does it like this
1837 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1838 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
1841 case FSCTL_SET_REPARSE_POINT:
1842 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
1846 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1847 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
1850 case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
1853 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
1854 * and return their volume names. If max_data_count is 16, then it is just
1855 * asking for the number of volumes and length of the combined names.
1857 * pdata is the data allocated by our caller, but that uses
1858 * total_data_count (which is 0 in our case) rather than max_data_count.
1859 * Allocate the correct amount and return the pointer to let
1860 * it be deallocated when we return.
1862 SHADOW_COPY_DATA *shadow_data = NULL;
1863 TALLOC_CTX *shadow_mem_ctx = NULL;
1864 bool labels = False;
1865 uint32 labels_data_count = 0;
1869 if (!fsp_belongs_conn(conn, req, fsp, ¤t_user)) {
1873 if (max_data_count < 16) {
1874 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
1876 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1880 if (max_data_count > 16) {
1884 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
1885 if (shadow_mem_ctx == NULL) {
1886 DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
1887 reply_nterror(req, NT_STATUS_NO_MEMORY);
1891 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
1892 if (shadow_data == NULL) {
1893 DEBUG(0,("TALLOC_ZERO() failed!\n"));
1894 talloc_destroy(shadow_mem_ctx);
1895 reply_nterror(req, NT_STATUS_NO_MEMORY);
1899 shadow_data->mem_ctx = shadow_mem_ctx;
1902 * Call the VFS routine to actually do the work.
1904 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
1905 talloc_destroy(shadow_data->mem_ctx);
1906 if (errno == ENOSYS) {
1907 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
1908 conn->connectpath));
1909 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1912 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
1913 conn->connectpath));
1914 reply_nterror(req, NT_STATUS_UNSUCCESSFUL);
1919 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
1924 data_count = 12+labels_data_count+4;
1927 if (max_data_count<data_count) {
1928 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
1929 max_data_count,data_count));
1930 talloc_destroy(shadow_data->mem_ctx);
1931 reply_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
1935 pdata = nttrans_realloc(ppdata, data_count);
1936 if (pdata == NULL) {
1937 talloc_destroy(shadow_data->mem_ctx);
1938 reply_nterror(req, NT_STATUS_NO_MEMORY);
1944 /* num_volumes 4 bytes */
1945 SIVAL(pdata,0,shadow_data->num_volumes);
1948 /* num_labels 4 bytes */
1949 SIVAL(pdata,4,shadow_data->num_volumes);
1952 /* needed_data_count 4 bytes */
1953 SIVAL(pdata,8,labels_data_count);
1957 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
1958 shadow_data->num_volumes,fsp->fsp_name));
1959 if (labels && shadow_data->labels) {
1960 for (i=0;i<shadow_data->num_volumes;i++) {
1961 srvstr_push(pdata, req->flags2,
1962 cur_pdata, shadow_data->labels[i],
1963 2*sizeof(SHADOW_COPY_LABEL),
1964 STR_UNICODE|STR_TERMINATE);
1965 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
1966 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
1970 talloc_destroy(shadow_data->mem_ctx);
1972 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
1978 case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
1980 /* pretend this succeeded -
1982 * we have to send back a list with all files owned by this SID
1984 * but I have to check that --metze
1988 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
1990 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
1992 if (!fsp_belongs_conn(conn, req, fsp, ¤t_user)) {
1996 /* unknown 4 bytes: this is not the length of the sid :-( */
1997 /*unknown = IVAL(pdata,0);*/
1999 sid_parse(pdata+4,sid_len,&sid);
2000 DEBUGADD(10, ("for SID: %s\n", sid_string_dbg(&sid)));
2002 if (!sid_to_uid(&sid, &uid)) {
2003 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2004 sid_string_dbg(&sid),
2005 (unsigned long)sid_len));
2009 /* we can take a look at the find source :-)
2011 * find ./ -uid $uid -name '*' is what we need here
2014 * and send 4bytes len and then NULL terminated unicode strings
2017 * but I don't know how to deal with the paged results
2018 * (maybe we can hang the result anywhere in the fsp struct)
2020 * we don't send all files at once
2021 * and at the next we should *not* start from the beginning,
2022 * so we have to cache the result
2027 /* this works for now... */
2028 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2032 if (!logged_message) {
2033 logged_message = True; /* Only print this once... */
2034 DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
2039 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2043 #ifdef HAVE_SYS_QUOTAS
2044 /****************************************************************************
2045 Reply to get user quota
2046 ****************************************************************************/
2048 static void call_nt_transact_get_user_quota(connection_struct *conn,
2049 struct smb_request *req,
2053 uint32 parameter_count,
2056 uint32 max_data_count)
2058 NTSTATUS nt_status = NT_STATUS_OK;
2059 char *params = *ppparams;
2060 char *pdata = *ppdata;
2062 int data_len=0,param_len=0;
2065 files_struct *fsp = NULL;
2069 bool start_enum = True;
2070 SMB_NTQUOTA_STRUCT qt;
2071 SMB_NTQUOTA_LIST *tmp_list;
2072 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2077 if (current_user.ut.uid != 0) {
2078 DEBUG(1,("get_user_quota: access_denied service [%s] user [%s]\n",
2079 lp_servicename(SNUM(conn)),conn->user));
2080 reply_doserror(req, ERRDOS, ERRnoaccess);
2085 * Ensure minimum number of parameters sent.
2088 if (parameter_count < 4) {
2089 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2090 reply_doserror(req, ERRDOS, ERRinvalidparam);
2094 /* maybe we can check the quota_fnum */
2095 fsp = file_fsp(SVAL(params,0));
2096 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2097 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2098 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2102 /* the NULL pointer checking for fsp->fake_file_handle->pd
2103 * is done by CHECK_NTQUOTA_HANDLE_OK()
2105 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->pd;
2107 level = SVAL(params,2);
2109 /* unknown 12 bytes leading in params */
2112 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2113 /* seems that we should continue with the enum here --metze */
2115 if (qt_handle->quota_list!=NULL &&
2116 qt_handle->tmp_list==NULL) {
2119 free_ntquota_list(&(qt_handle->quota_list));
2121 /* Realloc the size of parameters and data we will return */
2123 params = nttrans_realloc(ppparams, param_len);
2124 if(params == NULL) {
2125 reply_doserror(req, ERRDOS, ERRnomem);
2130 SIVAL(params,0,data_len);
2137 case TRANSACT_GET_USER_QUOTA_LIST_START:
2139 if (qt_handle->quota_list==NULL &&
2140 qt_handle->tmp_list==NULL) {
2144 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2145 reply_doserror(req, ERRSRV, ERRerror);
2149 /* Realloc the size of parameters and data we will return */
2151 params = nttrans_realloc(ppparams, param_len);
2152 if(params == NULL) {
2153 reply_doserror(req, ERRDOS, ERRnomem);
2157 /* we should not trust the value in max_data_count*/
2158 max_data_count = MIN(max_data_count,2048);
2160 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2162 reply_doserror(req, ERRDOS, ERRnomem);
2168 /* set params Size of returned Quota Data 4 bytes*/
2169 /* but set it later when we know it */
2171 /* for each entry push the data */
2174 qt_handle->tmp_list = qt_handle->quota_list;
2177 tmp_list = qt_handle->tmp_list;
2179 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2180 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2182 sid_len = ndr_size_dom_sid(
2183 &tmp_list->quotas->sid, 0);
2184 entry_len = 40 + sid_len;
2186 /* nextoffset entry 4 bytes */
2187 SIVAL(entry,0,entry_len);
2189 /* then the len of the SID 4 bytes */
2190 SIVAL(entry,4,sid_len);
2192 /* unknown data 8 bytes SMB_BIG_UINT */
2193 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-metze*/
2195 /* the used disk space 8 bytes SMB_BIG_UINT */
2196 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2198 /* the soft quotas 8 bytes SMB_BIG_UINT */
2199 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2201 /* the hard quotas 8 bytes SMB_BIG_UINT */
2202 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2204 /* and now the SID */
2205 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2208 qt_handle->tmp_list = tmp_list;
2210 /* overwrite the offset of the last entry */
2211 SIVAL(entry-entry_len,0,0);
2213 data_len = 4+qt_len;
2214 /* overwrite the params quota_data_len */
2215 SIVAL(params,0,data_len);
2219 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2221 /* unknown 4 bytes IVAL(pdata,0) */
2223 if (data_count < 8) {
2224 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2225 reply_doserror(req, ERRDOS, ERRunknownlevel);
2229 sid_len = IVAL(pdata,4);
2230 /* Ensure this is less than 1mb. */
2231 if (sid_len > (1024*1024)) {
2232 reply_doserror(req, ERRDOS, ERRnomem);
2236 if (data_count < 8+sid_len) {
2237 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2238 reply_doserror(req, ERRDOS, ERRunknownlevel);
2242 data_len = 4+40+sid_len;
2244 if (max_data_count < data_len) {
2245 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2246 max_data_count, data_len));
2248 SIVAL(params,0,data_len);
2250 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2254 sid_parse(pdata+8,sid_len,&sid);
2256 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2259 * we have to return zero's in all fields
2260 * instead of returning an error here
2265 /* Realloc the size of parameters and data we will return */
2267 params = nttrans_realloc(ppparams, param_len);
2268 if(params == NULL) {
2269 reply_doserror(req, ERRDOS, ERRnomem);
2273 pdata = nttrans_realloc(ppdata, data_len);
2275 reply_doserror(req, ERRDOS, ERRnomem);
2281 /* set params Size of returned Quota Data 4 bytes*/
2282 SIVAL(params,0,data_len);
2284 /* nextoffset entry 4 bytes */
2287 /* then the len of the SID 4 bytes */
2288 SIVAL(entry,4,sid_len);
2290 /* unknown data 8 bytes SMB_BIG_UINT */
2291 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-mezte*/
2293 /* the used disk space 8 bytes SMB_BIG_UINT */
2294 SBIG_UINT(entry,16,qt.usedspace);
2296 /* the soft quotas 8 bytes SMB_BIG_UINT */
2297 SBIG_UINT(entry,24,qt.softlim);
2299 /* the hard quotas 8 bytes SMB_BIG_UINT */
2300 SBIG_UINT(entry,32,qt.hardlim);
2302 /* and now the SID */
2303 sid_linearize(entry+40, sid_len, &sid);
2308 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2309 reply_doserror(req, ERRSRV, ERRerror);
2314 send_nt_replies(conn, req, nt_status, params, param_len,
2318 /****************************************************************************
2319 Reply to set user quota
2320 ****************************************************************************/
2322 static void call_nt_transact_set_user_quota(connection_struct *conn,
2323 struct smb_request *req,
2327 uint32 parameter_count,
2330 uint32 max_data_count)
2332 char *params = *ppparams;
2333 char *pdata = *ppdata;
2334 int data_len=0,param_len=0;
2335 SMB_NTQUOTA_STRUCT qt;
2338 files_struct *fsp = NULL;
2343 if (current_user.ut.uid != 0) {
2344 DEBUG(1,("set_user_quota: access_denied service [%s] user [%s]\n",
2345 lp_servicename(SNUM(conn)),conn->user));
2346 reply_doserror(req, ERRDOS, ERRnoaccess);
2351 * Ensure minimum number of parameters sent.
2354 if (parameter_count < 2) {
2355 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2356 reply_doserror(req, ERRDOS, ERRinvalidparam);
2360 /* maybe we can check the quota_fnum */
2361 fsp = file_fsp(SVAL(params,0));
2362 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2363 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2364 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2368 if (data_count < 40) {
2369 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2370 reply_doserror(req, ERRDOS, ERRunknownlevel);
2374 /* offset to next quota record.
2375 * 4 bytes IVAL(pdata,0)
2380 sid_len = IVAL(pdata,4);
2382 if (data_count < 40+sid_len) {
2383 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2384 reply_doserror(req, ERRDOS, ERRunknownlevel);
2388 /* unknown 8 bytes in pdata
2389 * maybe its the change time in NTTIME
2392 /* the used space 8 bytes (SMB_BIG_UINT)*/
2393 qt.usedspace = (SMB_BIG_UINT)IVAL(pdata,16);
2394 #ifdef LARGE_SMB_OFF_T
2395 qt.usedspace |= (((SMB_BIG_UINT)IVAL(pdata,20)) << 32);
2396 #else /* LARGE_SMB_OFF_T */
2397 if ((IVAL(pdata,20) != 0)&&
2398 ((qt.usedspace != 0xFFFFFFFF)||
2399 (IVAL(pdata,20)!=0xFFFFFFFF))) {
2400 /* more than 32 bits? */
2401 reply_doserror(req, ERRDOS, ERRunknownlevel);
2404 #endif /* LARGE_SMB_OFF_T */
2406 /* the soft quotas 8 bytes (SMB_BIG_UINT)*/
2407 qt.softlim = (SMB_BIG_UINT)IVAL(pdata,24);
2408 #ifdef LARGE_SMB_OFF_T
2409 qt.softlim |= (((SMB_BIG_UINT)IVAL(pdata,28)) << 32);
2410 #else /* LARGE_SMB_OFF_T */
2411 if ((IVAL(pdata,28) != 0)&&
2412 ((qt.softlim != 0xFFFFFFFF)||
2413 (IVAL(pdata,28)!=0xFFFFFFFF))) {
2414 /* more than 32 bits? */
2415 reply_doserror(req, ERRDOS, ERRunknownlevel);
2418 #endif /* LARGE_SMB_OFF_T */
2420 /* the hard quotas 8 bytes (SMB_BIG_UINT)*/
2421 qt.hardlim = (SMB_BIG_UINT)IVAL(pdata,32);
2422 #ifdef LARGE_SMB_OFF_T
2423 qt.hardlim |= (((SMB_BIG_UINT)IVAL(pdata,36)) << 32);
2424 #else /* LARGE_SMB_OFF_T */
2425 if ((IVAL(pdata,36) != 0)&&
2426 ((qt.hardlim != 0xFFFFFFFF)||
2427 (IVAL(pdata,36)!=0xFFFFFFFF))) {
2428 /* more than 32 bits? */
2429 reply_doserror(req, ERRDOS, ERRunknownlevel);
2432 #endif /* LARGE_SMB_OFF_T */
2434 sid_parse(pdata+40,sid_len,&sid);
2435 DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid)));
2437 /* 44 unknown bytes left... */
2439 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2440 reply_doserror(req, ERRSRV, ERRerror);
2444 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2447 #endif /* HAVE_SYS_QUOTAS */
2449 static void handle_nttrans(connection_struct *conn,
2450 struct trans_state *state,
2451 struct smb_request *req)
2453 if (Protocol >= PROTOCOL_NT1) {
2454 req->flags2 |= 0x40; /* IS_LONG_NAME */
2455 SSVAL(req->inbuf,smb_flg2,req->flags2);
2458 /* Now we must call the relevant NT_TRANS function */
2459 switch(state->call) {
2460 case NT_TRANSACT_CREATE:
2462 START_PROFILE(NT_transact_create);
2463 call_nt_transact_create(
2465 &state->setup, state->setup_count,
2466 &state->param, state->total_param,
2467 &state->data, state->total_data,
2468 state->max_data_return);
2469 END_PROFILE(NT_transact_create);
2473 case NT_TRANSACT_IOCTL:
2475 START_PROFILE(NT_transact_ioctl);
2476 call_nt_transact_ioctl(
2478 &state->setup, state->setup_count,
2479 &state->param, state->total_param,
2480 &state->data, state->total_data,
2481 state->max_data_return);
2482 END_PROFILE(NT_transact_ioctl);
2486 case NT_TRANSACT_SET_SECURITY_DESC:
2488 START_PROFILE(NT_transact_set_security_desc);
2489 call_nt_transact_set_security_desc(
2491 &state->setup, state->setup_count,
2492 &state->param, state->total_param,
2493 &state->data, state->total_data,
2494 state->max_data_return);
2495 END_PROFILE(NT_transact_set_security_desc);
2499 case NT_TRANSACT_NOTIFY_CHANGE:
2501 START_PROFILE(NT_transact_notify_change);
2502 call_nt_transact_notify_change(
2504 &state->setup, state->setup_count,
2505 &state->param, state->total_param,
2506 &state->data, state->total_data,
2507 state->max_data_return,
2508 state->max_param_return);
2509 END_PROFILE(NT_transact_notify_change);
2513 case NT_TRANSACT_RENAME:
2515 START_PROFILE(NT_transact_rename);
2516 call_nt_transact_rename(
2518 &state->setup, state->setup_count,
2519 &state->param, state->total_param,
2520 &state->data, state->total_data,
2521 state->max_data_return);
2522 END_PROFILE(NT_transact_rename);
2526 case NT_TRANSACT_QUERY_SECURITY_DESC:
2528 START_PROFILE(NT_transact_query_security_desc);
2529 call_nt_transact_query_security_desc(
2531 &state->setup, state->setup_count,
2532 &state->param, state->total_param,
2533 &state->data, state->total_data,
2534 state->max_data_return);
2535 END_PROFILE(NT_transact_query_security_desc);
2539 #ifdef HAVE_SYS_QUOTAS
2540 case NT_TRANSACT_GET_USER_QUOTA:
2542 START_PROFILE(NT_transact_get_user_quota);
2543 call_nt_transact_get_user_quota(
2545 &state->setup, state->setup_count,
2546 &state->param, state->total_param,
2547 &state->data, state->total_data,
2548 state->max_data_return);
2549 END_PROFILE(NT_transact_get_user_quota);
2553 case NT_TRANSACT_SET_USER_QUOTA:
2555 START_PROFILE(NT_transact_set_user_quota);
2556 call_nt_transact_set_user_quota(
2558 &state->setup, state->setup_count,
2559 &state->param, state->total_param,
2560 &state->data, state->total_data,
2561 state->max_data_return);
2562 END_PROFILE(NT_transact_set_user_quota);
2565 #endif /* HAVE_SYS_QUOTAS */
2568 /* Error in request */
2569 DEBUG(0,("handle_nttrans: Unknown request %d in "
2570 "nttrans call\n", state->call));
2571 reply_doserror(req, ERRSRV, ERRerror);
2577 /****************************************************************************
2578 Reply to a SMBNTtrans.
2579 ****************************************************************************/
2581 void reply_nttrans(connection_struct *conn, struct smb_request *req)
2587 uint16 function_code;
2589 struct trans_state *state;
2592 START_PROFILE(SMBnttrans);
2594 if (req->wct < 19) {
2595 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2596 END_PROFILE(SMBnttrans);
2600 size = smb_len(req->inbuf) + 4;
2601 pscnt = IVAL(req->inbuf,smb_nt_ParameterCount);
2602 psoff = IVAL(req->inbuf,smb_nt_ParameterOffset);
2603 dscnt = IVAL(req->inbuf,smb_nt_DataCount);
2604 dsoff = IVAL(req->inbuf,smb_nt_DataOffset);
2605 function_code = SVAL(req->inbuf, smb_nt_Function);
2607 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2608 reply_doserror(req, ERRSRV, ERRaccess);
2609 END_PROFILE(SMBnttrans);
2613 result = allow_new_trans(conn->pending_trans, req->mid);
2614 if (!NT_STATUS_IS_OK(result)) {
2615 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
2616 reply_nterror(req, result);
2617 END_PROFILE(SMBnttrans);
2621 if ((state = TALLOC_P(conn->mem_ctx, struct trans_state)) == NULL) {
2622 reply_doserror(req, ERRSRV, ERRaccess);
2623 END_PROFILE(SMBnttrans);
2627 state->cmd = SMBnttrans;
2629 state->mid = req->mid;
2630 state->vuid = req->vuid;
2631 state->total_data = IVAL(req->inbuf, smb_nt_TotalDataCount);
2633 state->total_param = IVAL(req->inbuf, smb_nt_TotalParameterCount);
2634 state->param = NULL;
2635 state->max_data_return = IVAL(req->inbuf,smb_nt_MaxDataCount);
2636 state->max_param_return = IVAL(req->inbuf,smb_nt_MaxParameterCount);
2638 /* setup count is in *words* */
2639 state->setup_count = 2*CVAL(req->inbuf,smb_nt_SetupCount);
2640 state->setup = NULL;
2641 state->call = function_code;
2644 * All nttrans messages we handle have smb_wct == 19 +
2645 * state->setup_count. Ensure this is so as a sanity check.
2648 if(req->wct != 19 + (state->setup_count/2)) {
2649 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2650 req->wct, 19 + (state->setup_count/2)));
2654 /* Don't allow more than 128mb for each value. */
2655 if ((state->total_data > (1024*1024*128)) ||
2656 (state->total_param > (1024*1024*128))) {
2657 reply_doserror(req, ERRDOS, ERRnomem);
2658 END_PROFILE(SMBnttrans);
2662 if ((dscnt > state->total_data) || (pscnt > state->total_param))
2665 if (state->total_data) {
2666 /* Can't use talloc here, the core routines do realloc on the
2667 * params and data. */
2668 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
2669 DEBUG(0,("reply_nttrans: data malloc fail for %u "
2670 "bytes !\n", (unsigned int)state->total_data));
2672 reply_doserror(req, ERRDOS, ERRnomem);
2673 END_PROFILE(SMBnttrans);
2676 if ((dsoff+dscnt < dsoff) || (dsoff+dscnt < dscnt))
2678 if ((smb_base(req->inbuf)+dsoff+dscnt
2679 > (char *)req->inbuf + size) ||
2680 (smb_base(req->inbuf)+dsoff+dscnt < smb_base(req->inbuf)))
2683 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
2686 if (state->total_param) {
2687 /* Can't use talloc here, the core routines do realloc on the
2688 * params and data. */
2689 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
2690 DEBUG(0,("reply_nttrans: param malloc fail for %u "
2691 "bytes !\n", (unsigned int)state->total_param));
2692 SAFE_FREE(state->data);
2694 reply_doserror(req, ERRDOS, ERRnomem);
2695 END_PROFILE(SMBnttrans);
2698 if ((psoff+pscnt < psoff) || (psoff+pscnt < pscnt))
2700 if ((smb_base(req->inbuf)+psoff+pscnt
2701 > (char *)req->inbuf + size) ||
2702 (smb_base(req->inbuf)+psoff+pscnt < smb_base(req->inbuf)))
2705 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
2708 state->received_data = dscnt;
2709 state->received_param = pscnt;
2711 if(state->setup_count > 0) {
2712 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2713 state->setup_count));
2714 state->setup = (uint16 *)TALLOC(state, state->setup_count);
2715 if (state->setup == NULL) {
2716 DEBUG(0,("reply_nttrans : Out of memory\n"));
2717 SAFE_FREE(state->data);
2718 SAFE_FREE(state->param);
2720 reply_doserror(req, ERRDOS, ERRnomem);
2721 END_PROFILE(SMBnttrans);
2725 if ((smb_nt_SetupStart + state->setup_count < smb_nt_SetupStart) ||
2726 (smb_nt_SetupStart + state->setup_count < state->setup_count)) {
2729 if (smb_nt_SetupStart + state->setup_count > size) {
2733 memcpy( state->setup, &req->inbuf[smb_nt_SetupStart],
2734 state->setup_count);
2735 dump_data(10, (uint8 *)state->setup, state->setup_count);
2738 if ((state->received_data == state->total_data) &&
2739 (state->received_param == state->total_param)) {
2740 handle_nttrans(conn, state, req);
2741 SAFE_FREE(state->param);
2742 SAFE_FREE(state->data);
2744 END_PROFILE(SMBnttrans);
2748 DLIST_ADD(conn->pending_trans, state);
2750 /* We need to send an interim response then receive the rest
2751 of the parameter/data bytes */
2752 reply_outbuf(req, 0, 0);
2753 show_msg((char *)req->outbuf);
2754 END_PROFILE(SMBnttrans);
2759 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
2760 SAFE_FREE(state->data);
2761 SAFE_FREE(state->param);
2763 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2764 END_PROFILE(SMBnttrans);
2768 /****************************************************************************
2769 Reply to a SMBnttranss
2770 ****************************************************************************/
2772 void reply_nttranss(connection_struct *conn, struct smb_request *req)
2774 unsigned int pcnt,poff,dcnt,doff,pdisp,ddisp;
2775 struct trans_state *state;
2779 START_PROFILE(SMBnttranss);
2781 show_msg((char *)req->inbuf);
2783 if (req->wct < 18) {
2784 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2785 END_PROFILE(SMBnttranss);
2789 for (state = conn->pending_trans; state != NULL;
2790 state = state->next) {
2791 if (state->mid == req->mid) {
2796 if ((state == NULL) || (state->cmd != SMBnttrans)) {
2797 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2798 END_PROFILE(SMBnttranss);
2802 /* Revise state->total_param and state->total_data in case they have
2803 changed downwards */
2804 if (IVAL(req->inbuf, smb_nts_TotalParameterCount)
2805 < state->total_param) {
2806 state->total_param = IVAL(req->inbuf,
2807 smb_nts_TotalParameterCount);
2809 if (IVAL(req->inbuf, smb_nts_TotalDataCount) < state->total_data) {
2810 state->total_data = IVAL(req->inbuf, smb_nts_TotalDataCount);
2813 size = smb_len(req->inbuf) + 4;
2815 pcnt = IVAL(req->inbuf,smb_nts_ParameterCount);
2816 poff = IVAL(req->inbuf, smb_nts_ParameterOffset);
2817 pdisp = IVAL(req->inbuf, smb_nts_ParameterDisplacement);
2819 dcnt = IVAL(req->inbuf, smb_nts_DataCount);
2820 ddisp = IVAL(req->inbuf, smb_nts_DataDisplacement);
2821 doff = IVAL(req->inbuf, smb_nts_DataOffset);
2823 state->received_param += pcnt;
2824 state->received_data += dcnt;
2826 if ((state->received_data > state->total_data) ||
2827 (state->received_param > state->total_param))
2831 if (pdisp+pcnt > state->total_param)
2833 if ((pdisp+pcnt < pdisp) || (pdisp+pcnt < pcnt))
2835 if (pdisp > state->total_param)
2837 if ((smb_base(req->inbuf) + poff + pcnt
2838 > (char *)req->inbuf + size) ||
2839 (smb_base(req->inbuf) + poff + pcnt
2840 < smb_base(req->inbuf)))
2842 if (state->param + pdisp < state->param)
2845 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,
2850 if (ddisp+dcnt > state->total_data)
2852 if ((ddisp+dcnt < ddisp) || (ddisp+dcnt < dcnt))
2854 if (ddisp > state->total_data)
2856 if ((smb_base(req->inbuf) + doff + dcnt
2857 > (char *)req->inbuf + size) ||
2858 (smb_base(req->inbuf) + doff + dcnt
2859 < smb_base(req->inbuf)))
2861 if (state->data + ddisp < state->data)
2864 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,
2868 if ((state->received_param < state->total_param) ||
2869 (state->received_data < state->total_data)) {
2870 END_PROFILE(SMBnttranss);
2875 * construct_reply_common will copy smb_com from inbuf to
2876 * outbuf. SMBnttranss is wrong here.
2878 SCVAL(req->inbuf,smb_com,SMBnttrans);
2880 handle_nttrans(conn, state, req);
2882 DLIST_REMOVE(conn->pending_trans, state);
2883 SAFE_FREE(state->data);
2884 SAFE_FREE(state->param);
2886 END_PROFILE(SMBnttranss);
2891 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
2892 DLIST_REMOVE(conn->pending_trans, state);
2893 SAFE_FREE(state->data);
2894 SAFE_FREE(state->param);
2896 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2897 END_PROFILE(SMBnttranss);