2 Unix SMB/CIFS implementation.
3 SMB NT transaction handling
4 Copyright (C) Jeremy Allison 1994-2007
5 Copyright (C) Stefan (metze) Metzmacher 2003
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "smbd/globals.h"
24 extern enum protocol_types Protocol;
25 extern const struct generic_mapping file_generic_mapping;
27 static char *nttrans_realloc(char **ptr, size_t size)
30 smb_panic("nttrans_realloc() called with NULL ptr");
33 *ptr = (char *)SMB_REALLOC(*ptr, size);
37 memset(*ptr,'\0',size);
41 /****************************************************************************
42 Send the required number of replies back.
43 We assume all fields other than the data fields are
44 set correctly for the type of call.
45 HACK ! Always assumes smb_setup field is zero.
46 ****************************************************************************/
48 void send_nt_replies(connection_struct *conn,
49 struct smb_request *req, NTSTATUS nt_error,
50 char *params, int paramsize,
51 char *pdata, int datasize)
53 int data_to_send = datasize;
54 int params_to_send = paramsize;
58 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
59 int alignment_offset = 3;
60 int data_alignment_offset = 0;
63 * If there genuinely are no parameters or data to send just send
67 if(params_to_send == 0 && data_to_send == 0) {
68 reply_outbuf(req, 18, 0);
69 show_msg((char *)req->outbuf);
74 * When sending params and data ensure that both are nicely aligned.
75 * Only do this alignment when there is also data to send - else
76 * can cause NT redirector problems.
79 if (((params_to_send % 4) != 0) && (data_to_send != 0)) {
80 data_alignment_offset = 4 - (params_to_send % 4);
84 * Space is bufsize minus Netbios over TCP header minus SMB header.
85 * The alignment_offset is to align the param bytes on a four byte
86 * boundary (2 bytes for data len, one byte pad).
87 * NT needs this to work correctly.
90 useable_space = max_send - (smb_size
93 + data_alignment_offset);
95 if (useable_space < 0) {
96 char *msg = talloc_asprintf(
98 "send_nt_replies failed sanity useable_space = %d!!!",
100 DEBUG(0, ("%s\n", msg));
101 exit_server_cleanly(msg);
104 while (params_to_send || data_to_send) {
107 * Calculate whether we will totally or partially fill this packet.
110 total_sent_thistime = params_to_send + data_to_send;
113 * We can never send more than useable_space.
116 total_sent_thistime = MIN(total_sent_thistime, useable_space);
118 reply_outbuf(req, 18,
119 total_sent_thistime + alignment_offset
120 + data_alignment_offset);
123 * We might have had SMBnttranss in req->inbuf, fix that.
125 SCVAL(req->outbuf, smb_com, SMBnttrans);
128 * Set total params and data to be sent.
131 SIVAL(req->outbuf,smb_ntr_TotalParameterCount,paramsize);
132 SIVAL(req->outbuf,smb_ntr_TotalDataCount,datasize);
135 * Calculate how many parameters and data we can fit into
136 * this packet. Parameters get precedence.
139 params_sent_thistime = MIN(params_to_send,useable_space);
140 data_sent_thistime = useable_space - params_sent_thistime;
141 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
143 SIVAL(req->outbuf, smb_ntr_ParameterCount,
144 params_sent_thistime);
146 if(params_sent_thistime == 0) {
147 SIVAL(req->outbuf,smb_ntr_ParameterOffset,0);
148 SIVAL(req->outbuf,smb_ntr_ParameterDisplacement,0);
151 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
152 * parameter bytes, however the first 4 bytes of outbuf are
153 * the Netbios over TCP header. Thus use smb_base() to subtract
154 * them from the calculation.
157 SIVAL(req->outbuf,smb_ntr_ParameterOffset,
158 ((smb_buf(req->outbuf)+alignment_offset)
159 - smb_base(req->outbuf)));
161 * Absolute displacement of param bytes sent in this packet.
164 SIVAL(req->outbuf, smb_ntr_ParameterDisplacement,
169 * Deal with the data portion.
172 SIVAL(req->outbuf, smb_ntr_DataCount, data_sent_thistime);
174 if(data_sent_thistime == 0) {
175 SIVAL(req->outbuf,smb_ntr_DataOffset,0);
176 SIVAL(req->outbuf,smb_ntr_DataDisplacement, 0);
179 * The offset of the data bytes is the offset of the
180 * parameter bytes plus the number of parameters being sent this time.
183 SIVAL(req->outbuf, smb_ntr_DataOffset,
184 ((smb_buf(req->outbuf)+alignment_offset) -
185 smb_base(req->outbuf))
186 + params_sent_thistime + data_alignment_offset);
187 SIVAL(req->outbuf,smb_ntr_DataDisplacement, pd - pdata);
191 * Copy the param bytes into the packet.
194 if(params_sent_thistime) {
195 if (alignment_offset != 0) {
196 memset(smb_buf(req->outbuf), 0,
199 memcpy((smb_buf(req->outbuf)+alignment_offset), pp,
200 params_sent_thistime);
204 * Copy in the data bytes
207 if(data_sent_thistime) {
208 if (data_alignment_offset != 0) {
209 memset((smb_buf(req->outbuf)+alignment_offset+
210 params_sent_thistime), 0,
211 data_alignment_offset);
213 memcpy(smb_buf(req->outbuf)+alignment_offset
214 +params_sent_thistime+data_alignment_offset,
215 pd,data_sent_thistime);
218 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
219 params_sent_thistime, data_sent_thistime, useable_space));
220 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
221 params_to_send, data_to_send, paramsize, datasize));
223 if (NT_STATUS_V(nt_error)) {
224 error_packet_set((char *)req->outbuf,
229 /* Send the packet */
230 show_msg((char *)req->outbuf);
231 if (!srv_send_smb(smbd_server_fd(),
233 IS_CONN_ENCRYPTED(conn),
235 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
238 TALLOC_FREE(req->outbuf);
240 pp += params_sent_thistime;
241 pd += data_sent_thistime;
243 params_to_send -= params_sent_thistime;
244 data_to_send -= data_sent_thistime;
250 if(params_to_send < 0 || data_to_send < 0) {
251 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
252 params_to_send, data_to_send));
253 exit_server_cleanly("send_nt_replies: internal error");
258 /****************************************************************************
259 Is it an NTFS stream name ?
260 An NTFS file name is <path>.<extention>:<stream name>:<stream type>
261 $DATA can be used as both a stream name and a stream type. A missing stream
262 name or type implies $DATA.
264 Both Windows stream names and POSIX files can contain the ':' character.
265 This function first checks for the existence of a colon in the last component
266 of the given name. If the name contains a colon we differentiate between a
267 stream and POSIX file by checking if the latter exists through a POSIX stat.
269 Function assumes we've already chdir() to the "root" directory of fname.
270 ****************************************************************************/
272 bool is_ntfs_stream_name(const char *fname)
274 const char *lastcomp;
275 SMB_STRUCT_STAT sbuf;
277 /* If all pathnames are treated as POSIX we ignore streams. */
278 if (lp_posix_pathnames()) {
282 /* Find the last component of the name. */
283 if ((lastcomp = strrchr_m(fname, '/')) != NULL)
288 /* If there is no colon in the last component, it's not a stream. */
289 if (strchr_m(lastcomp, ':') == NULL)
293 * If file already exists on disk, it's not a stream. The stat must
294 * bypass the vfs layer so streams modules don't intefere.
296 if (sys_stat(fname, &sbuf) == 0) {
297 DEBUG(5, ("is_ntfs_stream_name: file %s contains a ':' but is "
298 "not a stream\n", fname));
305 /****************************************************************************
306 Reply to an NT create and X call on a pipe
307 ****************************************************************************/
309 static void nt_open_pipe(char *fname, connection_struct *conn,
310 struct smb_request *req, int *ppnum)
315 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
317 /* Strip \\ off the name. */
320 status = open_np_file(req, fname, &fsp);
321 if (!NT_STATUS_IS_OK(status)) {
322 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
323 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
327 reply_nterror(req, status);
335 /****************************************************************************
336 Reply to an NT create and X call for pipes.
337 ****************************************************************************/
339 static void do_ntcreate_pipe_open(connection_struct *conn,
340 struct smb_request *req)
345 uint32 flags = IVAL(req->vwv+3, 1);
346 TALLOC_CTX *ctx = talloc_tos();
348 srvstr_pull_req_talloc(ctx, req, &fname, req->buf, STR_TERMINATE);
351 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
355 nt_open_pipe(fname, conn, req, &pnum);
363 * Deal with pipe return.
366 if (flags & EXTENDED_RESPONSE_REQUIRED) {
367 /* This is very strange. We
368 * return 50 words, but only set
369 * the wcnt to 42 ? It's definately
370 * what happens on the wire....
372 reply_outbuf(req, 50, 0);
373 SCVAL(req->outbuf,smb_wct,42);
375 reply_outbuf(req, 34, 0);
378 p = (char *)req->outbuf + smb_vwv2;
382 SIVAL(p,0,FILE_WAS_OPENED);
385 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
388 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
390 SSVAL(p,2, 0x5FF); /* ? */
393 if (flags & EXTENDED_RESPONSE_REQUIRED) {
395 SIVAL(p,0,FILE_GENERIC_ALL);
397 * For pipes W2K3 seems to return
399 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
401 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
404 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
409 /****************************************************************************
410 Reply to an NT create and X call.
411 ****************************************************************************/
413 void reply_ntcreate_and_X(struct smb_request *req)
415 connection_struct *conn = req->conn;
419 uint32 file_attributes;
421 uint32 create_disposition;
422 uint32 create_options;
424 uint64_t allocation_size;
425 /* Breakout the oplock request bits so we can set the
426 reply bits separately. */
428 SMB_OFF_T file_len = 0;
429 SMB_STRUCT_STAT sbuf;
431 files_struct *fsp = NULL;
433 struct timespec c_timespec;
434 struct timespec a_timespec;
435 struct timespec m_timespec;
438 uint8_t oplock_granted = NO_OPLOCK_RETURN;
439 TALLOC_CTX *ctx = talloc_tos();
441 START_PROFILE(SMBntcreateX);
444 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
448 flags = IVAL(req->vwv+3, 1);
449 access_mask = IVAL(req->vwv+7, 1);
450 file_attributes = IVAL(req->vwv+13, 1);
451 share_access = IVAL(req->vwv+15, 1);
452 create_disposition = IVAL(req->vwv+17, 1);
453 create_options = IVAL(req->vwv+19, 1);
454 root_dir_fid = (uint16)IVAL(req->vwv+5, 1);
456 allocation_size = (uint64_t)IVAL(req->vwv+9, 1);
457 #ifdef LARGE_SMB_OFF_T
458 allocation_size |= (((uint64_t)IVAL(req->vwv+11, 1)) << 32);
461 srvstr_get_path_req(ctx, req, &fname, (const char *)req->buf,
462 STR_TERMINATE, &status);
464 if (!NT_STATUS_IS_OK(status)) {
465 reply_nterror(req, status);
466 END_PROFILE(SMBntcreateX);
470 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
471 "file_attributes = 0x%x, share_access = 0x%x, "
472 "create_disposition = 0x%x create_options = 0x%x "
473 "root_dir_fid = 0x%x, fname = %s\n",
475 (unsigned int)access_mask,
476 (unsigned int)file_attributes,
477 (unsigned int)share_access,
478 (unsigned int)create_disposition,
479 (unsigned int)create_options,
480 (unsigned int)root_dir_fid,
484 * we need to remove ignored bits when they come directly from the client
485 * because we reuse some of them for internal stuff
487 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
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);
504 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
505 if (oplock_request) {
506 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
510 status = SMB_VFS_CREATE_FILE(
513 root_dir_fid, /* root_dir_fid */
515 CFF_DOS_PATH, /* create_file_flags */
516 access_mask, /* access_mask */
517 share_access, /* share_access */
518 create_disposition, /* create_disposition*/
519 create_options, /* create_options */
520 file_attributes, /* file_attributes */
521 oplock_request, /* oplock_request */
522 allocation_size, /* allocation_size */
529 if (!NT_STATUS_IS_OK(status)) {
530 if (open_was_deferred(req->mid)) {
531 /* We have re-scheduled this call, no error. */
532 END_PROFILE(SMBntcreateX);
535 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
536 reply_botherror(req, status, ERRDOS, ERRfilexists);
539 reply_nterror(req, status);
541 END_PROFILE(SMBntcreateX);
546 * If the caller set the extended oplock request bit
547 * and we granted one (by whatever means) - set the
548 * correct bit for extended oplock reply.
551 if (oplock_request &&
552 (lp_fake_oplocks(SNUM(conn))
553 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
556 * Exclusive oplock granted
559 if (flags & REQUEST_BATCH_OPLOCK) {
560 oplock_granted = BATCH_OPLOCK_RETURN;
562 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
564 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
565 oplock_granted = LEVEL_II_OPLOCK_RETURN;
567 oplock_granted = NO_OPLOCK_RETURN;
570 file_len = sbuf.st_size;
571 fattr = dos_mode(conn,fsp->fsp_name,&sbuf);
573 fattr = FILE_ATTRIBUTE_NORMAL;
576 if (flags & EXTENDED_RESPONSE_REQUIRED) {
577 /* This is very strange. We
578 * return 50 words, but only set
579 * the wcnt to 42 ? It's definately
580 * what happens on the wire....
582 reply_outbuf(req, 50, 0);
583 SCVAL(req->outbuf,smb_wct,42);
585 reply_outbuf(req, 34, 0);
588 p = (char *)req->outbuf + smb_vwv2;
590 SCVAL(p, 0, oplock_granted);
593 SSVAL(p,0,fsp->fnum);
595 if ((create_disposition == FILE_SUPERSEDE)
596 && (info == FILE_WAS_OVERWRITTEN)) {
597 SIVAL(p,0,FILE_WAS_SUPERSEDED);
604 c_timespec = get_create_timespec(
605 &sbuf,lp_fake_dir_create_times(SNUM(conn)));
606 a_timespec = get_atimespec(&sbuf);
607 m_timespec = get_mtimespec(&sbuf);
609 if (lp_dos_filetime_resolution(SNUM(conn))) {
610 dos_filetime_timespec(&c_timespec);
611 dos_filetime_timespec(&a_timespec);
612 dos_filetime_timespec(&m_timespec);
615 put_long_date_timespec(p, c_timespec); /* create time. */
617 put_long_date_timespec(p, a_timespec); /* access time */
619 put_long_date_timespec(p, m_timespec); /* write time */
621 put_long_date_timespec(p, m_timespec); /* change time */
623 SIVAL(p,0,fattr); /* File Attributes. */
625 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn,fsp,&sbuf));
627 SOFF_T(p,0,file_len);
629 if (flags & EXTENDED_RESPONSE_REQUIRED) {
633 SCVAL(p,0,fsp->is_directory ? 1 : 0);
635 if (flags & EXTENDED_RESPONSE_REQUIRED) {
638 if (fsp->is_directory
639 || can_write_to_file(conn, fsp->fsp_name, &sbuf)) {
640 perms = FILE_GENERIC_ALL;
642 perms = FILE_GENERIC_READ|FILE_EXECUTE;
647 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n",
648 fsp->fnum, fsp->fsp_name));
651 END_PROFILE(SMBntcreateX);
655 /****************************************************************************
656 Reply to a NT_TRANSACT_CREATE call to open a pipe.
657 ****************************************************************************/
659 static void do_nt_transact_create_pipe(connection_struct *conn,
660 struct smb_request *req,
661 uint16 **ppsetup, uint32 setup_count,
662 char **ppparams, uint32 parameter_count,
663 char **ppdata, uint32 data_count)
666 char *params = *ppparams;
672 TALLOC_CTX *ctx = talloc_tos();
675 * Ensure minimum number of parameters sent.
678 if(parameter_count < 54) {
679 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
680 reply_doserror(req, ERRDOS, ERRnoaccess);
684 flags = IVAL(params,0);
686 srvstr_get_path(ctx, params, req->flags2, &fname, params+53,
687 parameter_count-53, STR_TERMINATE,
689 if (!NT_STATUS_IS_OK(status)) {
690 reply_nterror(req, status);
694 nt_open_pipe(fname, conn, req, &pnum);
701 /* Realloc the size of parameters and data we will return */
702 if (flags & EXTENDED_RESPONSE_REQUIRED) {
703 /* Extended response is 32 more byyes. */
708 params = nttrans_realloc(ppparams, param_len);
710 reply_doserror(req, ERRDOS, ERRnomem);
715 SCVAL(p,0,NO_OPLOCK_RETURN);
720 SIVAL(p,0,FILE_WAS_OPENED);
724 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
727 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
729 SSVAL(p,2, 0x5FF); /* ? */
732 if (flags & EXTENDED_RESPONSE_REQUIRED) {
734 SIVAL(p,0,FILE_GENERIC_ALL);
736 * For pipes W2K3 seems to return
738 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
740 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
743 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
745 /* Send the required number of replies */
746 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
751 /****************************************************************************
752 Internal fn to set security descriptors.
753 ****************************************************************************/
755 static NTSTATUS set_sd(files_struct *fsp, uint8 *data, uint32 sd_len,
756 uint32 security_info_sent)
758 SEC_DESC *psd = NULL;
761 if (sd_len == 0 || !lp_nt_acl_support(SNUM(fsp->conn))) {
765 status = unmarshall_sec_desc(talloc_tos(), data, sd_len, &psd);
767 if (!NT_STATUS_IS_OK(status)) {
771 if (psd->owner_sid == NULL) {
772 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
774 if (psd->group_sid == NULL) {
775 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
778 /* Convert all the generic bits. */
779 security_acl_map_generic(psd->dacl, &file_generic_mapping);
780 security_acl_map_generic(psd->sacl, &file_generic_mapping);
782 if (DEBUGLEVEL >= 10) {
783 DEBUG(10,("set_sd for file %s\n", fsp->fsp_name ));
784 NDR_PRINT_DEBUG(security_descriptor, psd);
787 status = SMB_VFS_FSET_NT_ACL(fsp, security_info_sent, psd);
794 /****************************************************************************
795 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
796 ****************************************************************************/
798 static struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
800 struct ea_list *ea_list_head = NULL;
807 while (offset + 4 <= data_size) {
808 size_t next_offset = IVAL(pdata,offset);
809 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
815 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
816 if (next_offset == 0) {
819 offset += next_offset;
825 /****************************************************************************
826 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
827 ****************************************************************************/
829 static void call_nt_transact_create(connection_struct *conn,
830 struct smb_request *req,
831 uint16 **ppsetup, uint32 setup_count,
832 char **ppparams, uint32 parameter_count,
833 char **ppdata, uint32 data_count,
834 uint32 max_data_count)
837 char *params = *ppparams;
838 char *data = *ppdata;
839 /* Breakout the oplock request bits so we can set the reply bits separately. */
841 SMB_OFF_T file_len = 0;
842 SMB_STRUCT_STAT sbuf;
844 files_struct *fsp = NULL;
848 uint32 file_attributes;
850 uint32 create_disposition;
851 uint32 create_options;
853 struct security_descriptor *sd = NULL;
856 struct timespec c_timespec;
857 struct timespec a_timespec;
858 struct timespec m_timespec;
859 struct ea_list *ea_list = NULL;
862 uint64_t allocation_size;
864 uint8_t oplock_granted;
865 TALLOC_CTX *ctx = talloc_tos();
867 DEBUG(5,("call_nt_transact_create\n"));
870 * If it's an IPC, use the pipe handler.
874 if (lp_nt_pipe_support()) {
875 do_nt_transact_create_pipe(
877 ppsetup, setup_count,
878 ppparams, parameter_count,
882 reply_doserror(req, ERRDOS, ERRnoaccess);
887 * Ensure minimum number of parameters sent.
890 if(parameter_count < 54) {
891 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
892 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
896 flags = IVAL(params,0);
897 access_mask = IVAL(params,8);
898 file_attributes = IVAL(params,20);
899 share_access = IVAL(params,24);
900 create_disposition = IVAL(params,28);
901 create_options = IVAL(params,32);
902 sd_len = IVAL(params,36);
903 ea_len = IVAL(params,40);
904 root_dir_fid = (uint16)IVAL(params,4);
905 allocation_size = (uint64_t)IVAL(params,12);
906 #ifdef LARGE_SMB_OFF_T
907 allocation_size |= (((uint64_t)IVAL(params,16)) << 32);
911 * we need to remove ignored bits when they come directly from the client
912 * because we reuse some of them for internal stuff
914 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
916 /* Ensure the data_len is correct for the sd and ea values given. */
917 if ((ea_len + sd_len > data_count)
918 || (ea_len > data_count) || (sd_len > data_count)
919 || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
920 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
921 "%u, data_count = %u\n", (unsigned int)ea_len,
922 (unsigned int)sd_len, (unsigned int)data_count));
923 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
928 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
931 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
933 if (!NT_STATUS_IS_OK(status)) {
934 DEBUG(10, ("call_nt_transact_create: "
935 "unmarshall_sec_desc failed: %s\n",
937 reply_nterror(req, status);
943 if (!lp_ea_support(SNUM(conn))) {
944 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
945 "EA's not supported.\n",
946 (unsigned int)ea_len));
947 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
952 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
953 "too small (should be more than 10)\n",
954 (unsigned int)ea_len ));
955 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
959 /* We have already checked that ea_len <= data_count here. */
960 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
962 if (ea_list == NULL) {
963 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
968 srvstr_get_path(ctx, params, req->flags2, &fname,
969 params+53, parameter_count-53,
970 STR_TERMINATE, &status);
971 if (!NT_STATUS_IS_OK(status)) {
972 reply_nterror(req, status);
976 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
977 if (oplock_request) {
978 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
982 status = SMB_VFS_CREATE_FILE(
985 root_dir_fid, /* root_dir_fid */
987 CFF_DOS_PATH, /* create_file_flags */
988 access_mask, /* access_mask */
989 share_access, /* share_access */
990 create_disposition, /* create_disposition*/
991 create_options, /* create_options */
992 file_attributes, /* file_attributes */
993 oplock_request, /* oplock_request */
994 allocation_size, /* allocation_size */
996 ea_list, /* ea_list */
1001 if(!NT_STATUS_IS_OK(status)) {
1002 if (open_was_deferred(req->mid)) {
1003 /* We have re-scheduled this call, no error. */
1006 reply_openerror(req, status);
1011 * If the caller set the extended oplock request bit
1012 * and we granted one (by whatever means) - set the
1013 * correct bit for extended oplock reply.
1016 if (oplock_request &&
1017 (lp_fake_oplocks(SNUM(conn))
1018 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
1021 * Exclusive oplock granted
1024 if (flags & REQUEST_BATCH_OPLOCK) {
1025 oplock_granted = BATCH_OPLOCK_RETURN;
1027 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
1029 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1030 oplock_granted = LEVEL_II_OPLOCK_RETURN;
1032 oplock_granted = NO_OPLOCK_RETURN;
1035 file_len = sbuf.st_size;
1036 fattr = dos_mode(conn,fsp->fsp_name,&sbuf);
1038 fattr = FILE_ATTRIBUTE_NORMAL;
1041 /* Realloc the size of parameters and data we will return */
1042 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1043 /* Extended response is 32 more byyes. */
1048 params = nttrans_realloc(ppparams, param_len);
1049 if(params == NULL) {
1050 reply_doserror(req, ERRDOS, ERRnomem);
1055 SCVAL(p, 0, oplock_granted);
1058 SSVAL(p,0,fsp->fnum);
1060 if ((create_disposition == FILE_SUPERSEDE)
1061 && (info == FILE_WAS_OVERWRITTEN)) {
1062 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1069 c_timespec = get_create_timespec(
1070 &sbuf,lp_fake_dir_create_times(SNUM(conn)));
1071 a_timespec = get_atimespec(&sbuf);
1072 m_timespec = get_mtimespec(&sbuf);
1074 if (lp_dos_filetime_resolution(SNUM(conn))) {
1075 dos_filetime_timespec(&c_timespec);
1076 dos_filetime_timespec(&a_timespec);
1077 dos_filetime_timespec(&m_timespec);
1080 put_long_date_timespec(p, c_timespec); /* create time. */
1082 put_long_date_timespec(p, a_timespec); /* access time */
1084 put_long_date_timespec(p, m_timespec); /* write time */
1086 put_long_date_timespec(p, m_timespec); /* change time */
1088 SIVAL(p,0,fattr); /* File Attributes. */
1090 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn,fsp,&sbuf));
1092 SOFF_T(p,0,file_len);
1094 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1098 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1100 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1103 if (fsp->is_directory
1104 || can_write_to_file(conn, fsp->fsp_name, &sbuf)) {
1105 perms = FILE_GENERIC_ALL;
1107 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1112 DEBUG(5,("call_nt_transact_create: open name = %s\n", fsp->fsp_name));
1114 /* Send the required number of replies */
1115 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1120 /****************************************************************************
1121 Reply to a NT CANCEL request.
1122 conn POINTER CAN BE NULL HERE !
1123 ****************************************************************************/
1125 void reply_ntcancel(struct smb_request *req)
1128 * Go through and cancel any pending change notifies.
1131 START_PROFILE(SMBntcancel);
1132 remove_pending_change_notify_requests_by_mid(req->mid);
1133 remove_pending_lock_requests_by_mid(req->mid);
1134 srv_cancel_sign_response(req->mid);
1136 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", req->mid));
1138 END_PROFILE(SMBntcancel);
1142 /****************************************************************************
1144 ****************************************************************************/
1146 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
1147 connection_struct *conn,
1148 struct smb_request *req,
1149 const char *oldname_in,
1150 const char *newname_in,
1153 SMB_STRUCT_STAT sbuf1, sbuf2;
1154 char *oldname = NULL;
1155 char *newname = NULL;
1156 char *last_component_oldname = NULL;
1157 char *last_component_newname = NULL;
1158 files_struct *fsp1,*fsp2;
1162 NTSTATUS status = NT_STATUS_OK;
1168 if (!CAN_WRITE(conn)) {
1169 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1172 status = unix_convert(ctx, conn, oldname_in, False, &oldname,
1173 &last_component_oldname, &sbuf1);
1174 if (!NT_STATUS_IS_OK(status)) {
1178 status = check_name(conn, oldname);
1179 if (!NT_STATUS_IS_OK(status)) {
1183 /* Source must already exist. */
1184 if (!VALID_STAT(sbuf1)) {
1185 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1187 /* Ensure attributes match. */
1188 fattr = dos_mode(conn,oldname,&sbuf1);
1189 if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
1190 return NT_STATUS_NO_SUCH_FILE;
1193 status = unix_convert(ctx, conn, newname_in, False, &newname,
1194 &last_component_newname, &sbuf2);
1195 if (!NT_STATUS_IS_OK(status)) {
1199 status = check_name(conn, newname);
1200 if (!NT_STATUS_IS_OK(status)) {
1204 /* Disallow if newname already exists. */
1205 if (VALID_STAT(sbuf2)) {
1206 return NT_STATUS_OBJECT_NAME_COLLISION;
1209 /* No links from a directory. */
1210 if (S_ISDIR(sbuf1.st_mode)) {
1211 return NT_STATUS_FILE_IS_A_DIRECTORY;
1214 /* Ensure this is within the share. */
1215 status = check_reduced_name(conn, oldname);
1216 if (!NT_STATUS_IS_OK(status)) {
1220 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1223 status = SMB_VFS_CREATE_FILE(
1226 0, /* root_dir_fid */
1227 oldname, /* fname */
1228 0, /* create_file_flags */
1229 FILE_READ_DATA, /* access_mask */
1230 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1232 FILE_OPEN, /* create_disposition*/
1233 0, /* create_options */
1234 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
1235 NO_OPLOCK, /* oplock_request */
1236 0, /* allocation_size */
1241 &sbuf1); /* psbuf */
1243 if (!NT_STATUS_IS_OK(status)) {
1247 status = SMB_VFS_CREATE_FILE(
1250 0, /* root_dir_fid */
1251 newname, /* fname */
1252 0, /* create_file_flags */
1253 FILE_WRITE_DATA, /* access_mask */
1254 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1256 FILE_CREATE, /* create_disposition*/
1257 0, /* create_options */
1258 fattr, /* file_attributes */
1259 NO_OPLOCK, /* oplock_request */
1260 0, /* allocation_size */
1265 &sbuf2); /* psbuf */
1267 if (!NT_STATUS_IS_OK(status)) {
1268 close_file(NULL, fsp1, ERROR_CLOSE);
1272 if (sbuf1.st_size) {
1273 ret = vfs_transfer_file(fsp1, fsp2, sbuf1.st_size);
1277 * As we are opening fsp1 read-only we only expect
1278 * an error on close on fsp2 if we are out of space.
1279 * Thus we don't look at the error return from the
1282 close_file(NULL, fsp1, NORMAL_CLOSE);
1284 /* Ensure the modtime is set correctly on the destination file. */
1285 set_close_write_time(fsp2, get_mtimespec(&sbuf1));
1287 status = close_file(NULL, fsp2, NORMAL_CLOSE);
1289 /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
1290 creates the file. This isn't the correct thing to do in the copy
1292 if (!parent_dirname(talloc_tos(), newname, &parent, NULL)) {
1293 return NT_STATUS_NO_MEMORY;
1295 file_set_dosmode(conn, newname, fattr, &sbuf2, parent, false);
1296 TALLOC_FREE(parent);
1298 if (ret < (SMB_OFF_T)sbuf1.st_size) {
1299 return NT_STATUS_DISK_FULL;
1302 if (!NT_STATUS_IS_OK(status)) {
1303 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1304 nt_errstr(status), oldname, newname));
1309 /****************************************************************************
1310 Reply to a NT rename request.
1311 ****************************************************************************/
1313 void reply_ntrename(struct smb_request *req)
1315 connection_struct *conn = req->conn;
1316 char *oldname = NULL;
1317 char *newname = NULL;
1320 bool src_has_wcard = False;
1321 bool dest_has_wcard = False;
1324 TALLOC_CTX *ctx = talloc_tos();
1326 START_PROFILE(SMBntrename);
1329 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1330 END_PROFILE(SMBntrename);
1334 attrs = SVAL(req->vwv+0, 0);
1335 rename_type = SVAL(req->vwv+1, 0);
1337 p = (const char *)req->buf + 1;
1338 p += srvstr_get_path_req_wcard(ctx, req, &oldname, p, STR_TERMINATE,
1339 &status, &src_has_wcard);
1340 if (!NT_STATUS_IS_OK(status)) {
1341 reply_nterror(req, status);
1342 END_PROFILE(SMBntrename);
1346 if (ms_has_wild(oldname)) {
1347 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1348 END_PROFILE(SMBntrename);
1353 p += srvstr_get_path_req_wcard(ctx, req, &newname, p, STR_TERMINATE,
1354 &status, &dest_has_wcard);
1355 if (!NT_STATUS_IS_OK(status)) {
1356 reply_nterror(req, status);
1357 END_PROFILE(SMBntrename);
1361 status = resolve_dfspath(ctx, conn,
1362 req->flags2 & FLAGS2_DFS_PATHNAMES,
1365 if (!NT_STATUS_IS_OK(status)) {
1366 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1367 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1368 ERRSRV, ERRbadpath);
1369 END_PROFILE(SMBntrename);
1372 reply_nterror(req, status);
1373 END_PROFILE(SMBntrename);
1377 status = resolve_dfspath(ctx, conn,
1378 req->flags2 & FLAGS2_DFS_PATHNAMES,
1381 if (!NT_STATUS_IS_OK(status)) {
1382 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1383 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1384 ERRSRV, ERRbadpath);
1385 END_PROFILE(SMBntrename);
1388 reply_nterror(req, status);
1389 END_PROFILE(SMBntrename);
1393 /* The new name must begin with a ':' if the old name is a stream. */
1394 if (is_ntfs_stream_name(oldname) && (newname[0] != ':')) {
1395 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1396 END_PROFILE(SMBntrename);
1400 DEBUG(3,("reply_ntrename : %s -> %s\n",oldname,newname));
1402 switch(rename_type) {
1403 case RENAME_FLAG_RENAME:
1404 status = rename_internals(ctx, conn, req, oldname,
1405 newname, attrs, False, src_has_wcard,
1406 dest_has_wcard, DELETE_ACCESS);
1408 case RENAME_FLAG_HARD_LINK:
1409 if (src_has_wcard || dest_has_wcard) {
1411 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1413 status = hardlink_internals(ctx,
1419 case RENAME_FLAG_COPY:
1420 if (src_has_wcard || dest_has_wcard) {
1422 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1424 status = copy_internals(ctx, conn, req, oldname,
1428 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1429 status = NT_STATUS_INVALID_PARAMETER;
1432 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1436 if (!NT_STATUS_IS_OK(status)) {
1437 if (open_was_deferred(req->mid)) {
1438 /* We have re-scheduled this call. */
1439 END_PROFILE(SMBntrename);
1443 reply_nterror(req, status);
1444 END_PROFILE(SMBntrename);
1448 reply_outbuf(req, 0, 0);
1450 END_PROFILE(SMBntrename);
1454 /****************************************************************************
1455 Reply to a notify change - queue the request and
1456 don't allow a directory to be opened.
1457 ****************************************************************************/
1459 static void call_nt_transact_notify_change(connection_struct *conn,
1460 struct smb_request *req,
1464 uint32 parameter_count,
1465 char **ppdata, uint32 data_count,
1466 uint32 max_data_count,
1467 uint32 max_param_count)
1469 uint16 *setup = *ppsetup;
1475 if(setup_count < 6) {
1476 reply_doserror(req, ERRDOS, ERRbadfunc);
1480 fsp = file_fsp(req, SVAL(setup,4));
1481 filter = IVAL(setup, 0);
1482 recursive = (SVAL(setup, 6) != 0) ? True : False;
1484 DEBUG(3,("call_nt_transact_notify_change\n"));
1487 reply_doserror(req, ERRDOS, ERRbadfid);
1492 char *filter_string;
1494 if (!(filter_string = notify_filter_string(NULL, filter))) {
1495 reply_nterror(req,NT_STATUS_NO_MEMORY);
1499 DEBUG(3,("call_nt_transact_notify_change: notify change "
1500 "called on %s, filter = %s, recursive = %d\n",
1501 fsp->fsp_name, filter_string, recursive));
1503 TALLOC_FREE(filter_string);
1506 if((!fsp->is_directory) || (conn != fsp->conn)) {
1507 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1511 if (fsp->notify == NULL) {
1513 status = change_notify_create(fsp, filter, recursive);
1515 if (!NT_STATUS_IS_OK(status)) {
1516 DEBUG(10, ("change_notify_create returned %s\n",
1517 nt_errstr(status)));
1518 reply_nterror(req, status);
1523 if (fsp->notify->num_changes != 0) {
1526 * We've got changes pending, respond immediately
1530 * TODO: write a torture test to check the filtering behaviour
1534 change_notify_reply(fsp->conn, req, max_param_count,
1538 * change_notify_reply() above has independently sent its
1545 * No changes pending, queue the request
1548 status = change_notify_add_request(req,
1552 if (!NT_STATUS_IS_OK(status)) {
1553 reply_nterror(req, status);
1558 /****************************************************************************
1559 Reply to an NT transact rename command.
1560 ****************************************************************************/
1562 static void call_nt_transact_rename(connection_struct *conn,
1563 struct smb_request *req,
1564 uint16 **ppsetup, uint32 setup_count,
1565 char **ppparams, uint32 parameter_count,
1566 char **ppdata, uint32 data_count,
1567 uint32 max_data_count)
1569 char *params = *ppparams;
1570 char *new_name = NULL;
1571 files_struct *fsp = NULL;
1572 bool dest_has_wcard = False;
1574 TALLOC_CTX *ctx = talloc_tos();
1576 if(parameter_count < 5) {
1577 reply_doserror(req, ERRDOS, ERRbadfunc);
1581 fsp = file_fsp(req, SVAL(params, 0));
1582 if (!check_fsp(conn, req, fsp)) {
1585 srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
1586 parameter_count - 4,
1587 STR_TERMINATE, &status, &dest_has_wcard);
1588 if (!NT_STATUS_IS_OK(status)) {
1589 reply_nterror(req, status);
1594 * W2K3 ignores this request as the RAW-RENAME test
1595 * demonstrates, so we do.
1597 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1599 DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1600 fsp->fsp_name, new_name));
1605 /******************************************************************************
1606 Fake up a completely empty SD.
1607 *******************************************************************************/
1609 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
1613 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1615 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1616 return NT_STATUS_NO_MEMORY;
1619 return NT_STATUS_OK;
1622 /****************************************************************************
1623 Reply to query a security descriptor.
1624 ****************************************************************************/
1626 static void call_nt_transact_query_security_desc(connection_struct *conn,
1627 struct smb_request *req,
1631 uint32 parameter_count,
1634 uint32 max_data_count)
1636 char *params = *ppparams;
1637 char *data = *ppdata;
1638 SEC_DESC *psd = NULL;
1640 uint32 security_info_wanted;
1641 files_struct *fsp = NULL;
1645 if(parameter_count < 8) {
1646 reply_doserror(req, ERRDOS, ERRbadfunc);
1650 fsp = file_fsp(req, SVAL(params,0));
1652 reply_doserror(req, ERRDOS, ERRbadfid);
1656 security_info_wanted = IVAL(params,4);
1658 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, info_wanted = 0x%x\n", fsp->fsp_name,
1659 (unsigned int)security_info_wanted ));
1661 params = nttrans_realloc(ppparams, 4);
1662 if(params == NULL) {
1663 reply_doserror(req, ERRDOS, ERRnomem);
1668 * Get the permissions to return.
1671 if (!lp_nt_acl_support(SNUM(conn))) {
1672 status = get_null_nt_acl(talloc_tos(), &psd);
1674 status = SMB_VFS_FGET_NT_ACL(
1675 fsp, security_info_wanted, &psd);
1677 if (!NT_STATUS_IS_OK(status)) {
1678 reply_nterror(req, status);
1682 /* If the SACL/DACL is NULL, but was requested, we mark that it is
1683 * present in the reply to match Windows behavior */
1684 if (psd->sacl == NULL &&
1685 security_info_wanted & SACL_SECURITY_INFORMATION)
1686 psd->type |= SEC_DESC_SACL_PRESENT;
1687 if (psd->dacl == NULL &&
1688 security_info_wanted & DACL_SECURITY_INFORMATION)
1689 psd->type |= SEC_DESC_DACL_PRESENT;
1691 sd_size = ndr_size_security_descriptor(psd, NULL, 0);
1693 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
1695 if (DEBUGLEVEL >= 10) {
1696 DEBUG(10,("call_nt_transact_query_security_desc for file %s\n", fsp->fsp_name));
1697 NDR_PRINT_DEBUG(security_descriptor, psd);
1700 SIVAL(params,0,(uint32)sd_size);
1702 if (max_data_count < sd_size) {
1703 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
1704 params, 4, *ppdata, 0);
1709 * Allocate the data we will point this at.
1712 data = nttrans_realloc(ppdata, sd_size);
1714 reply_doserror(req, ERRDOS, ERRnomem);
1718 status = marshall_sec_desc(talloc_tos(), psd,
1719 &blob.data, &blob.length);
1721 if (!NT_STATUS_IS_OK(status)) {
1722 reply_nterror(req, status);
1726 SMB_ASSERT(sd_size == blob.length);
1727 memcpy(data, blob.data, sd_size);
1729 send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
1734 /****************************************************************************
1735 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
1736 ****************************************************************************/
1738 static void call_nt_transact_set_security_desc(connection_struct *conn,
1739 struct smb_request *req,
1743 uint32 parameter_count,
1746 uint32 max_data_count)
1748 char *params= *ppparams;
1749 char *data = *ppdata;
1750 files_struct *fsp = NULL;
1751 uint32 security_info_sent = 0;
1754 if(parameter_count < 8) {
1755 reply_doserror(req, ERRDOS, ERRbadfunc);
1759 if((fsp = file_fsp(req, SVAL(params,0))) == NULL) {
1760 reply_doserror(req, ERRDOS, ERRbadfid);
1764 if(!lp_nt_acl_support(SNUM(conn))) {
1768 security_info_sent = IVAL(params,4);
1770 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
1771 (unsigned int)security_info_sent ));
1773 if (data_count == 0) {
1774 reply_doserror(req, ERRDOS, ERRnoaccess);
1778 status = set_sd(fsp, (uint8 *)data, data_count, security_info_sent);
1780 if (!NT_STATUS_IS_OK(status)) {
1781 reply_nterror(req, status);
1786 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1790 /****************************************************************************
1792 ****************************************************************************/
1794 static void call_nt_transact_ioctl(connection_struct *conn,
1795 struct smb_request *req,
1796 uint16 **ppsetup, uint32 setup_count,
1797 char **ppparams, uint32 parameter_count,
1798 char **ppdata, uint32 data_count,
1799 uint32 max_data_count)
1806 char *pdata = *ppdata;
1808 if (setup_count != 8) {
1809 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
1810 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1814 function = IVAL(*ppsetup, 0);
1815 fidnum = SVAL(*ppsetup, 4);
1816 isFSctl = CVAL(*ppsetup, 6);
1817 compfilter = CVAL(*ppsetup, 7);
1819 DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
1820 function, fidnum, isFSctl, compfilter));
1822 fsp=file_fsp(req, fidnum);
1823 /* this check is done in each implemented function case for now
1824 because I don't want to break anything... --metze
1825 FSP_BELONGS_CONN(fsp,conn);*/
1827 SMB_PERFCOUNT_SET_IOCTL(&req->pcd, function);
1830 case FSCTL_SET_SPARSE:
1831 /* pretend this succeeded - tho strictly we should
1832 mark the file sparse (if the local fs supports it)
1833 so we can know if we need to pre-allocate or not */
1835 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
1836 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1839 case FSCTL_CREATE_OR_GET_OBJECT_ID:
1841 unsigned char objid[16];
1843 /* This should return the object-id on this file.
1844 * I think I'll make this be the inode+dev. JRA.
1847 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
1849 if (!fsp_belongs_conn(conn, req, fsp)) {
1854 pdata = nttrans_realloc(ppdata, data_count);
1855 if (pdata == NULL) {
1856 reply_nterror(req, NT_STATUS_NO_MEMORY);
1859 push_file_id_16(pdata, &fsp->file_id);
1860 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
1861 push_file_id_16(pdata+32, &fsp->file_id);
1862 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
1867 case FSCTL_GET_REPARSE_POINT:
1868 /* pretend this fail - my winXP does it like this
1872 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1873 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
1876 case FSCTL_SET_REPARSE_POINT:
1877 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
1881 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1882 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
1885 case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
1888 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
1889 * and return their volume names. If max_data_count is 16, then it is just
1890 * asking for the number of volumes and length of the combined names.
1892 * pdata is the data allocated by our caller, but that uses
1893 * total_data_count (which is 0 in our case) rather than max_data_count.
1894 * Allocate the correct amount and return the pointer to let
1895 * it be deallocated when we return.
1897 SHADOW_COPY_DATA *shadow_data = NULL;
1898 TALLOC_CTX *shadow_mem_ctx = NULL;
1899 bool labels = False;
1900 uint32 labels_data_count = 0;
1904 if (!fsp_belongs_conn(conn, req, fsp)) {
1908 if (max_data_count < 16) {
1909 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
1911 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1915 if (max_data_count > 16) {
1919 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
1920 if (shadow_mem_ctx == NULL) {
1921 DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
1922 reply_nterror(req, NT_STATUS_NO_MEMORY);
1926 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
1927 if (shadow_data == NULL) {
1928 DEBUG(0,("TALLOC_ZERO() failed!\n"));
1929 talloc_destroy(shadow_mem_ctx);
1930 reply_nterror(req, NT_STATUS_NO_MEMORY);
1934 shadow_data->mem_ctx = shadow_mem_ctx;
1937 * Call the VFS routine to actually do the work.
1939 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
1940 talloc_destroy(shadow_data->mem_ctx);
1941 if (errno == ENOSYS) {
1942 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
1943 conn->connectpath));
1944 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1947 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
1948 conn->connectpath));
1949 reply_nterror(req, NT_STATUS_UNSUCCESSFUL);
1954 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
1959 data_count = 12+labels_data_count+4;
1962 if (max_data_count<data_count) {
1963 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
1964 max_data_count,data_count));
1965 talloc_destroy(shadow_data->mem_ctx);
1966 reply_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
1970 pdata = nttrans_realloc(ppdata, data_count);
1971 if (pdata == NULL) {
1972 talloc_destroy(shadow_data->mem_ctx);
1973 reply_nterror(req, NT_STATUS_NO_MEMORY);
1979 /* num_volumes 4 bytes */
1980 SIVAL(pdata,0,shadow_data->num_volumes);
1983 /* num_labels 4 bytes */
1984 SIVAL(pdata,4,shadow_data->num_volumes);
1987 /* needed_data_count 4 bytes */
1988 SIVAL(pdata,8,labels_data_count);
1992 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
1993 shadow_data->num_volumes,fsp->fsp_name));
1994 if (labels && shadow_data->labels) {
1995 for (i=0;i<shadow_data->num_volumes;i++) {
1996 srvstr_push(pdata, req->flags2,
1997 cur_pdata, shadow_data->labels[i],
1998 2*sizeof(SHADOW_COPY_LABEL),
1999 STR_UNICODE|STR_TERMINATE);
2000 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
2001 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
2005 talloc_destroy(shadow_data->mem_ctx);
2007 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
2013 case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
2015 /* pretend this succeeded -
2017 * we have to send back a list with all files owned by this SID
2019 * but I have to check that --metze
2023 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
2025 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
2027 if (!fsp_belongs_conn(conn, req, fsp)) {
2031 /* unknown 4 bytes: this is not the length of the sid :-( */
2032 /*unknown = IVAL(pdata,0);*/
2034 sid_parse(pdata+4,sid_len,&sid);
2035 DEBUGADD(10, ("for SID: %s\n", sid_string_dbg(&sid)));
2037 if (!sid_to_uid(&sid, &uid)) {
2038 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2039 sid_string_dbg(&sid),
2040 (unsigned long)sid_len));
2044 /* we can take a look at the find source :-)
2046 * find ./ -uid $uid -name '*' is what we need here
2049 * and send 4bytes len and then NULL terminated unicode strings
2052 * but I don't know how to deal with the paged results
2053 * (maybe we can hang the result anywhere in the fsp struct)
2055 * we don't send all files at once
2056 * and at the next we should *not* start from the beginning,
2057 * so we have to cache the result
2062 /* this works for now... */
2063 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2067 if (!logged_ioctl_message) {
2068 logged_ioctl_message = true; /* Only print this once... */
2069 DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
2074 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2078 #ifdef HAVE_SYS_QUOTAS
2079 /****************************************************************************
2080 Reply to get user quota
2081 ****************************************************************************/
2083 static void call_nt_transact_get_user_quota(connection_struct *conn,
2084 struct smb_request *req,
2088 uint32 parameter_count,
2091 uint32 max_data_count)
2093 NTSTATUS nt_status = NT_STATUS_OK;
2094 char *params = *ppparams;
2095 char *pdata = *ppdata;
2097 int data_len=0,param_len=0;
2100 files_struct *fsp = NULL;
2104 bool start_enum = True;
2105 SMB_NTQUOTA_STRUCT qt;
2106 SMB_NTQUOTA_LIST *tmp_list;
2107 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2112 if (conn->server_info->utok.uid != 0) {
2113 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2114 "[%s]\n", lp_servicename(SNUM(conn)),
2115 conn->server_info->unix_name));
2116 reply_doserror(req, ERRDOS, ERRnoaccess);
2121 * Ensure minimum number of parameters sent.
2124 if (parameter_count < 4) {
2125 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2126 reply_doserror(req, ERRDOS, ERRinvalidparam);
2130 /* maybe we can check the quota_fnum */
2131 fsp = file_fsp(req, SVAL(params,0));
2132 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2133 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2134 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2138 /* the NULL pointer checking for fsp->fake_file_handle->pd
2139 * is done by CHECK_NTQUOTA_HANDLE_OK()
2141 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->private_data;
2143 level = SVAL(params,2);
2145 /* unknown 12 bytes leading in params */
2148 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2149 /* seems that we should continue with the enum here --metze */
2151 if (qt_handle->quota_list!=NULL &&
2152 qt_handle->tmp_list==NULL) {
2155 free_ntquota_list(&(qt_handle->quota_list));
2157 /* Realloc the size of parameters and data we will return */
2159 params = nttrans_realloc(ppparams, param_len);
2160 if(params == NULL) {
2161 reply_doserror(req, ERRDOS, ERRnomem);
2166 SIVAL(params,0,data_len);
2173 case TRANSACT_GET_USER_QUOTA_LIST_START:
2175 if (qt_handle->quota_list==NULL &&
2176 qt_handle->tmp_list==NULL) {
2180 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2181 reply_doserror(req, ERRSRV, ERRerror);
2185 /* Realloc the size of parameters and data we will return */
2187 params = nttrans_realloc(ppparams, param_len);
2188 if(params == NULL) {
2189 reply_doserror(req, ERRDOS, ERRnomem);
2193 /* we should not trust the value in max_data_count*/
2194 max_data_count = MIN(max_data_count,2048);
2196 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2198 reply_doserror(req, ERRDOS, ERRnomem);
2204 /* set params Size of returned Quota Data 4 bytes*/
2205 /* but set it later when we know it */
2207 /* for each entry push the data */
2210 qt_handle->tmp_list = qt_handle->quota_list;
2213 tmp_list = qt_handle->tmp_list;
2215 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2216 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2218 sid_len = ndr_size_dom_sid(
2219 &tmp_list->quotas->sid, NULL, 0);
2220 entry_len = 40 + sid_len;
2222 /* nextoffset entry 4 bytes */
2223 SIVAL(entry,0,entry_len);
2225 /* then the len of the SID 4 bytes */
2226 SIVAL(entry,4,sid_len);
2228 /* unknown data 8 bytes uint64_t */
2229 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-metze*/
2231 /* the used disk space 8 bytes uint64_t */
2232 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2234 /* the soft quotas 8 bytes uint64_t */
2235 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2237 /* the hard quotas 8 bytes uint64_t */
2238 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2240 /* and now the SID */
2241 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2244 qt_handle->tmp_list = tmp_list;
2246 /* overwrite the offset of the last entry */
2247 SIVAL(entry-entry_len,0,0);
2249 data_len = 4+qt_len;
2250 /* overwrite the params quota_data_len */
2251 SIVAL(params,0,data_len);
2255 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2257 /* unknown 4 bytes IVAL(pdata,0) */
2259 if (data_count < 8) {
2260 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2261 reply_doserror(req, ERRDOS, ERRunknownlevel);
2265 sid_len = IVAL(pdata,4);
2266 /* Ensure this is less than 1mb. */
2267 if (sid_len > (1024*1024)) {
2268 reply_doserror(req, ERRDOS, ERRnomem);
2272 if (data_count < 8+sid_len) {
2273 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2274 reply_doserror(req, ERRDOS, ERRunknownlevel);
2278 data_len = 4+40+sid_len;
2280 if (max_data_count < data_len) {
2281 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2282 max_data_count, data_len));
2284 SIVAL(params,0,data_len);
2286 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2290 sid_parse(pdata+8,sid_len,&sid);
2292 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2295 * we have to return zero's in all fields
2296 * instead of returning an error here
2301 /* Realloc the size of parameters and data we will return */
2303 params = nttrans_realloc(ppparams, param_len);
2304 if(params == NULL) {
2305 reply_doserror(req, ERRDOS, ERRnomem);
2309 pdata = nttrans_realloc(ppdata, data_len);
2311 reply_doserror(req, ERRDOS, ERRnomem);
2317 /* set params Size of returned Quota Data 4 bytes*/
2318 SIVAL(params,0,data_len);
2320 /* nextoffset entry 4 bytes */
2323 /* then the len of the SID 4 bytes */
2324 SIVAL(entry,4,sid_len);
2326 /* unknown data 8 bytes uint64_t */
2327 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-mezte*/
2329 /* the used disk space 8 bytes uint64_t */
2330 SBIG_UINT(entry,16,qt.usedspace);
2332 /* the soft quotas 8 bytes uint64_t */
2333 SBIG_UINT(entry,24,qt.softlim);
2335 /* the hard quotas 8 bytes uint64_t */
2336 SBIG_UINT(entry,32,qt.hardlim);
2338 /* and now the SID */
2339 sid_linearize(entry+40, sid_len, &sid);
2344 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2345 reply_doserror(req, ERRSRV, ERRerror);
2350 send_nt_replies(conn, req, nt_status, params, param_len,
2354 /****************************************************************************
2355 Reply to set user quota
2356 ****************************************************************************/
2358 static void call_nt_transact_set_user_quota(connection_struct *conn,
2359 struct smb_request *req,
2363 uint32 parameter_count,
2366 uint32 max_data_count)
2368 char *params = *ppparams;
2369 char *pdata = *ppdata;
2370 int data_len=0,param_len=0;
2371 SMB_NTQUOTA_STRUCT qt;
2374 files_struct *fsp = NULL;
2379 if (conn->server_info->utok.uid != 0) {
2380 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2381 "[%s]\n", lp_servicename(SNUM(conn)),
2382 conn->server_info->unix_name));
2383 reply_doserror(req, ERRDOS, ERRnoaccess);
2388 * Ensure minimum number of parameters sent.
2391 if (parameter_count < 2) {
2392 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2393 reply_doserror(req, ERRDOS, ERRinvalidparam);
2397 /* maybe we can check the quota_fnum */
2398 fsp = file_fsp(req, SVAL(params,0));
2399 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2400 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2401 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2405 if (data_count < 40) {
2406 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2407 reply_doserror(req, ERRDOS, ERRunknownlevel);
2411 /* offset to next quota record.
2412 * 4 bytes IVAL(pdata,0)
2417 sid_len = IVAL(pdata,4);
2419 if (data_count < 40+sid_len) {
2420 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2421 reply_doserror(req, ERRDOS, ERRunknownlevel);
2425 /* unknown 8 bytes in pdata
2426 * maybe its the change time in NTTIME
2429 /* the used space 8 bytes (uint64_t)*/
2430 qt.usedspace = (uint64_t)IVAL(pdata,16);
2431 #ifdef LARGE_SMB_OFF_T
2432 qt.usedspace |= (((uint64_t)IVAL(pdata,20)) << 32);
2433 #else /* LARGE_SMB_OFF_T */
2434 if ((IVAL(pdata,20) != 0)&&
2435 ((qt.usedspace != 0xFFFFFFFF)||
2436 (IVAL(pdata,20)!=0xFFFFFFFF))) {
2437 /* more than 32 bits? */
2438 reply_doserror(req, ERRDOS, ERRunknownlevel);
2441 #endif /* LARGE_SMB_OFF_T */
2443 /* the soft quotas 8 bytes (uint64_t)*/
2444 qt.softlim = (uint64_t)IVAL(pdata,24);
2445 #ifdef LARGE_SMB_OFF_T
2446 qt.softlim |= (((uint64_t)IVAL(pdata,28)) << 32);
2447 #else /* LARGE_SMB_OFF_T */
2448 if ((IVAL(pdata,28) != 0)&&
2449 ((qt.softlim != 0xFFFFFFFF)||
2450 (IVAL(pdata,28)!=0xFFFFFFFF))) {
2451 /* more than 32 bits? */
2452 reply_doserror(req, ERRDOS, ERRunknownlevel);
2455 #endif /* LARGE_SMB_OFF_T */
2457 /* the hard quotas 8 bytes (uint64_t)*/
2458 qt.hardlim = (uint64_t)IVAL(pdata,32);
2459 #ifdef LARGE_SMB_OFF_T
2460 qt.hardlim |= (((uint64_t)IVAL(pdata,36)) << 32);
2461 #else /* LARGE_SMB_OFF_T */
2462 if ((IVAL(pdata,36) != 0)&&
2463 ((qt.hardlim != 0xFFFFFFFF)||
2464 (IVAL(pdata,36)!=0xFFFFFFFF))) {
2465 /* more than 32 bits? */
2466 reply_doserror(req, ERRDOS, ERRunknownlevel);
2469 #endif /* LARGE_SMB_OFF_T */
2471 sid_parse(pdata+40,sid_len,&sid);
2472 DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid)));
2474 /* 44 unknown bytes left... */
2476 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2477 reply_doserror(req, ERRSRV, ERRerror);
2481 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2484 #endif /* HAVE_SYS_QUOTAS */
2486 static void handle_nttrans(connection_struct *conn,
2487 struct trans_state *state,
2488 struct smb_request *req)
2490 if (Protocol >= PROTOCOL_NT1) {
2491 req->flags2 |= 0x40; /* IS_LONG_NAME */
2492 SSVAL(req->inbuf,smb_flg2,req->flags2);
2496 SMB_PERFCOUNT_SET_SUBOP(&req->pcd, state->call);
2498 /* Now we must call the relevant NT_TRANS function */
2499 switch(state->call) {
2500 case NT_TRANSACT_CREATE:
2502 START_PROFILE(NT_transact_create);
2503 call_nt_transact_create(
2505 &state->setup, state->setup_count,
2506 &state->param, state->total_param,
2507 &state->data, state->total_data,
2508 state->max_data_return);
2509 END_PROFILE(NT_transact_create);
2513 case NT_TRANSACT_IOCTL:
2515 START_PROFILE(NT_transact_ioctl);
2516 call_nt_transact_ioctl(
2518 &state->setup, state->setup_count,
2519 &state->param, state->total_param,
2520 &state->data, state->total_data,
2521 state->max_data_return);
2522 END_PROFILE(NT_transact_ioctl);
2526 case NT_TRANSACT_SET_SECURITY_DESC:
2528 START_PROFILE(NT_transact_set_security_desc);
2529 call_nt_transact_set_security_desc(
2531 &state->setup, state->setup_count,
2532 &state->param, state->total_param,
2533 &state->data, state->total_data,
2534 state->max_data_return);
2535 END_PROFILE(NT_transact_set_security_desc);
2539 case NT_TRANSACT_NOTIFY_CHANGE:
2541 START_PROFILE(NT_transact_notify_change);
2542 call_nt_transact_notify_change(
2544 &state->setup, state->setup_count,
2545 &state->param, state->total_param,
2546 &state->data, state->total_data,
2547 state->max_data_return,
2548 state->max_param_return);
2549 END_PROFILE(NT_transact_notify_change);
2553 case NT_TRANSACT_RENAME:
2555 START_PROFILE(NT_transact_rename);
2556 call_nt_transact_rename(
2558 &state->setup, state->setup_count,
2559 &state->param, state->total_param,
2560 &state->data, state->total_data,
2561 state->max_data_return);
2562 END_PROFILE(NT_transact_rename);
2566 case NT_TRANSACT_QUERY_SECURITY_DESC:
2568 START_PROFILE(NT_transact_query_security_desc);
2569 call_nt_transact_query_security_desc(
2571 &state->setup, state->setup_count,
2572 &state->param, state->total_param,
2573 &state->data, state->total_data,
2574 state->max_data_return);
2575 END_PROFILE(NT_transact_query_security_desc);
2579 #ifdef HAVE_SYS_QUOTAS
2580 case NT_TRANSACT_GET_USER_QUOTA:
2582 START_PROFILE(NT_transact_get_user_quota);
2583 call_nt_transact_get_user_quota(
2585 &state->setup, state->setup_count,
2586 &state->param, state->total_param,
2587 &state->data, state->total_data,
2588 state->max_data_return);
2589 END_PROFILE(NT_transact_get_user_quota);
2593 case NT_TRANSACT_SET_USER_QUOTA:
2595 START_PROFILE(NT_transact_set_user_quota);
2596 call_nt_transact_set_user_quota(
2598 &state->setup, state->setup_count,
2599 &state->param, state->total_param,
2600 &state->data, state->total_data,
2601 state->max_data_return);
2602 END_PROFILE(NT_transact_set_user_quota);
2605 #endif /* HAVE_SYS_QUOTAS */
2608 /* Error in request */
2609 DEBUG(0,("handle_nttrans: Unknown request %d in "
2610 "nttrans call\n", state->call));
2611 reply_doserror(req, ERRSRV, ERRerror);
2617 /****************************************************************************
2618 Reply to a SMBNTtrans.
2619 ****************************************************************************/
2621 void reply_nttrans(struct smb_request *req)
2623 connection_struct *conn = req->conn;
2628 uint16 function_code;
2630 struct trans_state *state;
2632 START_PROFILE(SMBnttrans);
2634 if (req->wct < 19) {
2635 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2636 END_PROFILE(SMBnttrans);
2640 pscnt = IVAL(req->vwv+9, 1);
2641 psoff = IVAL(req->vwv+11, 1);
2642 dscnt = IVAL(req->vwv+13, 1);
2643 dsoff = IVAL(req->vwv+15, 1);
2644 function_code = SVAL(req->vwv+18, 0);
2646 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2647 reply_doserror(req, ERRSRV, ERRaccess);
2648 END_PROFILE(SMBnttrans);
2652 result = allow_new_trans(conn->pending_trans, req->mid);
2653 if (!NT_STATUS_IS_OK(result)) {
2654 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
2655 reply_nterror(req, result);
2656 END_PROFILE(SMBnttrans);
2660 if ((state = TALLOC_P(conn, struct trans_state)) == NULL) {
2661 reply_doserror(req, ERRSRV, ERRaccess);
2662 END_PROFILE(SMBnttrans);
2666 state->cmd = SMBnttrans;
2668 state->mid = req->mid;
2669 state->vuid = req->vuid;
2670 state->total_data = IVAL(req->vwv+3, 1);
2672 state->total_param = IVAL(req->vwv+1, 1);
2673 state->param = NULL;
2674 state->max_data_return = IVAL(req->vwv+7, 1);
2675 state->max_param_return = IVAL(req->vwv+5, 1);
2677 /* setup count is in *words* */
2678 state->setup_count = 2*CVAL(req->vwv+17, 1);
2679 state->setup = NULL;
2680 state->call = function_code;
2682 DEBUG(10, ("num_setup=%u, "
2683 "param_total=%u, this_param=%u, max_param=%u, "
2684 "data_total=%u, this_data=%u, max_data=%u, "
2685 "param_offset=%u, data_offset=%u\n",
2686 (unsigned)state->setup_count,
2687 (unsigned)state->total_param, (unsigned)pscnt,
2688 (unsigned)state->max_param_return,
2689 (unsigned)state->total_data, (unsigned)dscnt,
2690 (unsigned)state->max_data_return,
2691 (unsigned)psoff, (unsigned)dsoff));
2694 * All nttrans messages we handle have smb_wct == 19 +
2695 * state->setup_count. Ensure this is so as a sanity check.
2698 if(req->wct != 19 + (state->setup_count/2)) {
2699 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2700 req->wct, 19 + (state->setup_count/2)));
2704 /* Don't allow more than 128mb for each value. */
2705 if ((state->total_data > (1024*1024*128)) ||
2706 (state->total_param > (1024*1024*128))) {
2707 reply_doserror(req, ERRDOS, ERRnomem);
2708 END_PROFILE(SMBnttrans);
2712 if ((dscnt > state->total_data) || (pscnt > state->total_param))
2715 if (state->total_data) {
2717 if (trans_oob(state->total_data, 0, dscnt)
2718 || trans_oob(smb_len(req->inbuf), dsoff, dscnt)) {
2722 /* Can't use talloc here, the core routines do realloc on the
2723 * params and data. */
2724 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
2725 DEBUG(0,("reply_nttrans: data malloc fail for %u "
2726 "bytes !\n", (unsigned int)state->total_data));
2728 reply_doserror(req, ERRDOS, ERRnomem);
2729 END_PROFILE(SMBnttrans);
2733 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
2736 if (state->total_param) {
2738 if (trans_oob(state->total_param, 0, pscnt)
2739 || trans_oob(smb_len(req->inbuf), psoff, pscnt)) {
2743 /* Can't use talloc here, the core routines do realloc on the
2744 * params and data. */
2745 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
2746 DEBUG(0,("reply_nttrans: param malloc fail for %u "
2747 "bytes !\n", (unsigned int)state->total_param));
2748 SAFE_FREE(state->data);
2750 reply_doserror(req, ERRDOS, ERRnomem);
2751 END_PROFILE(SMBnttrans);
2755 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
2758 state->received_data = dscnt;
2759 state->received_param = pscnt;
2761 if(state->setup_count > 0) {
2762 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2763 state->setup_count));
2766 * No overflow possible here, state->setup_count is an
2767 * unsigned int, being filled by a single byte from
2768 * CVAL(req->vwv+13, 0) above. The cast in the comparison
2769 * below is not necessary, it's here to clarify things. The
2770 * validity of req->vwv and req->wct has been checked in
2771 * init_smb_request already.
2773 if ((state->setup_count/2) + 19 > (unsigned int)req->wct) {
2777 state->setup = (uint16 *)TALLOC(state, state->setup_count);
2778 if (state->setup == NULL) {
2779 DEBUG(0,("reply_nttrans : Out of memory\n"));
2780 SAFE_FREE(state->data);
2781 SAFE_FREE(state->param);
2783 reply_doserror(req, ERRDOS, ERRnomem);
2784 END_PROFILE(SMBnttrans);
2788 memcpy(state->setup, req->vwv+19, state->setup_count);
2789 dump_data(10, (uint8 *)state->setup, state->setup_count);
2792 if ((state->received_data == state->total_data) &&
2793 (state->received_param == state->total_param)) {
2794 handle_nttrans(conn, state, req);
2795 SAFE_FREE(state->param);
2796 SAFE_FREE(state->data);
2798 END_PROFILE(SMBnttrans);
2802 DLIST_ADD(conn->pending_trans, state);
2804 /* We need to send an interim response then receive the rest
2805 of the parameter/data bytes */
2806 reply_outbuf(req, 0, 0);
2807 show_msg((char *)req->outbuf);
2808 END_PROFILE(SMBnttrans);
2813 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
2814 SAFE_FREE(state->data);
2815 SAFE_FREE(state->param);
2817 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2818 END_PROFILE(SMBnttrans);
2822 /****************************************************************************
2823 Reply to a SMBnttranss
2824 ****************************************************************************/
2826 void reply_nttranss(struct smb_request *req)
2828 connection_struct *conn = req->conn;
2829 uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
2830 struct trans_state *state;
2832 START_PROFILE(SMBnttranss);
2834 show_msg((char *)req->inbuf);
2836 if (req->wct < 18) {
2837 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2838 END_PROFILE(SMBnttranss);
2842 for (state = conn->pending_trans; state != NULL;
2843 state = state->next) {
2844 if (state->mid == req->mid) {
2849 if ((state == NULL) || (state->cmd != SMBnttrans)) {
2850 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2851 END_PROFILE(SMBnttranss);
2855 /* Revise state->total_param and state->total_data in case they have
2856 changed downwards */
2857 if (IVAL(req->vwv+1, 1) < state->total_param) {
2858 state->total_param = IVAL(req->vwv+1, 1);
2860 if (IVAL(req->vwv+3, 1) < state->total_data) {
2861 state->total_data = IVAL(req->vwv+3, 1);
2864 pcnt = IVAL(req->vwv+5, 1);
2865 poff = IVAL(req->vwv+7, 1);
2866 pdisp = IVAL(req->vwv+9, 1);
2868 dcnt = IVAL(req->vwv+11, 1);
2869 doff = IVAL(req->vwv+13, 1);
2870 ddisp = IVAL(req->vwv+15, 1);
2872 state->received_param += pcnt;
2873 state->received_data += dcnt;
2875 if ((state->received_data > state->total_data) ||
2876 (state->received_param > state->total_param))
2880 if (trans_oob(state->total_param, pdisp, pcnt)
2881 || trans_oob(smb_len(req->inbuf), poff, pcnt)) {
2884 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,pcnt);
2888 if (trans_oob(state->total_data, ddisp, dcnt)
2889 || trans_oob(smb_len(req->inbuf), doff, dcnt)) {
2892 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
2895 if ((state->received_param < state->total_param) ||
2896 (state->received_data < state->total_data)) {
2897 END_PROFILE(SMBnttranss);
2901 handle_nttrans(conn, state, req);
2903 DLIST_REMOVE(conn->pending_trans, state);
2904 SAFE_FREE(state->data);
2905 SAFE_FREE(state->param);
2907 END_PROFILE(SMBnttranss);
2912 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
2913 DLIST_REMOVE(conn->pending_trans, state);
2914 SAFE_FREE(state->data);
2915 SAFE_FREE(state->param);
2917 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2918 END_PROFILE(SMBnttranss);