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(struct smb_request *req)
418 connection_struct *conn = req->conn;
422 uint32 file_attributes;
424 uint32 create_disposition;
425 uint32 create_options;
427 SMB_BIG_UINT allocation_size;
428 /* Breakout the oplock request bits so we can set the
429 reply bits separately. */
431 SMB_OFF_T file_len = 0;
432 SMB_STRUCT_STAT sbuf;
434 files_struct *fsp = NULL;
436 struct timespec c_timespec;
437 struct timespec a_timespec;
438 struct timespec m_timespec;
441 uint8_t oplock_granted = NO_OPLOCK_RETURN;
442 TALLOC_CTX *ctx = talloc_tos();
444 START_PROFILE(SMBntcreateX);
447 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
451 flags = IVAL(req->inbuf,smb_ntcreate_Flags);
452 access_mask = IVAL(req->inbuf,smb_ntcreate_DesiredAccess);
453 file_attributes = IVAL(req->inbuf,smb_ntcreate_FileAttributes);
454 share_access = IVAL(req->inbuf,smb_ntcreate_ShareAccess);
455 create_disposition = IVAL(req->inbuf,smb_ntcreate_CreateDisposition);
456 create_options = IVAL(req->inbuf,smb_ntcreate_CreateOptions);
457 root_dir_fid = (uint16)IVAL(req->inbuf,smb_ntcreate_RootDirectoryFid);
459 allocation_size = (SMB_BIG_UINT)IVAL(req->inbuf,
460 smb_ntcreate_AllocationSize);
461 #ifdef LARGE_SMB_OFF_T
462 allocation_size |= (((SMB_BIG_UINT)IVAL(
464 smb_ntcreate_AllocationSize + 4)) << 32);
467 srvstr_get_path(ctx, (char *)req->inbuf, req->flags2, &fname,
468 smb_buf(req->inbuf), 0, STR_TERMINATE, &status);
470 if (!NT_STATUS_IS_OK(status)) {
471 reply_nterror(req, status);
472 END_PROFILE(SMBntcreateX);
476 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
477 "file_attributes = 0x%x, share_access = 0x%x, "
478 "create_disposition = 0x%x create_options = 0x%x "
479 "root_dir_fid = 0x%x, fname = %s\n",
481 (unsigned int)access_mask,
482 (unsigned int)file_attributes,
483 (unsigned int)share_access,
484 (unsigned int)create_disposition,
485 (unsigned int)create_options,
486 (unsigned int)root_dir_fid,
490 * If it's an IPC, use the pipe handler.
494 if (lp_nt_pipe_support()) {
495 do_ntcreate_pipe_open(conn, req);
496 END_PROFILE(SMBntcreateX);
499 reply_doserror(req, ERRDOS, ERRnoaccess);
500 END_PROFILE(SMBntcreateX);
505 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
506 if (oplock_request) {
507 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
511 status = create_file(conn, req, root_dir_fid, fname,
512 access_mask, share_access, create_disposition,
513 create_options, file_attributes, oplock_request,
514 allocation_size, NULL, NULL, &fsp, &info, &sbuf);
516 if (!NT_STATUS_IS_OK(status)) {
517 if (open_was_deferred(req->mid)) {
518 /* We have re-scheduled this call, no error. */
519 END_PROFILE(SMBntcreateX);
522 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
523 reply_botherror(req, status, ERRDOS, ERRfilexists);
526 reply_nterror(req, status);
528 END_PROFILE(SMBntcreateX);
533 * If the caller set the extended oplock request bit
534 * and we granted one (by whatever means) - set the
535 * correct bit for extended oplock reply.
538 if (oplock_request &&
539 (lp_fake_oplocks(SNUM(conn))
540 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
543 * Exclusive oplock granted
546 if (flags & REQUEST_BATCH_OPLOCK) {
547 oplock_granted = BATCH_OPLOCK_RETURN;
549 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
551 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
552 oplock_granted = LEVEL_II_OPLOCK_RETURN;
554 oplock_granted = NO_OPLOCK_RETURN;
557 file_len = sbuf.st_size;
558 fattr = dos_mode(conn,fname,&sbuf);
560 fattr = FILE_ATTRIBUTE_NORMAL;
563 if (flags & EXTENDED_RESPONSE_REQUIRED) {
564 /* This is very strange. We
565 * return 50 words, but only set
566 * the wcnt to 42 ? It's definately
567 * what happens on the wire....
569 reply_outbuf(req, 50, 0);
570 SCVAL(req->outbuf,smb_wct,42);
572 reply_outbuf(req, 34, 0);
575 p = (char *)req->outbuf + smb_vwv2;
577 SCVAL(p, 0, oplock_granted);
580 SSVAL(p,0,fsp->fnum);
582 if ((create_disposition == FILE_SUPERSEDE)
583 && (info == FILE_WAS_OVERWRITTEN)) {
584 SIVAL(p,0,FILE_WAS_SUPERSEDED);
591 c_timespec = get_create_timespec(
592 &sbuf,lp_fake_dir_create_times(SNUM(conn)));
593 a_timespec = get_atimespec(&sbuf);
594 m_timespec = get_mtimespec(&sbuf);
596 if (lp_dos_filetime_resolution(SNUM(conn))) {
597 dos_filetime_timespec(&c_timespec);
598 dos_filetime_timespec(&a_timespec);
599 dos_filetime_timespec(&m_timespec);
602 put_long_date_timespec(p, c_timespec); /* create time. */
604 put_long_date_timespec(p, a_timespec); /* access time */
606 put_long_date_timespec(p, m_timespec); /* write time */
608 put_long_date_timespec(p, m_timespec); /* change time */
610 SIVAL(p,0,fattr); /* File Attributes. */
612 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
614 SOFF_T(p,0,file_len);
616 if (flags & EXTENDED_RESPONSE_REQUIRED) {
620 SCVAL(p,0,fsp->is_directory ? 1 : 0);
622 if (flags & EXTENDED_RESPONSE_REQUIRED) {
625 if (fsp->is_directory
626 || can_write_to_file(conn, fname, &sbuf)) {
627 perms = FILE_GENERIC_ALL;
629 perms = FILE_GENERIC_READ|FILE_EXECUTE;
634 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n",
635 fsp->fnum, fsp->fsp_name));
638 END_PROFILE(SMBntcreateX);
642 /****************************************************************************
643 Reply to a NT_TRANSACT_CREATE call to open a pipe.
644 ****************************************************************************/
646 static void do_nt_transact_create_pipe(connection_struct *conn,
647 struct smb_request *req,
648 uint16 **ppsetup, uint32 setup_count,
649 char **ppparams, uint32 parameter_count,
650 char **ppdata, uint32 data_count)
653 char *params = *ppparams;
659 TALLOC_CTX *ctx = talloc_tos();
662 * Ensure minimum number of parameters sent.
665 if(parameter_count < 54) {
666 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
667 reply_doserror(req, ERRDOS, ERRnoaccess);
671 flags = IVAL(params,0);
673 srvstr_get_path(ctx, params, req->flags2, &fname, params+53,
674 parameter_count-53, STR_TERMINATE,
676 if (!NT_STATUS_IS_OK(status)) {
677 reply_nterror(req, status);
681 nt_open_pipe(fname, conn, req, &pnum);
688 /* Realloc the size of parameters and data we will return */
689 if (flags & EXTENDED_RESPONSE_REQUIRED) {
690 /* Extended response is 32 more byyes. */
695 params = nttrans_realloc(ppparams, param_len);
697 reply_doserror(req, ERRDOS, ERRnomem);
702 SCVAL(p,0,NO_OPLOCK_RETURN);
707 SIVAL(p,0,FILE_WAS_OPENED);
711 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
714 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
716 SSVAL(p,2, 0x5FF); /* ? */
719 if (flags & EXTENDED_RESPONSE_REQUIRED) {
721 SIVAL(p,0,FILE_GENERIC_ALL);
723 * For pipes W2K3 seems to return
725 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
727 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
730 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
732 /* Send the required number of replies */
733 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
738 /****************************************************************************
739 Internal fn to set security descriptors.
740 ****************************************************************************/
742 static NTSTATUS set_sd(files_struct *fsp, uint8 *data, uint32 sd_len,
743 uint32 security_info_sent)
745 SEC_DESC *psd = NULL;
748 if (sd_len == 0 || !lp_nt_acl_support(SNUM(fsp->conn))) {
752 status = unmarshall_sec_desc(talloc_tos(), data, sd_len, &psd);
754 if (!NT_STATUS_IS_OK(status)) {
758 if (psd->owner_sid==0) {
759 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
761 if (psd->group_sid==0) {
762 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
765 security_info_sent &= ~SACL_SECURITY_INFORMATION;
768 security_info_sent &= ~DACL_SECURITY_INFORMATION;
771 if (fsp->fh->fd != -1) {
772 status = SMB_VFS_FSET_NT_ACL(fsp, 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(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(struct smb_request *req)
1260 connection_struct *conn = req->conn;
1261 char *oldname = NULL;
1262 char *newname = NULL;
1265 bool src_has_wcard = False;
1266 bool dest_has_wcard = False;
1269 TALLOC_CTX *ctx = talloc_tos();
1271 START_PROFILE(SMBntrename);
1274 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1275 END_PROFILE(SMBntrename);
1279 attrs = SVAL(req->inbuf,smb_vwv0);
1280 rename_type = SVAL(req->inbuf,smb_vwv1);
1282 p = smb_buf(req->inbuf) + 1;
1283 p += srvstr_get_path_wcard(ctx, (char *)req->inbuf, req->flags2, &oldname, p,
1284 0, STR_TERMINATE, &status,
1286 if (!NT_STATUS_IS_OK(status)) {
1287 reply_nterror(req, status);
1288 END_PROFILE(SMBntrename);
1292 if( is_ntfs_stream_name(oldname)) {
1293 /* Can't rename a stream. */
1294 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1295 END_PROFILE(SMBntrename);
1299 if (ms_has_wild(oldname)) {
1300 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1301 END_PROFILE(SMBntrename);
1306 p += srvstr_get_path_wcard(ctx, (char *)req->inbuf, req->flags2, &newname, p,
1307 0, STR_TERMINATE, &status,
1309 if (!NT_STATUS_IS_OK(status)) {
1310 reply_nterror(req, status);
1311 END_PROFILE(SMBntrename);
1315 status = resolve_dfspath(ctx, conn,
1316 req->flags2 & FLAGS2_DFS_PATHNAMES,
1319 if (!NT_STATUS_IS_OK(status)) {
1320 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1321 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1322 ERRSRV, ERRbadpath);
1323 END_PROFILE(SMBntrename);
1326 reply_nterror(req, status);
1327 END_PROFILE(SMBntrename);
1331 status = resolve_dfspath(ctx, conn,
1332 req->flags2 & FLAGS2_DFS_PATHNAMES,
1335 if (!NT_STATUS_IS_OK(status)) {
1336 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1337 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1338 ERRSRV, ERRbadpath);
1339 END_PROFILE(SMBntrename);
1342 reply_nterror(req, status);
1343 END_PROFILE(SMBntrename);
1347 DEBUG(3,("reply_ntrename : %s -> %s\n",oldname,newname));
1349 switch(rename_type) {
1350 case RENAME_FLAG_RENAME:
1351 status = rename_internals(ctx, conn, req, oldname,
1352 newname, attrs, False, src_has_wcard,
1355 case RENAME_FLAG_HARD_LINK:
1356 if (src_has_wcard || dest_has_wcard) {
1358 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1360 status = hardlink_internals(ctx,
1366 case RENAME_FLAG_COPY:
1367 if (src_has_wcard || dest_has_wcard) {
1369 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1371 status = copy_internals(ctx, conn, req, oldname,
1375 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1376 status = NT_STATUS_INVALID_PARAMETER;
1379 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1383 if (!NT_STATUS_IS_OK(status)) {
1384 if (open_was_deferred(req->mid)) {
1385 /* We have re-scheduled this call. */
1386 END_PROFILE(SMBntrename);
1390 reply_nterror(req, status);
1391 END_PROFILE(SMBntrename);
1395 reply_outbuf(req, 0, 0);
1397 END_PROFILE(SMBntrename);
1401 /****************************************************************************
1402 Reply to a notify change - queue the request and
1403 don't allow a directory to be opened.
1404 ****************************************************************************/
1406 static void call_nt_transact_notify_change(connection_struct *conn,
1407 struct smb_request *req,
1411 uint32 parameter_count,
1412 char **ppdata, uint32 data_count,
1413 uint32 max_data_count,
1414 uint32 max_param_count)
1416 uint16 *setup = *ppsetup;
1422 if(setup_count < 6) {
1423 reply_doserror(req, ERRDOS, ERRbadfunc);
1427 fsp = file_fsp(SVAL(setup,4));
1428 filter = IVAL(setup, 0);
1429 recursive = (SVAL(setup, 6) != 0) ? True : False;
1431 DEBUG(3,("call_nt_transact_notify_change\n"));
1434 reply_doserror(req, ERRDOS, ERRbadfid);
1439 char *filter_string;
1441 if (!(filter_string = notify_filter_string(NULL, filter))) {
1442 reply_nterror(req,NT_STATUS_NO_MEMORY);
1446 DEBUG(3,("call_nt_transact_notify_change: notify change "
1447 "called on %s, filter = %s, recursive = %d\n",
1448 fsp->fsp_name, filter_string, recursive));
1450 TALLOC_FREE(filter_string);
1453 if((!fsp->is_directory) || (conn != fsp->conn)) {
1454 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1458 if (fsp->notify == NULL) {
1460 status = change_notify_create(fsp, filter, recursive);
1462 if (!NT_STATUS_IS_OK(status)) {
1463 DEBUG(10, ("change_notify_create returned %s\n",
1464 nt_errstr(status)));
1465 reply_nterror(req, status);
1470 if (fsp->notify->num_changes != 0) {
1473 * We've got changes pending, respond immediately
1477 * TODO: write a torture test to check the filtering behaviour
1481 change_notify_reply(fsp->conn, req->inbuf, max_param_count, fsp->notify);
1484 * change_notify_reply() above has independently sent its
1491 * No changes pending, queue the request
1494 status = change_notify_add_request(req,
1498 if (!NT_STATUS_IS_OK(status)) {
1499 reply_nterror(req, status);
1504 /****************************************************************************
1505 Reply to an NT transact rename command.
1506 ****************************************************************************/
1508 static void call_nt_transact_rename(connection_struct *conn,
1509 struct smb_request *req,
1510 uint16 **ppsetup, uint32 setup_count,
1511 char **ppparams, uint32 parameter_count,
1512 char **ppdata, uint32 data_count,
1513 uint32 max_data_count)
1515 char *params = *ppparams;
1516 char *new_name = NULL;
1517 files_struct *fsp = NULL;
1518 bool replace_if_exists = False;
1519 bool dest_has_wcard = False;
1521 TALLOC_CTX *ctx = talloc_tos();
1523 if(parameter_count < 5) {
1524 reply_doserror(req, ERRDOS, ERRbadfunc);
1528 fsp = file_fsp(SVAL(params, 0));
1529 replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
1530 if (!check_fsp(conn, req, fsp, ¤t_user)) {
1533 srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
1534 parameter_count - 4,
1535 STR_TERMINATE, &status, &dest_has_wcard);
1536 if (!NT_STATUS_IS_OK(status)) {
1537 reply_nterror(req, status);
1541 status = rename_internals(ctx,
1551 if (!NT_STATUS_IS_OK(status)) {
1552 if (open_was_deferred(req->mid)) {
1553 /* We have re-scheduled this call. */
1556 reply_nterror(req, status);
1561 * Rename was successful.
1563 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1565 DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n",
1566 fsp->fsp_name, new_name));
1571 /******************************************************************************
1572 Fake up a completely empty SD.
1573 *******************************************************************************/
1575 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
1579 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1581 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1582 return NT_STATUS_NO_MEMORY;
1585 return NT_STATUS_OK;
1588 /****************************************************************************
1589 Reply to query a security descriptor.
1590 ****************************************************************************/
1592 static void call_nt_transact_query_security_desc(connection_struct *conn,
1593 struct smb_request *req,
1597 uint32 parameter_count,
1600 uint32 max_data_count)
1602 char *params = *ppparams;
1603 char *data = *ppdata;
1604 SEC_DESC *psd = NULL;
1606 uint32 security_info_wanted;
1608 files_struct *fsp = NULL;
1612 if(parameter_count < 8) {
1613 reply_doserror(req, ERRDOS, ERRbadfunc);
1617 fsp = file_fsp(SVAL(params,0));
1619 reply_doserror(req, ERRDOS, ERRbadfid);
1623 security_info_wanted = IVAL(params,4);
1625 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, info_wanted = 0x%x\n", fsp->fsp_name,
1626 (unsigned int)security_info_wanted ));
1628 params = nttrans_realloc(ppparams, 4);
1629 if(params == NULL) {
1630 reply_doserror(req, ERRDOS, ERRnomem);
1634 frame = talloc_stackframe();
1637 * Get the permissions to return.
1640 if (!lp_nt_acl_support(SNUM(conn))) {
1641 status = get_null_nt_acl(talloc_tos(), &psd);
1643 if (fsp->fh->fd != -1) {
1644 status = SMB_VFS_FGET_NT_ACL(
1645 fsp, security_info_wanted, &psd);
1648 status = SMB_VFS_GET_NT_ACL(
1649 conn, fsp->fsp_name, security_info_wanted, &psd);
1653 if (!NT_STATUS_IS_OK(status)) {
1655 reply_nterror(req, status);
1659 sd_size = ndr_size_security_descriptor(psd, 0);
1661 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
1663 SIVAL(params,0,(uint32)sd_size);
1665 if (max_data_count < sd_size) {
1666 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
1667 params, 4, *ppdata, 0);
1673 * Allocate the data we will point this at.
1676 data = nttrans_realloc(ppdata, sd_size);
1679 reply_doserror(req, ERRDOS, ERRnomem);
1683 status = marshall_sec_desc(talloc_tos(), psd,
1684 &blob.data, &blob.length);
1686 if (!NT_STATUS_IS_OK(status)) {
1688 reply_nterror(req, status);
1692 SMB_ASSERT(sd_size == blob.length);
1693 memcpy(data, blob.data, sd_size);
1695 send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
1701 /****************************************************************************
1702 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
1703 ****************************************************************************/
1705 static void call_nt_transact_set_security_desc(connection_struct *conn,
1706 struct smb_request *req,
1710 uint32 parameter_count,
1713 uint32 max_data_count)
1715 char *params= *ppparams;
1716 char *data = *ppdata;
1717 files_struct *fsp = NULL;
1718 uint32 security_info_sent = 0;
1721 if(parameter_count < 8) {
1722 reply_doserror(req, ERRDOS, ERRbadfunc);
1726 if((fsp = file_fsp(SVAL(params,0))) == NULL) {
1727 reply_doserror(req, ERRDOS, ERRbadfid);
1731 if(!lp_nt_acl_support(SNUM(conn))) {
1735 security_info_sent = IVAL(params,4);
1737 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
1738 (unsigned int)security_info_sent ));
1740 if (data_count == 0) {
1741 reply_doserror(req, ERRDOS, ERRnoaccess);
1745 status = set_sd(fsp, (uint8 *)data, data_count, security_info_sent);
1747 if (!NT_STATUS_IS_OK(status)) {
1748 reply_nterror(req, status);
1753 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1757 /****************************************************************************
1759 ****************************************************************************/
1761 static void call_nt_transact_ioctl(connection_struct *conn,
1762 struct smb_request *req,
1763 uint16 **ppsetup, uint32 setup_count,
1764 char **ppparams, uint32 parameter_count,
1765 char **ppdata, uint32 data_count,
1766 uint32 max_data_count)
1773 static bool logged_message;
1774 char *pdata = *ppdata;
1776 if (setup_count != 8) {
1777 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
1778 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1782 function = IVAL(*ppsetup, 0);
1783 fidnum = SVAL(*ppsetup, 4);
1784 isFSctl = CVAL(*ppsetup, 6);
1785 compfilter = CVAL(*ppsetup, 7);
1787 DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
1788 function, fidnum, isFSctl, compfilter));
1790 fsp=file_fsp(fidnum);
1791 /* this check is done in each implemented function case for now
1792 because I don't want to break anything... --metze
1793 FSP_BELONGS_CONN(fsp,conn);*/
1796 case FSCTL_SET_SPARSE:
1797 /* pretend this succeeded - tho strictly we should
1798 mark the file sparse (if the local fs supports it)
1799 so we can know if we need to pre-allocate or not */
1801 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
1802 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1805 case FSCTL_CREATE_OR_GET_OBJECT_ID:
1807 unsigned char objid[16];
1809 /* This should return the object-id on this file.
1810 * I think I'll make this be the inode+dev. JRA.
1813 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
1815 if (!fsp_belongs_conn(conn, req, fsp, ¤t_user)) {
1820 pdata = nttrans_realloc(ppdata, data_count);
1821 if (pdata == NULL) {
1822 reply_nterror(req, NT_STATUS_NO_MEMORY);
1825 push_file_id_16(pdata, &fsp->file_id);
1826 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
1827 push_file_id_16(pdata+32, &fsp->file_id);
1828 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
1833 case FSCTL_GET_REPARSE_POINT:
1834 /* pretend this fail - my winXP does it like this
1838 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1839 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
1842 case FSCTL_SET_REPARSE_POINT:
1843 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
1847 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1848 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
1851 case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
1854 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
1855 * and return their volume names. If max_data_count is 16, then it is just
1856 * asking for the number of volumes and length of the combined names.
1858 * pdata is the data allocated by our caller, but that uses
1859 * total_data_count (which is 0 in our case) rather than max_data_count.
1860 * Allocate the correct amount and return the pointer to let
1861 * it be deallocated when we return.
1863 SHADOW_COPY_DATA *shadow_data = NULL;
1864 TALLOC_CTX *shadow_mem_ctx = NULL;
1865 bool labels = False;
1866 uint32 labels_data_count = 0;
1870 if (!fsp_belongs_conn(conn, req, fsp, ¤t_user)) {
1874 if (max_data_count < 16) {
1875 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
1877 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1881 if (max_data_count > 16) {
1885 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
1886 if (shadow_mem_ctx == NULL) {
1887 DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
1888 reply_nterror(req, NT_STATUS_NO_MEMORY);
1892 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
1893 if (shadow_data == NULL) {
1894 DEBUG(0,("TALLOC_ZERO() failed!\n"));
1895 talloc_destroy(shadow_mem_ctx);
1896 reply_nterror(req, NT_STATUS_NO_MEMORY);
1900 shadow_data->mem_ctx = shadow_mem_ctx;
1903 * Call the VFS routine to actually do the work.
1905 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
1906 talloc_destroy(shadow_data->mem_ctx);
1907 if (errno == ENOSYS) {
1908 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
1909 conn->connectpath));
1910 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1913 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
1914 conn->connectpath));
1915 reply_nterror(req, NT_STATUS_UNSUCCESSFUL);
1920 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
1925 data_count = 12+labels_data_count+4;
1928 if (max_data_count<data_count) {
1929 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
1930 max_data_count,data_count));
1931 talloc_destroy(shadow_data->mem_ctx);
1932 reply_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
1936 pdata = nttrans_realloc(ppdata, data_count);
1937 if (pdata == NULL) {
1938 talloc_destroy(shadow_data->mem_ctx);
1939 reply_nterror(req, NT_STATUS_NO_MEMORY);
1945 /* num_volumes 4 bytes */
1946 SIVAL(pdata,0,shadow_data->num_volumes);
1949 /* num_labels 4 bytes */
1950 SIVAL(pdata,4,shadow_data->num_volumes);
1953 /* needed_data_count 4 bytes */
1954 SIVAL(pdata,8,labels_data_count);
1958 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
1959 shadow_data->num_volumes,fsp->fsp_name));
1960 if (labels && shadow_data->labels) {
1961 for (i=0;i<shadow_data->num_volumes;i++) {
1962 srvstr_push(pdata, req->flags2,
1963 cur_pdata, shadow_data->labels[i],
1964 2*sizeof(SHADOW_COPY_LABEL),
1965 STR_UNICODE|STR_TERMINATE);
1966 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
1967 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
1971 talloc_destroy(shadow_data->mem_ctx);
1973 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
1979 case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
1981 /* pretend this succeeded -
1983 * we have to send back a list with all files owned by this SID
1985 * but I have to check that --metze
1989 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
1991 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
1993 if (!fsp_belongs_conn(conn, req, fsp, ¤t_user)) {
1997 /* unknown 4 bytes: this is not the length of the sid :-( */
1998 /*unknown = IVAL(pdata,0);*/
2000 sid_parse(pdata+4,sid_len,&sid);
2001 DEBUGADD(10, ("for SID: %s\n", sid_string_dbg(&sid)));
2003 if (!sid_to_uid(&sid, &uid)) {
2004 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2005 sid_string_dbg(&sid),
2006 (unsigned long)sid_len));
2010 /* we can take a look at the find source :-)
2012 * find ./ -uid $uid -name '*' is what we need here
2015 * and send 4bytes len and then NULL terminated unicode strings
2018 * but I don't know how to deal with the paged results
2019 * (maybe we can hang the result anywhere in the fsp struct)
2021 * we don't send all files at once
2022 * and at the next we should *not* start from the beginning,
2023 * so we have to cache the result
2028 /* this works for now... */
2029 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2033 if (!logged_message) {
2034 logged_message = True; /* Only print this once... */
2035 DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
2040 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2044 #ifdef HAVE_SYS_QUOTAS
2045 /****************************************************************************
2046 Reply to get user quota
2047 ****************************************************************************/
2049 static void call_nt_transact_get_user_quota(connection_struct *conn,
2050 struct smb_request *req,
2054 uint32 parameter_count,
2057 uint32 max_data_count)
2059 NTSTATUS nt_status = NT_STATUS_OK;
2060 char *params = *ppparams;
2061 char *pdata = *ppdata;
2063 int data_len=0,param_len=0;
2066 files_struct *fsp = NULL;
2070 bool start_enum = True;
2071 SMB_NTQUOTA_STRUCT qt;
2072 SMB_NTQUOTA_LIST *tmp_list;
2073 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2078 if (current_user.ut.uid != 0) {
2079 DEBUG(1,("get_user_quota: access_denied service [%s] user [%s]\n",
2080 lp_servicename(SNUM(conn)),conn->user));
2081 reply_doserror(req, ERRDOS, ERRnoaccess);
2086 * Ensure minimum number of parameters sent.
2089 if (parameter_count < 4) {
2090 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2091 reply_doserror(req, ERRDOS, ERRinvalidparam);
2095 /* maybe we can check the quota_fnum */
2096 fsp = file_fsp(SVAL(params,0));
2097 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2098 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2099 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2103 /* the NULL pointer checking for fsp->fake_file_handle->pd
2104 * is done by CHECK_NTQUOTA_HANDLE_OK()
2106 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->pd;
2108 level = SVAL(params,2);
2110 /* unknown 12 bytes leading in params */
2113 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2114 /* seems that we should continue with the enum here --metze */
2116 if (qt_handle->quota_list!=NULL &&
2117 qt_handle->tmp_list==NULL) {
2120 free_ntquota_list(&(qt_handle->quota_list));
2122 /* Realloc the size of parameters and data we will return */
2124 params = nttrans_realloc(ppparams, param_len);
2125 if(params == NULL) {
2126 reply_doserror(req, ERRDOS, ERRnomem);
2131 SIVAL(params,0,data_len);
2138 case TRANSACT_GET_USER_QUOTA_LIST_START:
2140 if (qt_handle->quota_list==NULL &&
2141 qt_handle->tmp_list==NULL) {
2145 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2146 reply_doserror(req, ERRSRV, ERRerror);
2150 /* Realloc the size of parameters and data we will return */
2152 params = nttrans_realloc(ppparams, param_len);
2153 if(params == NULL) {
2154 reply_doserror(req, ERRDOS, ERRnomem);
2158 /* we should not trust the value in max_data_count*/
2159 max_data_count = MIN(max_data_count,2048);
2161 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2163 reply_doserror(req, ERRDOS, ERRnomem);
2169 /* set params Size of returned Quota Data 4 bytes*/
2170 /* but set it later when we know it */
2172 /* for each entry push the data */
2175 qt_handle->tmp_list = qt_handle->quota_list;
2178 tmp_list = qt_handle->tmp_list;
2180 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2181 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2183 sid_len = ndr_size_dom_sid(
2184 &tmp_list->quotas->sid, 0);
2185 entry_len = 40 + sid_len;
2187 /* nextoffset entry 4 bytes */
2188 SIVAL(entry,0,entry_len);
2190 /* then the len of the SID 4 bytes */
2191 SIVAL(entry,4,sid_len);
2193 /* unknown data 8 bytes SMB_BIG_UINT */
2194 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-metze*/
2196 /* the used disk space 8 bytes SMB_BIG_UINT */
2197 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2199 /* the soft quotas 8 bytes SMB_BIG_UINT */
2200 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2202 /* the hard quotas 8 bytes SMB_BIG_UINT */
2203 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2205 /* and now the SID */
2206 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2209 qt_handle->tmp_list = tmp_list;
2211 /* overwrite the offset of the last entry */
2212 SIVAL(entry-entry_len,0,0);
2214 data_len = 4+qt_len;
2215 /* overwrite the params quota_data_len */
2216 SIVAL(params,0,data_len);
2220 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2222 /* unknown 4 bytes IVAL(pdata,0) */
2224 if (data_count < 8) {
2225 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2226 reply_doserror(req, ERRDOS, ERRunknownlevel);
2230 sid_len = IVAL(pdata,4);
2231 /* Ensure this is less than 1mb. */
2232 if (sid_len > (1024*1024)) {
2233 reply_doserror(req, ERRDOS, ERRnomem);
2237 if (data_count < 8+sid_len) {
2238 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2239 reply_doserror(req, ERRDOS, ERRunknownlevel);
2243 data_len = 4+40+sid_len;
2245 if (max_data_count < data_len) {
2246 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2247 max_data_count, data_len));
2249 SIVAL(params,0,data_len);
2251 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2255 sid_parse(pdata+8,sid_len,&sid);
2257 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2260 * we have to return zero's in all fields
2261 * instead of returning an error here
2266 /* Realloc the size of parameters and data we will return */
2268 params = nttrans_realloc(ppparams, param_len);
2269 if(params == NULL) {
2270 reply_doserror(req, ERRDOS, ERRnomem);
2274 pdata = nttrans_realloc(ppdata, data_len);
2276 reply_doserror(req, ERRDOS, ERRnomem);
2282 /* set params Size of returned Quota Data 4 bytes*/
2283 SIVAL(params,0,data_len);
2285 /* nextoffset entry 4 bytes */
2288 /* then the len of the SID 4 bytes */
2289 SIVAL(entry,4,sid_len);
2291 /* unknown data 8 bytes SMB_BIG_UINT */
2292 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-mezte*/
2294 /* the used disk space 8 bytes SMB_BIG_UINT */
2295 SBIG_UINT(entry,16,qt.usedspace);
2297 /* the soft quotas 8 bytes SMB_BIG_UINT */
2298 SBIG_UINT(entry,24,qt.softlim);
2300 /* the hard quotas 8 bytes SMB_BIG_UINT */
2301 SBIG_UINT(entry,32,qt.hardlim);
2303 /* and now the SID */
2304 sid_linearize(entry+40, sid_len, &sid);
2309 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2310 reply_doserror(req, ERRSRV, ERRerror);
2315 send_nt_replies(conn, req, nt_status, params, param_len,
2319 /****************************************************************************
2320 Reply to set user quota
2321 ****************************************************************************/
2323 static void call_nt_transact_set_user_quota(connection_struct *conn,
2324 struct smb_request *req,
2328 uint32 parameter_count,
2331 uint32 max_data_count)
2333 char *params = *ppparams;
2334 char *pdata = *ppdata;
2335 int data_len=0,param_len=0;
2336 SMB_NTQUOTA_STRUCT qt;
2339 files_struct *fsp = NULL;
2344 if (current_user.ut.uid != 0) {
2345 DEBUG(1,("set_user_quota: access_denied service [%s] user [%s]\n",
2346 lp_servicename(SNUM(conn)),conn->user));
2347 reply_doserror(req, ERRDOS, ERRnoaccess);
2352 * Ensure minimum number of parameters sent.
2355 if (parameter_count < 2) {
2356 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2357 reply_doserror(req, ERRDOS, ERRinvalidparam);
2361 /* maybe we can check the quota_fnum */
2362 fsp = file_fsp(SVAL(params,0));
2363 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2364 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2365 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2369 if (data_count < 40) {
2370 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2371 reply_doserror(req, ERRDOS, ERRunknownlevel);
2375 /* offset to next quota record.
2376 * 4 bytes IVAL(pdata,0)
2381 sid_len = IVAL(pdata,4);
2383 if (data_count < 40+sid_len) {
2384 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2385 reply_doserror(req, ERRDOS, ERRunknownlevel);
2389 /* unknown 8 bytes in pdata
2390 * maybe its the change time in NTTIME
2393 /* the used space 8 bytes (SMB_BIG_UINT)*/
2394 qt.usedspace = (SMB_BIG_UINT)IVAL(pdata,16);
2395 #ifdef LARGE_SMB_OFF_T
2396 qt.usedspace |= (((SMB_BIG_UINT)IVAL(pdata,20)) << 32);
2397 #else /* LARGE_SMB_OFF_T */
2398 if ((IVAL(pdata,20) != 0)&&
2399 ((qt.usedspace != 0xFFFFFFFF)||
2400 (IVAL(pdata,20)!=0xFFFFFFFF))) {
2401 /* more than 32 bits? */
2402 reply_doserror(req, ERRDOS, ERRunknownlevel);
2405 #endif /* LARGE_SMB_OFF_T */
2407 /* the soft quotas 8 bytes (SMB_BIG_UINT)*/
2408 qt.softlim = (SMB_BIG_UINT)IVAL(pdata,24);
2409 #ifdef LARGE_SMB_OFF_T
2410 qt.softlim |= (((SMB_BIG_UINT)IVAL(pdata,28)) << 32);
2411 #else /* LARGE_SMB_OFF_T */
2412 if ((IVAL(pdata,28) != 0)&&
2413 ((qt.softlim != 0xFFFFFFFF)||
2414 (IVAL(pdata,28)!=0xFFFFFFFF))) {
2415 /* more than 32 bits? */
2416 reply_doserror(req, ERRDOS, ERRunknownlevel);
2419 #endif /* LARGE_SMB_OFF_T */
2421 /* the hard quotas 8 bytes (SMB_BIG_UINT)*/
2422 qt.hardlim = (SMB_BIG_UINT)IVAL(pdata,32);
2423 #ifdef LARGE_SMB_OFF_T
2424 qt.hardlim |= (((SMB_BIG_UINT)IVAL(pdata,36)) << 32);
2425 #else /* LARGE_SMB_OFF_T */
2426 if ((IVAL(pdata,36) != 0)&&
2427 ((qt.hardlim != 0xFFFFFFFF)||
2428 (IVAL(pdata,36)!=0xFFFFFFFF))) {
2429 /* more than 32 bits? */
2430 reply_doserror(req, ERRDOS, ERRunknownlevel);
2433 #endif /* LARGE_SMB_OFF_T */
2435 sid_parse(pdata+40,sid_len,&sid);
2436 DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid)));
2438 /* 44 unknown bytes left... */
2440 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2441 reply_doserror(req, ERRSRV, ERRerror);
2445 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2448 #endif /* HAVE_SYS_QUOTAS */
2450 static void handle_nttrans(connection_struct *conn,
2451 struct trans_state *state,
2452 struct smb_request *req)
2454 if (Protocol >= PROTOCOL_NT1) {
2455 req->flags2 |= 0x40; /* IS_LONG_NAME */
2456 SSVAL(req->inbuf,smb_flg2,req->flags2);
2459 /* Now we must call the relevant NT_TRANS function */
2460 switch(state->call) {
2461 case NT_TRANSACT_CREATE:
2463 START_PROFILE(NT_transact_create);
2464 call_nt_transact_create(
2466 &state->setup, state->setup_count,
2467 &state->param, state->total_param,
2468 &state->data, state->total_data,
2469 state->max_data_return);
2470 END_PROFILE(NT_transact_create);
2474 case NT_TRANSACT_IOCTL:
2476 START_PROFILE(NT_transact_ioctl);
2477 call_nt_transact_ioctl(
2479 &state->setup, state->setup_count,
2480 &state->param, state->total_param,
2481 &state->data, state->total_data,
2482 state->max_data_return);
2483 END_PROFILE(NT_transact_ioctl);
2487 case NT_TRANSACT_SET_SECURITY_DESC:
2489 START_PROFILE(NT_transact_set_security_desc);
2490 call_nt_transact_set_security_desc(
2492 &state->setup, state->setup_count,
2493 &state->param, state->total_param,
2494 &state->data, state->total_data,
2495 state->max_data_return);
2496 END_PROFILE(NT_transact_set_security_desc);
2500 case NT_TRANSACT_NOTIFY_CHANGE:
2502 START_PROFILE(NT_transact_notify_change);
2503 call_nt_transact_notify_change(
2505 &state->setup, state->setup_count,
2506 &state->param, state->total_param,
2507 &state->data, state->total_data,
2508 state->max_data_return,
2509 state->max_param_return);
2510 END_PROFILE(NT_transact_notify_change);
2514 case NT_TRANSACT_RENAME:
2516 START_PROFILE(NT_transact_rename);
2517 call_nt_transact_rename(
2519 &state->setup, state->setup_count,
2520 &state->param, state->total_param,
2521 &state->data, state->total_data,
2522 state->max_data_return);
2523 END_PROFILE(NT_transact_rename);
2527 case NT_TRANSACT_QUERY_SECURITY_DESC:
2529 START_PROFILE(NT_transact_query_security_desc);
2530 call_nt_transact_query_security_desc(
2532 &state->setup, state->setup_count,
2533 &state->param, state->total_param,
2534 &state->data, state->total_data,
2535 state->max_data_return);
2536 END_PROFILE(NT_transact_query_security_desc);
2540 #ifdef HAVE_SYS_QUOTAS
2541 case NT_TRANSACT_GET_USER_QUOTA:
2543 START_PROFILE(NT_transact_get_user_quota);
2544 call_nt_transact_get_user_quota(
2546 &state->setup, state->setup_count,
2547 &state->param, state->total_param,
2548 &state->data, state->total_data,
2549 state->max_data_return);
2550 END_PROFILE(NT_transact_get_user_quota);
2554 case NT_TRANSACT_SET_USER_QUOTA:
2556 START_PROFILE(NT_transact_set_user_quota);
2557 call_nt_transact_set_user_quota(
2559 &state->setup, state->setup_count,
2560 &state->param, state->total_param,
2561 &state->data, state->total_data,
2562 state->max_data_return);
2563 END_PROFILE(NT_transact_set_user_quota);
2566 #endif /* HAVE_SYS_QUOTAS */
2569 /* Error in request */
2570 DEBUG(0,("handle_nttrans: Unknown request %d in "
2571 "nttrans call\n", state->call));
2572 reply_doserror(req, ERRSRV, ERRerror);
2578 /****************************************************************************
2579 Reply to a SMBNTtrans.
2580 ****************************************************************************/
2582 void reply_nttrans(struct smb_request *req)
2584 connection_struct *conn = req->conn;
2589 uint16 function_code;
2591 struct trans_state *state;
2594 START_PROFILE(SMBnttrans);
2596 if (req->wct < 19) {
2597 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2598 END_PROFILE(SMBnttrans);
2602 size = smb_len(req->inbuf) + 4;
2603 pscnt = IVAL(req->inbuf,smb_nt_ParameterCount);
2604 psoff = IVAL(req->inbuf,smb_nt_ParameterOffset);
2605 dscnt = IVAL(req->inbuf,smb_nt_DataCount);
2606 dsoff = IVAL(req->inbuf,smb_nt_DataOffset);
2607 function_code = SVAL(req->inbuf, smb_nt_Function);
2609 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2610 reply_doserror(req, ERRSRV, ERRaccess);
2611 END_PROFILE(SMBnttrans);
2615 result = allow_new_trans(conn->pending_trans, req->mid);
2616 if (!NT_STATUS_IS_OK(result)) {
2617 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
2618 reply_nterror(req, result);
2619 END_PROFILE(SMBnttrans);
2623 if ((state = TALLOC_P(conn->mem_ctx, struct trans_state)) == NULL) {
2624 reply_doserror(req, ERRSRV, ERRaccess);
2625 END_PROFILE(SMBnttrans);
2629 state->cmd = SMBnttrans;
2631 state->mid = req->mid;
2632 state->vuid = req->vuid;
2633 state->total_data = IVAL(req->inbuf, smb_nt_TotalDataCount);
2635 state->total_param = IVAL(req->inbuf, smb_nt_TotalParameterCount);
2636 state->param = NULL;
2637 state->max_data_return = IVAL(req->inbuf,smb_nt_MaxDataCount);
2638 state->max_param_return = IVAL(req->inbuf,smb_nt_MaxParameterCount);
2640 /* setup count is in *words* */
2641 state->setup_count = 2*CVAL(req->inbuf,smb_nt_SetupCount);
2642 state->setup = NULL;
2643 state->call = function_code;
2646 * All nttrans messages we handle have smb_wct == 19 +
2647 * state->setup_count. Ensure this is so as a sanity check.
2650 if(req->wct != 19 + (state->setup_count/2)) {
2651 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2652 req->wct, 19 + (state->setup_count/2)));
2656 /* Don't allow more than 128mb for each value. */
2657 if ((state->total_data > (1024*1024*128)) ||
2658 (state->total_param > (1024*1024*128))) {
2659 reply_doserror(req, ERRDOS, ERRnomem);
2660 END_PROFILE(SMBnttrans);
2664 if ((dscnt > state->total_data) || (pscnt > state->total_param))
2667 if (state->total_data) {
2668 /* Can't use talloc here, the core routines do realloc on the
2669 * params and data. */
2670 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
2671 DEBUG(0,("reply_nttrans: data malloc fail for %u "
2672 "bytes !\n", (unsigned int)state->total_data));
2674 reply_doserror(req, ERRDOS, ERRnomem);
2675 END_PROFILE(SMBnttrans);
2678 if ((dsoff+dscnt < dsoff) || (dsoff+dscnt < dscnt))
2680 if ((smb_base(req->inbuf)+dsoff+dscnt
2681 > (char *)req->inbuf + size) ||
2682 (smb_base(req->inbuf)+dsoff+dscnt < smb_base(req->inbuf)))
2685 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
2688 if (state->total_param) {
2689 /* Can't use talloc here, the core routines do realloc on the
2690 * params and data. */
2691 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
2692 DEBUG(0,("reply_nttrans: param malloc fail for %u "
2693 "bytes !\n", (unsigned int)state->total_param));
2694 SAFE_FREE(state->data);
2696 reply_doserror(req, ERRDOS, ERRnomem);
2697 END_PROFILE(SMBnttrans);
2700 if ((psoff+pscnt < psoff) || (psoff+pscnt < pscnt))
2702 if ((smb_base(req->inbuf)+psoff+pscnt
2703 > (char *)req->inbuf + size) ||
2704 (smb_base(req->inbuf)+psoff+pscnt < smb_base(req->inbuf)))
2707 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
2710 state->received_data = dscnt;
2711 state->received_param = pscnt;
2713 if(state->setup_count > 0) {
2714 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2715 state->setup_count));
2716 state->setup = (uint16 *)TALLOC(state, state->setup_count);
2717 if (state->setup == NULL) {
2718 DEBUG(0,("reply_nttrans : Out of memory\n"));
2719 SAFE_FREE(state->data);
2720 SAFE_FREE(state->param);
2722 reply_doserror(req, ERRDOS, ERRnomem);
2723 END_PROFILE(SMBnttrans);
2727 if ((smb_nt_SetupStart + state->setup_count < smb_nt_SetupStart) ||
2728 (smb_nt_SetupStart + state->setup_count < state->setup_count)) {
2731 if (smb_nt_SetupStart + state->setup_count > size) {
2735 memcpy( state->setup, &req->inbuf[smb_nt_SetupStart],
2736 state->setup_count);
2737 dump_data(10, (uint8 *)state->setup, state->setup_count);
2740 if ((state->received_data == state->total_data) &&
2741 (state->received_param == state->total_param)) {
2742 handle_nttrans(conn, state, req);
2743 SAFE_FREE(state->param);
2744 SAFE_FREE(state->data);
2746 END_PROFILE(SMBnttrans);
2750 DLIST_ADD(conn->pending_trans, state);
2752 /* We need to send an interim response then receive the rest
2753 of the parameter/data bytes */
2754 reply_outbuf(req, 0, 0);
2755 show_msg((char *)req->outbuf);
2756 END_PROFILE(SMBnttrans);
2761 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
2762 SAFE_FREE(state->data);
2763 SAFE_FREE(state->param);
2765 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2766 END_PROFILE(SMBnttrans);
2770 /****************************************************************************
2771 Reply to a SMBnttranss
2772 ****************************************************************************/
2774 void reply_nttranss(struct smb_request *req)
2776 connection_struct *conn = req->conn;
2777 unsigned int pcnt,poff,dcnt,doff,pdisp,ddisp;
2778 struct trans_state *state;
2782 START_PROFILE(SMBnttranss);
2784 show_msg((char *)req->inbuf);
2786 if (req->wct < 18) {
2787 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2788 END_PROFILE(SMBnttranss);
2792 for (state = conn->pending_trans; state != NULL;
2793 state = state->next) {
2794 if (state->mid == req->mid) {
2799 if ((state == NULL) || (state->cmd != SMBnttrans)) {
2800 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2801 END_PROFILE(SMBnttranss);
2805 /* Revise state->total_param and state->total_data in case they have
2806 changed downwards */
2807 if (IVAL(req->inbuf, smb_nts_TotalParameterCount)
2808 < state->total_param) {
2809 state->total_param = IVAL(req->inbuf,
2810 smb_nts_TotalParameterCount);
2812 if (IVAL(req->inbuf, smb_nts_TotalDataCount) < state->total_data) {
2813 state->total_data = IVAL(req->inbuf, smb_nts_TotalDataCount);
2816 size = smb_len(req->inbuf) + 4;
2818 pcnt = IVAL(req->inbuf,smb_nts_ParameterCount);
2819 poff = IVAL(req->inbuf, smb_nts_ParameterOffset);
2820 pdisp = IVAL(req->inbuf, smb_nts_ParameterDisplacement);
2822 dcnt = IVAL(req->inbuf, smb_nts_DataCount);
2823 ddisp = IVAL(req->inbuf, smb_nts_DataDisplacement);
2824 doff = IVAL(req->inbuf, smb_nts_DataOffset);
2826 state->received_param += pcnt;
2827 state->received_data += dcnt;
2829 if ((state->received_data > state->total_data) ||
2830 (state->received_param > state->total_param))
2834 if (pdisp+pcnt > state->total_param)
2836 if ((pdisp+pcnt < pdisp) || (pdisp+pcnt < pcnt))
2838 if (pdisp > state->total_param)
2840 if ((smb_base(req->inbuf) + poff + pcnt
2841 > (char *)req->inbuf + size) ||
2842 (smb_base(req->inbuf) + poff + pcnt
2843 < smb_base(req->inbuf)))
2845 if (state->param + pdisp < state->param)
2848 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,
2853 if (ddisp+dcnt > state->total_data)
2855 if ((ddisp+dcnt < ddisp) || (ddisp+dcnt < dcnt))
2857 if (ddisp > state->total_data)
2859 if ((smb_base(req->inbuf) + doff + dcnt
2860 > (char *)req->inbuf + size) ||
2861 (smb_base(req->inbuf) + doff + dcnt
2862 < smb_base(req->inbuf)))
2864 if (state->data + ddisp < state->data)
2867 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,
2871 if ((state->received_param < state->total_param) ||
2872 (state->received_data < state->total_data)) {
2873 END_PROFILE(SMBnttranss);
2878 * construct_reply_common will copy smb_com from inbuf to
2879 * outbuf. SMBnttranss is wrong here.
2881 SCVAL(req->inbuf,smb_com,SMBnttrans);
2883 handle_nttrans(conn, state, req);
2885 DLIST_REMOVE(conn->pending_trans, state);
2886 SAFE_FREE(state->data);
2887 SAFE_FREE(state->param);
2889 END_PROFILE(SMBnttranss);
2894 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
2895 DLIST_REMOVE(conn->pending_trans, state);
2896 SAFE_FREE(state->data);
2897 SAFE_FREE(state->param);
2899 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2900 END_PROFILE(SMBnttranss);