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, fsp->fh->fd,
773 security_info_sent, psd);
776 status = SMB_VFS_SET_NT_ACL(fsp, fsp->fsp_name,
777 security_info_sent, psd);
785 /****************************************************************************
786 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
787 ****************************************************************************/
789 static struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
791 struct ea_list *ea_list_head = NULL;
798 while (offset + 4 <= data_size) {
799 size_t next_offset = IVAL(pdata,offset);
800 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
806 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
807 if (next_offset == 0) {
810 offset += next_offset;
816 /****************************************************************************
817 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
818 ****************************************************************************/
820 static void call_nt_transact_create(connection_struct *conn,
821 struct smb_request *req,
822 uint16 **ppsetup, uint32 setup_count,
823 char **ppparams, uint32 parameter_count,
824 char **ppdata, uint32 data_count,
825 uint32 max_data_count)
828 char *params = *ppparams;
829 char *data = *ppdata;
830 /* Breakout the oplock request bits so we can set the reply bits separately. */
832 SMB_OFF_T file_len = 0;
833 SMB_STRUCT_STAT sbuf;
835 files_struct *fsp = NULL;
839 uint32 file_attributes;
841 uint32 create_disposition;
842 uint32 create_options;
844 struct security_descriptor *sd = NULL;
847 struct timespec c_timespec;
848 struct timespec a_timespec;
849 struct timespec m_timespec;
850 struct ea_list *ea_list = NULL;
853 SMB_BIG_UINT allocation_size;
855 uint8_t oplock_granted;
856 TALLOC_CTX *ctx = talloc_tos();
858 DEBUG(5,("call_nt_transact_create\n"));
861 * If it's an IPC, use the pipe handler.
865 if (lp_nt_pipe_support()) {
866 do_nt_transact_create_pipe(
868 ppsetup, setup_count,
869 ppparams, parameter_count,
873 reply_doserror(req, ERRDOS, ERRnoaccess);
879 * Ensure minimum number of parameters sent.
882 if(parameter_count < 54) {
883 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
884 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
888 flags = IVAL(params,0);
889 access_mask = IVAL(params,8);
890 file_attributes = IVAL(params,20);
891 share_access = IVAL(params,24);
892 create_disposition = IVAL(params,28);
893 create_options = IVAL(params,32);
894 sd_len = IVAL(params,36);
895 ea_len = IVAL(params,40);
896 root_dir_fid = (uint16)IVAL(params,4);
897 allocation_size = (SMB_BIG_UINT)IVAL(params,12);
898 #ifdef LARGE_SMB_OFF_T
899 allocation_size |= (((SMB_BIG_UINT)IVAL(params,16)) << 32);
902 /* Ensure the data_len is correct for the sd and ea values given. */
903 if ((ea_len + sd_len > data_count)
904 || (ea_len > data_count) || (sd_len > data_count)
905 || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
906 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
907 "%u, data_count = %u\n", (unsigned int)ea_len,
908 (unsigned int)sd_len, (unsigned int)data_count));
909 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
914 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
917 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
919 if (!NT_STATUS_IS_OK(status)) {
920 DEBUG(10, ("call_nt_transact_create: "
921 "unmarshall_sec_desc failed: %s\n",
923 reply_nterror(req, status);
929 if (!lp_ea_support(SNUM(conn))) {
930 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
931 "EA's not supported.\n",
932 (unsigned int)ea_len));
933 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
938 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
939 "too small (should be more than 10)\n",
940 (unsigned int)ea_len ));
941 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
945 /* We have already checked that ea_len <= data_count here. */
946 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
948 if (ea_list == NULL) {
949 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
954 srvstr_get_path(ctx, params, req->flags2, &fname,
955 params+53, parameter_count-53,
956 STR_TERMINATE, &status);
957 if (!NT_STATUS_IS_OK(status)) {
958 reply_nterror(req, status);
962 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
963 if (oplock_request) {
964 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
968 status = create_file(conn, req, root_dir_fid, fname,
969 access_mask, share_access, create_disposition,
970 create_options, file_attributes, oplock_request,
971 allocation_size, sd, ea_list, &fsp, &info, &sbuf);
973 if(!NT_STATUS_IS_OK(status)) {
974 if (open_was_deferred(req->mid)) {
975 /* We have re-scheduled this call, no error. */
978 reply_openerror(req, status);
983 * If the caller set the extended oplock request bit
984 * and we granted one (by whatever means) - set the
985 * correct bit for extended oplock reply.
988 if (oplock_request &&
989 (lp_fake_oplocks(SNUM(conn))
990 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
993 * Exclusive oplock granted
996 if (flags & REQUEST_BATCH_OPLOCK) {
997 oplock_granted = BATCH_OPLOCK_RETURN;
999 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
1001 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1002 oplock_granted = LEVEL_II_OPLOCK_RETURN;
1004 oplock_granted = NO_OPLOCK_RETURN;
1007 file_len = sbuf.st_size;
1008 fattr = dos_mode(conn,fname,&sbuf);
1010 fattr = FILE_ATTRIBUTE_NORMAL;
1013 /* Realloc the size of parameters and data we will return */
1014 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1015 /* Extended response is 32 more byyes. */
1020 params = nttrans_realloc(ppparams, param_len);
1021 if(params == NULL) {
1022 reply_doserror(req, ERRDOS, ERRnomem);
1027 SCVAL(p, 0, oplock_granted);
1030 SSVAL(p,0,fsp->fnum);
1032 if ((create_disposition == FILE_SUPERSEDE)
1033 && (info == FILE_WAS_OVERWRITTEN)) {
1034 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1041 c_timespec = get_create_timespec(
1042 &sbuf,lp_fake_dir_create_times(SNUM(conn)));
1043 a_timespec = get_atimespec(&sbuf);
1044 m_timespec = get_mtimespec(&sbuf);
1046 if (lp_dos_filetime_resolution(SNUM(conn))) {
1047 dos_filetime_timespec(&c_timespec);
1048 dos_filetime_timespec(&a_timespec);
1049 dos_filetime_timespec(&m_timespec);
1052 put_long_date_timespec(p, c_timespec); /* create time. */
1054 put_long_date_timespec(p, a_timespec); /* access time */
1056 put_long_date_timespec(p, m_timespec); /* write time */
1058 put_long_date_timespec(p, m_timespec); /* change time */
1060 SIVAL(p,0,fattr); /* File Attributes. */
1062 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
1064 SOFF_T(p,0,file_len);
1066 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1070 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1072 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1075 if (fsp->is_directory
1076 || can_write_to_file(conn, fname, &sbuf)) {
1077 perms = FILE_GENERIC_ALL;
1079 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1084 DEBUG(5,("call_nt_transact_create: open name = %s\n", fname));
1086 /* Send the required number of replies */
1087 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1092 /****************************************************************************
1093 Reply to a NT CANCEL request.
1094 conn POINTER CAN BE NULL HERE !
1095 ****************************************************************************/
1097 void reply_ntcancel(struct smb_request *req)
1100 * Go through and cancel any pending change notifies.
1103 START_PROFILE(SMBntcancel);
1104 remove_pending_change_notify_requests_by_mid(req->mid);
1105 remove_pending_lock_requests_by_mid(req->mid);
1106 srv_cancel_sign_response(req->mid);
1108 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", req->mid));
1110 END_PROFILE(SMBntcancel);
1114 /****************************************************************************
1116 ****************************************************************************/
1118 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
1119 connection_struct *conn,
1120 struct smb_request *req,
1121 const char *oldname_in,
1122 const char *newname_in,
1125 SMB_STRUCT_STAT sbuf1, sbuf2;
1126 char *oldname = NULL;
1127 char *newname = NULL;
1128 char *last_component_oldname = NULL;
1129 char *last_component_newname = NULL;
1130 files_struct *fsp1,*fsp2;
1134 NTSTATUS status = NT_STATUS_OK;
1139 if (!CAN_WRITE(conn)) {
1140 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1143 status = unix_convert(ctx, conn, oldname_in, False, &oldname,
1144 &last_component_oldname, &sbuf1);
1145 if (!NT_STATUS_IS_OK(status)) {
1149 status = check_name(conn, oldname);
1150 if (!NT_STATUS_IS_OK(status)) {
1154 /* Source must already exist. */
1155 if (!VALID_STAT(sbuf1)) {
1156 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1158 /* Ensure attributes match. */
1159 fattr = dos_mode(conn,oldname,&sbuf1);
1160 if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
1161 return NT_STATUS_NO_SUCH_FILE;
1164 status = unix_convert(ctx, conn, newname_in, False, &newname,
1165 &last_component_newname, &sbuf2);
1166 if (!NT_STATUS_IS_OK(status)) {
1170 status = check_name(conn, newname);
1171 if (!NT_STATUS_IS_OK(status)) {
1175 /* Disallow if newname already exists. */
1176 if (VALID_STAT(sbuf2)) {
1177 return NT_STATUS_OBJECT_NAME_COLLISION;
1180 /* No links from a directory. */
1181 if (S_ISDIR(sbuf1.st_mode)) {
1182 return NT_STATUS_FILE_IS_A_DIRECTORY;
1185 /* Ensure this is within the share. */
1186 status = check_reduced_name(conn, oldname);
1187 if (!NT_STATUS_IS_OK(status)) {
1191 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1194 status = open_file_ntcreate(conn, req, oldname, &sbuf1,
1195 FILE_READ_DATA, /* Read-only. */
1196 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1198 0, /* No create options. */
1199 FILE_ATTRIBUTE_NORMAL,
1203 if (!NT_STATUS_IS_OK(status)) {
1207 status = open_file_ntcreate(conn, req, newname, &sbuf2,
1208 FILE_WRITE_DATA, /* Read-only. */
1209 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1211 0, /* No create options. */
1216 if (!NT_STATUS_IS_OK(status)) {
1217 close_file(fsp1,ERROR_CLOSE);
1221 if (sbuf1.st_size) {
1222 ret = vfs_transfer_file(fsp1, fsp2, sbuf1.st_size);
1226 * As we are opening fsp1 read-only we only expect
1227 * an error on close on fsp2 if we are out of space.
1228 * Thus we don't look at the error return from the
1231 close_file(fsp1,NORMAL_CLOSE);
1233 /* Ensure the modtime is set correctly on the destination file. */
1234 fsp_set_pending_modtime(fsp2, get_mtimespec(&sbuf1));
1236 status = close_file(fsp2,NORMAL_CLOSE);
1238 /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
1239 creates the file. This isn't the correct thing to do in the copy
1241 file_set_dosmode(conn, newname, fattr, &sbuf2,
1242 parent_dirname(newname),false);
1244 if (ret < (SMB_OFF_T)sbuf1.st_size) {
1245 return NT_STATUS_DISK_FULL;
1248 if (!NT_STATUS_IS_OK(status)) {
1249 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1250 nt_errstr(status), oldname, newname));
1255 /****************************************************************************
1256 Reply to a NT rename request.
1257 ****************************************************************************/
1259 void reply_ntrename(struct smb_request *req)
1261 connection_struct *conn = req->conn;
1262 char *oldname = NULL;
1263 char *newname = NULL;
1266 bool src_has_wcard = False;
1267 bool dest_has_wcard = False;
1270 TALLOC_CTX *ctx = talloc_tos();
1272 START_PROFILE(SMBntrename);
1275 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1276 END_PROFILE(SMBntrename);
1280 attrs = SVAL(req->inbuf,smb_vwv0);
1281 rename_type = SVAL(req->inbuf,smb_vwv1);
1283 p = smb_buf(req->inbuf) + 1;
1284 p += srvstr_get_path_wcard(ctx, (char *)req->inbuf, req->flags2, &oldname, p,
1285 0, STR_TERMINATE, &status,
1287 if (!NT_STATUS_IS_OK(status)) {
1288 reply_nterror(req, status);
1289 END_PROFILE(SMBntrename);
1293 if( is_ntfs_stream_name(oldname)) {
1294 /* Can't rename a stream. */
1295 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1296 END_PROFILE(SMBntrename);
1300 if (ms_has_wild(oldname)) {
1301 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1302 END_PROFILE(SMBntrename);
1307 p += srvstr_get_path_wcard(ctx, (char *)req->inbuf, req->flags2, &newname, p,
1308 0, STR_TERMINATE, &status,
1310 if (!NT_STATUS_IS_OK(status)) {
1311 reply_nterror(req, status);
1312 END_PROFILE(SMBntrename);
1316 status = resolve_dfspath(ctx, conn,
1317 req->flags2 & FLAGS2_DFS_PATHNAMES,
1320 if (!NT_STATUS_IS_OK(status)) {
1321 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1322 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1323 ERRSRV, ERRbadpath);
1324 END_PROFILE(SMBntrename);
1327 reply_nterror(req, status);
1328 END_PROFILE(SMBntrename);
1332 status = resolve_dfspath(ctx, conn,
1333 req->flags2 & FLAGS2_DFS_PATHNAMES,
1336 if (!NT_STATUS_IS_OK(status)) {
1337 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1338 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1339 ERRSRV, ERRbadpath);
1340 END_PROFILE(SMBntrename);
1343 reply_nterror(req, status);
1344 END_PROFILE(SMBntrename);
1348 DEBUG(3,("reply_ntrename : %s -> %s\n",oldname,newname));
1350 switch(rename_type) {
1351 case RENAME_FLAG_RENAME:
1352 status = rename_internals(ctx, conn, req, oldname,
1353 newname, attrs, False, src_has_wcard,
1356 case RENAME_FLAG_HARD_LINK:
1357 if (src_has_wcard || dest_has_wcard) {
1359 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1361 status = hardlink_internals(ctx,
1367 case RENAME_FLAG_COPY:
1368 if (src_has_wcard || dest_has_wcard) {
1370 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1372 status = copy_internals(ctx, conn, req, oldname,
1376 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1377 status = NT_STATUS_INVALID_PARAMETER;
1380 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1384 if (!NT_STATUS_IS_OK(status)) {
1385 if (open_was_deferred(req->mid)) {
1386 /* We have re-scheduled this call. */
1387 END_PROFILE(SMBntrename);
1391 reply_nterror(req, status);
1392 END_PROFILE(SMBntrename);
1396 reply_outbuf(req, 0, 0);
1398 END_PROFILE(SMBntrename);
1402 /****************************************************************************
1403 Reply to a notify change - queue the request and
1404 don't allow a directory to be opened.
1405 ****************************************************************************/
1407 static void call_nt_transact_notify_change(connection_struct *conn,
1408 struct smb_request *req,
1412 uint32 parameter_count,
1413 char **ppdata, uint32 data_count,
1414 uint32 max_data_count,
1415 uint32 max_param_count)
1417 uint16 *setup = *ppsetup;
1423 if(setup_count < 6) {
1424 reply_doserror(req, ERRDOS, ERRbadfunc);
1428 fsp = file_fsp(SVAL(setup,4));
1429 filter = IVAL(setup, 0);
1430 recursive = (SVAL(setup, 6) != 0) ? True : False;
1432 DEBUG(3,("call_nt_transact_notify_change\n"));
1435 reply_doserror(req, ERRDOS, ERRbadfid);
1440 char *filter_string;
1442 if (!(filter_string = notify_filter_string(NULL, filter))) {
1443 reply_nterror(req,NT_STATUS_NO_MEMORY);
1447 DEBUG(3,("call_nt_transact_notify_change: notify change "
1448 "called on %s, filter = %s, recursive = %d\n",
1449 fsp->fsp_name, filter_string, recursive));
1451 TALLOC_FREE(filter_string);
1454 if((!fsp->is_directory) || (conn != fsp->conn)) {
1455 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1459 if (fsp->notify == NULL) {
1461 status = change_notify_create(fsp, filter, recursive);
1463 if (!NT_STATUS_IS_OK(status)) {
1464 DEBUG(10, ("change_notify_create returned %s\n",
1465 nt_errstr(status)));
1466 reply_nterror(req, status);
1471 if (fsp->notify->num_changes != 0) {
1474 * We've got changes pending, respond immediately
1478 * TODO: write a torture test to check the filtering behaviour
1482 change_notify_reply(fsp->conn, req->inbuf, max_param_count, fsp->notify);
1485 * change_notify_reply() above has independently sent its
1492 * No changes pending, queue the request
1495 status = change_notify_add_request(req,
1499 if (!NT_STATUS_IS_OK(status)) {
1500 reply_nterror(req, status);
1505 /****************************************************************************
1506 Reply to an NT transact rename command.
1507 ****************************************************************************/
1509 static void call_nt_transact_rename(connection_struct *conn,
1510 struct smb_request *req,
1511 uint16 **ppsetup, uint32 setup_count,
1512 char **ppparams, uint32 parameter_count,
1513 char **ppdata, uint32 data_count,
1514 uint32 max_data_count)
1516 char *params = *ppparams;
1517 char *new_name = NULL;
1518 files_struct *fsp = NULL;
1519 bool replace_if_exists = False;
1520 bool dest_has_wcard = False;
1522 TALLOC_CTX *ctx = talloc_tos();
1524 if(parameter_count < 5) {
1525 reply_doserror(req, ERRDOS, ERRbadfunc);
1529 fsp = file_fsp(SVAL(params, 0));
1530 replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
1531 if (!check_fsp(conn, req, fsp, ¤t_user)) {
1534 srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
1535 parameter_count - 4,
1536 STR_TERMINATE, &status, &dest_has_wcard);
1537 if (!NT_STATUS_IS_OK(status)) {
1538 reply_nterror(req, status);
1542 status = rename_internals(ctx,
1552 if (!NT_STATUS_IS_OK(status)) {
1553 if (open_was_deferred(req->mid)) {
1554 /* We have re-scheduled this call. */
1557 reply_nterror(req, status);
1562 * Rename was successful.
1564 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1566 DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n",
1567 fsp->fsp_name, new_name));
1572 /******************************************************************************
1573 Fake up a completely empty SD.
1574 *******************************************************************************/
1576 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
1580 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1582 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1583 return NT_STATUS_NO_MEMORY;
1586 return NT_STATUS_OK;
1589 /****************************************************************************
1590 Reply to query a security descriptor.
1591 ****************************************************************************/
1593 static void call_nt_transact_query_security_desc(connection_struct *conn,
1594 struct smb_request *req,
1598 uint32 parameter_count,
1601 uint32 max_data_count)
1603 char *params = *ppparams;
1604 char *data = *ppdata;
1605 SEC_DESC *psd = NULL;
1607 uint32 security_info_wanted;
1609 files_struct *fsp = NULL;
1613 if(parameter_count < 8) {
1614 reply_doserror(req, ERRDOS, ERRbadfunc);
1618 fsp = file_fsp(SVAL(params,0));
1620 reply_doserror(req, ERRDOS, ERRbadfid);
1624 security_info_wanted = IVAL(params,4);
1626 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, info_wanted = 0x%x\n", fsp->fsp_name,
1627 (unsigned int)security_info_wanted ));
1629 params = nttrans_realloc(ppparams, 4);
1630 if(params == NULL) {
1631 reply_doserror(req, ERRDOS, ERRnomem);
1635 frame = talloc_stackframe();
1638 * Get the permissions to return.
1641 if (!lp_nt_acl_support(SNUM(conn))) {
1642 status = get_null_nt_acl(talloc_tos(), &psd);
1644 if (fsp->fh->fd != -1) {
1645 status = SMB_VFS_FGET_NT_ACL(
1646 fsp, fsp->fh->fd, security_info_wanted, &psd);
1649 status = SMB_VFS_GET_NT_ACL(
1650 conn, fsp->fsp_name, security_info_wanted, &psd);
1654 if (!NT_STATUS_IS_OK(status)) {
1656 reply_nterror(req, status);
1660 sd_size = ndr_size_security_descriptor(psd, 0);
1662 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
1664 SIVAL(params,0,(uint32)sd_size);
1666 if (max_data_count < sd_size) {
1667 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
1668 params, 4, *ppdata, 0);
1674 * Allocate the data we will point this at.
1677 data = nttrans_realloc(ppdata, sd_size);
1680 reply_doserror(req, ERRDOS, ERRnomem);
1684 status = marshall_sec_desc(talloc_tos(), psd,
1685 &blob.data, &blob.length);
1687 if (!NT_STATUS_IS_OK(status)) {
1689 reply_nterror(req, status);
1693 SMB_ASSERT(sd_size == blob.length);
1694 memcpy(data, blob.data, sd_size);
1696 send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
1702 /****************************************************************************
1703 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
1704 ****************************************************************************/
1706 static void call_nt_transact_set_security_desc(connection_struct *conn,
1707 struct smb_request *req,
1711 uint32 parameter_count,
1714 uint32 max_data_count)
1716 char *params= *ppparams;
1717 char *data = *ppdata;
1718 files_struct *fsp = NULL;
1719 uint32 security_info_sent = 0;
1722 if(parameter_count < 8) {
1723 reply_doserror(req, ERRDOS, ERRbadfunc);
1727 if((fsp = file_fsp(SVAL(params,0))) == NULL) {
1728 reply_doserror(req, ERRDOS, ERRbadfid);
1732 if(!lp_nt_acl_support(SNUM(conn))) {
1736 security_info_sent = IVAL(params,4);
1738 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
1739 (unsigned int)security_info_sent ));
1741 if (data_count == 0) {
1742 reply_doserror(req, ERRDOS, ERRnoaccess);
1746 status = set_sd(fsp, (uint8 *)data, data_count, security_info_sent);
1748 if (!NT_STATUS_IS_OK(status)) {
1749 reply_nterror(req, status);
1754 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1758 /****************************************************************************
1760 ****************************************************************************/
1762 static void call_nt_transact_ioctl(connection_struct *conn,
1763 struct smb_request *req,
1764 uint16 **ppsetup, uint32 setup_count,
1765 char **ppparams, uint32 parameter_count,
1766 char **ppdata, uint32 data_count,
1767 uint32 max_data_count)
1774 static bool logged_message;
1775 char *pdata = *ppdata;
1777 if (setup_count != 8) {
1778 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
1779 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1783 function = IVAL(*ppsetup, 0);
1784 fidnum = SVAL(*ppsetup, 4);
1785 isFSctl = CVAL(*ppsetup, 6);
1786 compfilter = CVAL(*ppsetup, 7);
1788 DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
1789 function, fidnum, isFSctl, compfilter));
1791 fsp=file_fsp(fidnum);
1792 /* this check is done in each implemented function case for now
1793 because I don't want to break anything... --metze
1794 FSP_BELONGS_CONN(fsp,conn);*/
1797 case FSCTL_SET_SPARSE:
1798 /* pretend this succeeded - tho strictly we should
1799 mark the file sparse (if the local fs supports it)
1800 so we can know if we need to pre-allocate or not */
1802 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
1803 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1806 case FSCTL_CREATE_OR_GET_OBJECT_ID:
1808 unsigned char objid[16];
1810 /* This should return the object-id on this file.
1811 * I think I'll make this be the inode+dev. JRA.
1814 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
1816 if (!fsp_belongs_conn(conn, req, fsp, ¤t_user)) {
1821 pdata = nttrans_realloc(ppdata, data_count);
1822 if (pdata == NULL) {
1823 reply_nterror(req, NT_STATUS_NO_MEMORY);
1826 push_file_id_16(pdata, &fsp->file_id);
1827 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
1828 push_file_id_16(pdata+32, &fsp->file_id);
1829 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
1834 case FSCTL_GET_REPARSE_POINT:
1835 /* pretend this fail - my winXP does it like this
1839 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1840 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
1843 case FSCTL_SET_REPARSE_POINT:
1844 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
1848 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1849 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
1852 case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
1855 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
1856 * and return their volume names. If max_data_count is 16, then it is just
1857 * asking for the number of volumes and length of the combined names.
1859 * pdata is the data allocated by our caller, but that uses
1860 * total_data_count (which is 0 in our case) rather than max_data_count.
1861 * Allocate the correct amount and return the pointer to let
1862 * it be deallocated when we return.
1864 SHADOW_COPY_DATA *shadow_data = NULL;
1865 TALLOC_CTX *shadow_mem_ctx = NULL;
1866 bool labels = False;
1867 uint32 labels_data_count = 0;
1871 if (!fsp_belongs_conn(conn, req, fsp, ¤t_user)) {
1875 if (max_data_count < 16) {
1876 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
1878 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1882 if (max_data_count > 16) {
1886 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
1887 if (shadow_mem_ctx == NULL) {
1888 DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
1889 reply_nterror(req, NT_STATUS_NO_MEMORY);
1893 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
1894 if (shadow_data == NULL) {
1895 DEBUG(0,("TALLOC_ZERO() failed!\n"));
1896 talloc_destroy(shadow_mem_ctx);
1897 reply_nterror(req, NT_STATUS_NO_MEMORY);
1901 shadow_data->mem_ctx = shadow_mem_ctx;
1904 * Call the VFS routine to actually do the work.
1906 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
1907 talloc_destroy(shadow_data->mem_ctx);
1908 if (errno == ENOSYS) {
1909 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
1910 conn->connectpath));
1911 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1914 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
1915 conn->connectpath));
1916 reply_nterror(req, NT_STATUS_UNSUCCESSFUL);
1921 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
1926 data_count = 12+labels_data_count+4;
1929 if (max_data_count<data_count) {
1930 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
1931 max_data_count,data_count));
1932 talloc_destroy(shadow_data->mem_ctx);
1933 reply_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
1937 pdata = nttrans_realloc(ppdata, data_count);
1938 if (pdata == NULL) {
1939 talloc_destroy(shadow_data->mem_ctx);
1940 reply_nterror(req, NT_STATUS_NO_MEMORY);
1946 /* num_volumes 4 bytes */
1947 SIVAL(pdata,0,shadow_data->num_volumes);
1950 /* num_labels 4 bytes */
1951 SIVAL(pdata,4,shadow_data->num_volumes);
1954 /* needed_data_count 4 bytes */
1955 SIVAL(pdata,8,labels_data_count);
1959 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
1960 shadow_data->num_volumes,fsp->fsp_name));
1961 if (labels && shadow_data->labels) {
1962 for (i=0;i<shadow_data->num_volumes;i++) {
1963 srvstr_push(pdata, req->flags2,
1964 cur_pdata, shadow_data->labels[i],
1965 2*sizeof(SHADOW_COPY_LABEL),
1966 STR_UNICODE|STR_TERMINATE);
1967 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
1968 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
1972 talloc_destroy(shadow_data->mem_ctx);
1974 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
1980 case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
1982 /* pretend this succeeded -
1984 * we have to send back a list with all files owned by this SID
1986 * but I have to check that --metze
1990 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
1992 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
1994 if (!fsp_belongs_conn(conn, req, fsp, ¤t_user)) {
1998 /* unknown 4 bytes: this is not the length of the sid :-( */
1999 /*unknown = IVAL(pdata,0);*/
2001 sid_parse(pdata+4,sid_len,&sid);
2002 DEBUGADD(10, ("for SID: %s\n", sid_string_dbg(&sid)));
2004 if (!sid_to_uid(&sid, &uid)) {
2005 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2006 sid_string_dbg(&sid),
2007 (unsigned long)sid_len));
2011 /* we can take a look at the find source :-)
2013 * find ./ -uid $uid -name '*' is what we need here
2016 * and send 4bytes len and then NULL terminated unicode strings
2019 * but I don't know how to deal with the paged results
2020 * (maybe we can hang the result anywhere in the fsp struct)
2022 * we don't send all files at once
2023 * and at the next we should *not* start from the beginning,
2024 * so we have to cache the result
2029 /* this works for now... */
2030 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2034 if (!logged_message) {
2035 logged_message = True; /* Only print this once... */
2036 DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
2041 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2045 #ifdef HAVE_SYS_QUOTAS
2046 /****************************************************************************
2047 Reply to get user quota
2048 ****************************************************************************/
2050 static void call_nt_transact_get_user_quota(connection_struct *conn,
2051 struct smb_request *req,
2055 uint32 parameter_count,
2058 uint32 max_data_count)
2060 NTSTATUS nt_status = NT_STATUS_OK;
2061 char *params = *ppparams;
2062 char *pdata = *ppdata;
2064 int data_len=0,param_len=0;
2067 files_struct *fsp = NULL;
2071 bool start_enum = True;
2072 SMB_NTQUOTA_STRUCT qt;
2073 SMB_NTQUOTA_LIST *tmp_list;
2074 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2079 if (current_user.ut.uid != 0) {
2080 DEBUG(1,("get_user_quota: access_denied service [%s] user [%s]\n",
2081 lp_servicename(SNUM(conn)),conn->user));
2082 reply_doserror(req, ERRDOS, ERRnoaccess);
2087 * Ensure minimum number of parameters sent.
2090 if (parameter_count < 4) {
2091 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2092 reply_doserror(req, ERRDOS, ERRinvalidparam);
2096 /* maybe we can check the quota_fnum */
2097 fsp = file_fsp(SVAL(params,0));
2098 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2099 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2100 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2104 /* the NULL pointer checking for fsp->fake_file_handle->pd
2105 * is done by CHECK_NTQUOTA_HANDLE_OK()
2107 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->pd;
2109 level = SVAL(params,2);
2111 /* unknown 12 bytes leading in params */
2114 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2115 /* seems that we should continue with the enum here --metze */
2117 if (qt_handle->quota_list!=NULL &&
2118 qt_handle->tmp_list==NULL) {
2121 free_ntquota_list(&(qt_handle->quota_list));
2123 /* Realloc the size of parameters and data we will return */
2125 params = nttrans_realloc(ppparams, param_len);
2126 if(params == NULL) {
2127 reply_doserror(req, ERRDOS, ERRnomem);
2132 SIVAL(params,0,data_len);
2139 case TRANSACT_GET_USER_QUOTA_LIST_START:
2141 if (qt_handle->quota_list==NULL &&
2142 qt_handle->tmp_list==NULL) {
2146 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2147 reply_doserror(req, ERRSRV, ERRerror);
2151 /* Realloc the size of parameters and data we will return */
2153 params = nttrans_realloc(ppparams, param_len);
2154 if(params == NULL) {
2155 reply_doserror(req, ERRDOS, ERRnomem);
2159 /* we should not trust the value in max_data_count*/
2160 max_data_count = MIN(max_data_count,2048);
2162 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2164 reply_doserror(req, ERRDOS, ERRnomem);
2170 /* set params Size of returned Quota Data 4 bytes*/
2171 /* but set it later when we know it */
2173 /* for each entry push the data */
2176 qt_handle->tmp_list = qt_handle->quota_list;
2179 tmp_list = qt_handle->tmp_list;
2181 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2182 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2184 sid_len = ndr_size_dom_sid(
2185 &tmp_list->quotas->sid, 0);
2186 entry_len = 40 + sid_len;
2188 /* nextoffset entry 4 bytes */
2189 SIVAL(entry,0,entry_len);
2191 /* then the len of the SID 4 bytes */
2192 SIVAL(entry,4,sid_len);
2194 /* unknown data 8 bytes SMB_BIG_UINT */
2195 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-metze*/
2197 /* the used disk space 8 bytes SMB_BIG_UINT */
2198 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2200 /* the soft quotas 8 bytes SMB_BIG_UINT */
2201 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2203 /* the hard quotas 8 bytes SMB_BIG_UINT */
2204 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2206 /* and now the SID */
2207 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2210 qt_handle->tmp_list = tmp_list;
2212 /* overwrite the offset of the last entry */
2213 SIVAL(entry-entry_len,0,0);
2215 data_len = 4+qt_len;
2216 /* overwrite the params quota_data_len */
2217 SIVAL(params,0,data_len);
2221 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2223 /* unknown 4 bytes IVAL(pdata,0) */
2225 if (data_count < 8) {
2226 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2227 reply_doserror(req, ERRDOS, ERRunknownlevel);
2231 sid_len = IVAL(pdata,4);
2232 /* Ensure this is less than 1mb. */
2233 if (sid_len > (1024*1024)) {
2234 reply_doserror(req, ERRDOS, ERRnomem);
2238 if (data_count < 8+sid_len) {
2239 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2240 reply_doserror(req, ERRDOS, ERRunknownlevel);
2244 data_len = 4+40+sid_len;
2246 if (max_data_count < data_len) {
2247 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2248 max_data_count, data_len));
2250 SIVAL(params,0,data_len);
2252 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2256 sid_parse(pdata+8,sid_len,&sid);
2258 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2261 * we have to return zero's in all fields
2262 * instead of returning an error here
2267 /* Realloc the size of parameters and data we will return */
2269 params = nttrans_realloc(ppparams, param_len);
2270 if(params == NULL) {
2271 reply_doserror(req, ERRDOS, ERRnomem);
2275 pdata = nttrans_realloc(ppdata, data_len);
2277 reply_doserror(req, ERRDOS, ERRnomem);
2283 /* set params Size of returned Quota Data 4 bytes*/
2284 SIVAL(params,0,data_len);
2286 /* nextoffset entry 4 bytes */
2289 /* then the len of the SID 4 bytes */
2290 SIVAL(entry,4,sid_len);
2292 /* unknown data 8 bytes SMB_BIG_UINT */
2293 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-mezte*/
2295 /* the used disk space 8 bytes SMB_BIG_UINT */
2296 SBIG_UINT(entry,16,qt.usedspace);
2298 /* the soft quotas 8 bytes SMB_BIG_UINT */
2299 SBIG_UINT(entry,24,qt.softlim);
2301 /* the hard quotas 8 bytes SMB_BIG_UINT */
2302 SBIG_UINT(entry,32,qt.hardlim);
2304 /* and now the SID */
2305 sid_linearize(entry+40, sid_len, &sid);
2310 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2311 reply_doserror(req, ERRSRV, ERRerror);
2316 send_nt_replies(conn, req, nt_status, params, param_len,
2320 /****************************************************************************
2321 Reply to set user quota
2322 ****************************************************************************/
2324 static void call_nt_transact_set_user_quota(connection_struct *conn,
2325 struct smb_request *req,
2329 uint32 parameter_count,
2332 uint32 max_data_count)
2334 char *params = *ppparams;
2335 char *pdata = *ppdata;
2336 int data_len=0,param_len=0;
2337 SMB_NTQUOTA_STRUCT qt;
2340 files_struct *fsp = NULL;
2345 if (current_user.ut.uid != 0) {
2346 DEBUG(1,("set_user_quota: access_denied service [%s] user [%s]\n",
2347 lp_servicename(SNUM(conn)),conn->user));
2348 reply_doserror(req, ERRDOS, ERRnoaccess);
2353 * Ensure minimum number of parameters sent.
2356 if (parameter_count < 2) {
2357 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2358 reply_doserror(req, ERRDOS, ERRinvalidparam);
2362 /* maybe we can check the quota_fnum */
2363 fsp = file_fsp(SVAL(params,0));
2364 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2365 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2366 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2370 if (data_count < 40) {
2371 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2372 reply_doserror(req, ERRDOS, ERRunknownlevel);
2376 /* offset to next quota record.
2377 * 4 bytes IVAL(pdata,0)
2382 sid_len = IVAL(pdata,4);
2384 if (data_count < 40+sid_len) {
2385 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2386 reply_doserror(req, ERRDOS, ERRunknownlevel);
2390 /* unknown 8 bytes in pdata
2391 * maybe its the change time in NTTIME
2394 /* the used space 8 bytes (SMB_BIG_UINT)*/
2395 qt.usedspace = (SMB_BIG_UINT)IVAL(pdata,16);
2396 #ifdef LARGE_SMB_OFF_T
2397 qt.usedspace |= (((SMB_BIG_UINT)IVAL(pdata,20)) << 32);
2398 #else /* LARGE_SMB_OFF_T */
2399 if ((IVAL(pdata,20) != 0)&&
2400 ((qt.usedspace != 0xFFFFFFFF)||
2401 (IVAL(pdata,20)!=0xFFFFFFFF))) {
2402 /* more than 32 bits? */
2403 reply_doserror(req, ERRDOS, ERRunknownlevel);
2406 #endif /* LARGE_SMB_OFF_T */
2408 /* the soft quotas 8 bytes (SMB_BIG_UINT)*/
2409 qt.softlim = (SMB_BIG_UINT)IVAL(pdata,24);
2410 #ifdef LARGE_SMB_OFF_T
2411 qt.softlim |= (((SMB_BIG_UINT)IVAL(pdata,28)) << 32);
2412 #else /* LARGE_SMB_OFF_T */
2413 if ((IVAL(pdata,28) != 0)&&
2414 ((qt.softlim != 0xFFFFFFFF)||
2415 (IVAL(pdata,28)!=0xFFFFFFFF))) {
2416 /* more than 32 bits? */
2417 reply_doserror(req, ERRDOS, ERRunknownlevel);
2420 #endif /* LARGE_SMB_OFF_T */
2422 /* the hard quotas 8 bytes (SMB_BIG_UINT)*/
2423 qt.hardlim = (SMB_BIG_UINT)IVAL(pdata,32);
2424 #ifdef LARGE_SMB_OFF_T
2425 qt.hardlim |= (((SMB_BIG_UINT)IVAL(pdata,36)) << 32);
2426 #else /* LARGE_SMB_OFF_T */
2427 if ((IVAL(pdata,36) != 0)&&
2428 ((qt.hardlim != 0xFFFFFFFF)||
2429 (IVAL(pdata,36)!=0xFFFFFFFF))) {
2430 /* more than 32 bits? */
2431 reply_doserror(req, ERRDOS, ERRunknownlevel);
2434 #endif /* LARGE_SMB_OFF_T */
2436 sid_parse(pdata+40,sid_len,&sid);
2437 DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid)));
2439 /* 44 unknown bytes left... */
2441 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2442 reply_doserror(req, ERRSRV, ERRerror);
2446 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2449 #endif /* HAVE_SYS_QUOTAS */
2451 static void handle_nttrans(connection_struct *conn,
2452 struct trans_state *state,
2453 struct smb_request *req)
2455 if (Protocol >= PROTOCOL_NT1) {
2456 req->flags2 |= 0x40; /* IS_LONG_NAME */
2457 SSVAL(req->inbuf,smb_flg2,req->flags2);
2460 /* Now we must call the relevant NT_TRANS function */
2461 switch(state->call) {
2462 case NT_TRANSACT_CREATE:
2464 START_PROFILE(NT_transact_create);
2465 call_nt_transact_create(
2467 &state->setup, state->setup_count,
2468 &state->param, state->total_param,
2469 &state->data, state->total_data,
2470 state->max_data_return);
2471 END_PROFILE(NT_transact_create);
2475 case NT_TRANSACT_IOCTL:
2477 START_PROFILE(NT_transact_ioctl);
2478 call_nt_transact_ioctl(
2480 &state->setup, state->setup_count,
2481 &state->param, state->total_param,
2482 &state->data, state->total_data,
2483 state->max_data_return);
2484 END_PROFILE(NT_transact_ioctl);
2488 case NT_TRANSACT_SET_SECURITY_DESC:
2490 START_PROFILE(NT_transact_set_security_desc);
2491 call_nt_transact_set_security_desc(
2493 &state->setup, state->setup_count,
2494 &state->param, state->total_param,
2495 &state->data, state->total_data,
2496 state->max_data_return);
2497 END_PROFILE(NT_transact_set_security_desc);
2501 case NT_TRANSACT_NOTIFY_CHANGE:
2503 START_PROFILE(NT_transact_notify_change);
2504 call_nt_transact_notify_change(
2506 &state->setup, state->setup_count,
2507 &state->param, state->total_param,
2508 &state->data, state->total_data,
2509 state->max_data_return,
2510 state->max_param_return);
2511 END_PROFILE(NT_transact_notify_change);
2515 case NT_TRANSACT_RENAME:
2517 START_PROFILE(NT_transact_rename);
2518 call_nt_transact_rename(
2520 &state->setup, state->setup_count,
2521 &state->param, state->total_param,
2522 &state->data, state->total_data,
2523 state->max_data_return);
2524 END_PROFILE(NT_transact_rename);
2528 case NT_TRANSACT_QUERY_SECURITY_DESC:
2530 START_PROFILE(NT_transact_query_security_desc);
2531 call_nt_transact_query_security_desc(
2533 &state->setup, state->setup_count,
2534 &state->param, state->total_param,
2535 &state->data, state->total_data,
2536 state->max_data_return);
2537 END_PROFILE(NT_transact_query_security_desc);
2541 #ifdef HAVE_SYS_QUOTAS
2542 case NT_TRANSACT_GET_USER_QUOTA:
2544 START_PROFILE(NT_transact_get_user_quota);
2545 call_nt_transact_get_user_quota(
2547 &state->setup, state->setup_count,
2548 &state->param, state->total_param,
2549 &state->data, state->total_data,
2550 state->max_data_return);
2551 END_PROFILE(NT_transact_get_user_quota);
2555 case NT_TRANSACT_SET_USER_QUOTA:
2557 START_PROFILE(NT_transact_set_user_quota);
2558 call_nt_transact_set_user_quota(
2560 &state->setup, state->setup_count,
2561 &state->param, state->total_param,
2562 &state->data, state->total_data,
2563 state->max_data_return);
2564 END_PROFILE(NT_transact_set_user_quota);
2567 #endif /* HAVE_SYS_QUOTAS */
2570 /* Error in request */
2571 DEBUG(0,("handle_nttrans: Unknown request %d in "
2572 "nttrans call\n", state->call));
2573 reply_doserror(req, ERRSRV, ERRerror);
2579 /****************************************************************************
2580 Reply to a SMBNTtrans.
2581 ****************************************************************************/
2583 void reply_nttrans(struct smb_request *req)
2585 connection_struct *conn = req->conn;
2590 uint16 function_code;
2592 struct trans_state *state;
2595 START_PROFILE(SMBnttrans);
2597 if (req->wct < 19) {
2598 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2599 END_PROFILE(SMBnttrans);
2603 size = smb_len(req->inbuf) + 4;
2604 pscnt = IVAL(req->inbuf,smb_nt_ParameterCount);
2605 psoff = IVAL(req->inbuf,smb_nt_ParameterOffset);
2606 dscnt = IVAL(req->inbuf,smb_nt_DataCount);
2607 dsoff = IVAL(req->inbuf,smb_nt_DataOffset);
2608 function_code = SVAL(req->inbuf, smb_nt_Function);
2610 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2611 reply_doserror(req, ERRSRV, ERRaccess);
2612 END_PROFILE(SMBnttrans);
2616 result = allow_new_trans(conn->pending_trans, req->mid);
2617 if (!NT_STATUS_IS_OK(result)) {
2618 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
2619 reply_nterror(req, result);
2620 END_PROFILE(SMBnttrans);
2624 if ((state = TALLOC_P(conn->mem_ctx, struct trans_state)) == NULL) {
2625 reply_doserror(req, ERRSRV, ERRaccess);
2626 END_PROFILE(SMBnttrans);
2630 state->cmd = SMBnttrans;
2632 state->mid = req->mid;
2633 state->vuid = req->vuid;
2634 state->total_data = IVAL(req->inbuf, smb_nt_TotalDataCount);
2636 state->total_param = IVAL(req->inbuf, smb_nt_TotalParameterCount);
2637 state->param = NULL;
2638 state->max_data_return = IVAL(req->inbuf,smb_nt_MaxDataCount);
2639 state->max_param_return = IVAL(req->inbuf,smb_nt_MaxParameterCount);
2641 /* setup count is in *words* */
2642 state->setup_count = 2*CVAL(req->inbuf,smb_nt_SetupCount);
2643 state->setup = NULL;
2644 state->call = function_code;
2647 * All nttrans messages we handle have smb_wct == 19 +
2648 * state->setup_count. Ensure this is so as a sanity check.
2651 if(req->wct != 19 + (state->setup_count/2)) {
2652 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2653 req->wct, 19 + (state->setup_count/2)));
2657 /* Don't allow more than 128mb for each value. */
2658 if ((state->total_data > (1024*1024*128)) ||
2659 (state->total_param > (1024*1024*128))) {
2660 reply_doserror(req, ERRDOS, ERRnomem);
2661 END_PROFILE(SMBnttrans);
2665 if ((dscnt > state->total_data) || (pscnt > state->total_param))
2668 if (state->total_data) {
2669 /* Can't use talloc here, the core routines do realloc on the
2670 * params and data. */
2671 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
2672 DEBUG(0,("reply_nttrans: data malloc fail for %u "
2673 "bytes !\n", (unsigned int)state->total_data));
2675 reply_doserror(req, ERRDOS, ERRnomem);
2676 END_PROFILE(SMBnttrans);
2679 if ((dsoff+dscnt < dsoff) || (dsoff+dscnt < dscnt))
2681 if ((smb_base(req->inbuf)+dsoff+dscnt
2682 > (char *)req->inbuf + size) ||
2683 (smb_base(req->inbuf)+dsoff+dscnt < smb_base(req->inbuf)))
2686 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
2689 if (state->total_param) {
2690 /* Can't use talloc here, the core routines do realloc on the
2691 * params and data. */
2692 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
2693 DEBUG(0,("reply_nttrans: param malloc fail for %u "
2694 "bytes !\n", (unsigned int)state->total_param));
2695 SAFE_FREE(state->data);
2697 reply_doserror(req, ERRDOS, ERRnomem);
2698 END_PROFILE(SMBnttrans);
2701 if ((psoff+pscnt < psoff) || (psoff+pscnt < pscnt))
2703 if ((smb_base(req->inbuf)+psoff+pscnt
2704 > (char *)req->inbuf + size) ||
2705 (smb_base(req->inbuf)+psoff+pscnt < smb_base(req->inbuf)))
2708 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
2711 state->received_data = dscnt;
2712 state->received_param = pscnt;
2714 if(state->setup_count > 0) {
2715 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2716 state->setup_count));
2717 state->setup = (uint16 *)TALLOC(state, state->setup_count);
2718 if (state->setup == NULL) {
2719 DEBUG(0,("reply_nttrans : Out of memory\n"));
2720 SAFE_FREE(state->data);
2721 SAFE_FREE(state->param);
2723 reply_doserror(req, ERRDOS, ERRnomem);
2724 END_PROFILE(SMBnttrans);
2728 if ((smb_nt_SetupStart + state->setup_count < smb_nt_SetupStart) ||
2729 (smb_nt_SetupStart + state->setup_count < state->setup_count)) {
2732 if (smb_nt_SetupStart + state->setup_count > size) {
2736 memcpy( state->setup, &req->inbuf[smb_nt_SetupStart],
2737 state->setup_count);
2738 dump_data(10, (uint8 *)state->setup, state->setup_count);
2741 if ((state->received_data == state->total_data) &&
2742 (state->received_param == state->total_param)) {
2743 handle_nttrans(conn, state, req);
2744 SAFE_FREE(state->param);
2745 SAFE_FREE(state->data);
2747 END_PROFILE(SMBnttrans);
2751 DLIST_ADD(conn->pending_trans, state);
2753 /* We need to send an interim response then receive the rest
2754 of the parameter/data bytes */
2755 reply_outbuf(req, 0, 0);
2756 show_msg((char *)req->outbuf);
2757 END_PROFILE(SMBnttrans);
2762 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
2763 SAFE_FREE(state->data);
2764 SAFE_FREE(state->param);
2766 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2767 END_PROFILE(SMBnttrans);
2771 /****************************************************************************
2772 Reply to a SMBnttranss
2773 ****************************************************************************/
2775 void reply_nttranss(struct smb_request *req)
2777 connection_struct *conn = req->conn;
2778 unsigned int pcnt,poff,dcnt,doff,pdisp,ddisp;
2779 struct trans_state *state;
2783 START_PROFILE(SMBnttranss);
2785 show_msg((char *)req->inbuf);
2787 if (req->wct < 18) {
2788 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2789 END_PROFILE(SMBnttranss);
2793 for (state = conn->pending_trans; state != NULL;
2794 state = state->next) {
2795 if (state->mid == req->mid) {
2800 if ((state == NULL) || (state->cmd != SMBnttrans)) {
2801 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2802 END_PROFILE(SMBnttranss);
2806 /* Revise state->total_param and state->total_data in case they have
2807 changed downwards */
2808 if (IVAL(req->inbuf, smb_nts_TotalParameterCount)
2809 < state->total_param) {
2810 state->total_param = IVAL(req->inbuf,
2811 smb_nts_TotalParameterCount);
2813 if (IVAL(req->inbuf, smb_nts_TotalDataCount) < state->total_data) {
2814 state->total_data = IVAL(req->inbuf, smb_nts_TotalDataCount);
2817 size = smb_len(req->inbuf) + 4;
2819 pcnt = IVAL(req->inbuf,smb_nts_ParameterCount);
2820 poff = IVAL(req->inbuf, smb_nts_ParameterOffset);
2821 pdisp = IVAL(req->inbuf, smb_nts_ParameterDisplacement);
2823 dcnt = IVAL(req->inbuf, smb_nts_DataCount);
2824 ddisp = IVAL(req->inbuf, smb_nts_DataDisplacement);
2825 doff = IVAL(req->inbuf, smb_nts_DataOffset);
2827 state->received_param += pcnt;
2828 state->received_data += dcnt;
2830 if ((state->received_data > state->total_data) ||
2831 (state->received_param > state->total_param))
2835 if (pdisp+pcnt > state->total_param)
2837 if ((pdisp+pcnt < pdisp) || (pdisp+pcnt < pcnt))
2839 if (pdisp > state->total_param)
2841 if ((smb_base(req->inbuf) + poff + pcnt
2842 > (char *)req->inbuf + size) ||
2843 (smb_base(req->inbuf) + poff + pcnt
2844 < smb_base(req->inbuf)))
2846 if (state->param + pdisp < state->param)
2849 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,
2854 if (ddisp+dcnt > state->total_data)
2856 if ((ddisp+dcnt < ddisp) || (ddisp+dcnt < dcnt))
2858 if (ddisp > state->total_data)
2860 if ((smb_base(req->inbuf) + doff + dcnt
2861 > (char *)req->inbuf + size) ||
2862 (smb_base(req->inbuf) + doff + dcnt
2863 < smb_base(req->inbuf)))
2865 if (state->data + ddisp < state->data)
2868 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,
2872 if ((state->received_param < state->total_param) ||
2873 (state->received_data < state->total_data)) {
2874 END_PROFILE(SMBnttranss);
2879 * construct_reply_common will copy smb_com from inbuf to
2880 * outbuf. SMBnttranss is wrong here.
2882 SCVAL(req->inbuf,smb_com,SMBnttrans);
2884 handle_nttrans(conn, state, req);
2886 DLIST_REMOVE(conn->pending_trans, state);
2887 SAFE_FREE(state->data);
2888 SAFE_FREE(state->param);
2890 END_PROFILE(SMBnttranss);
2895 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
2896 DLIST_REMOVE(conn->pending_trans, state);
2897 SAFE_FREE(state->data);
2898 SAFE_FREE(state->param);
2900 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2901 END_PROFILE(SMBnttranss);