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(),
234 IS_CONN_ENCRYPTED(conn),
236 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
239 TALLOC_FREE(req->outbuf);
241 pp += params_sent_thistime;
242 pd += data_sent_thistime;
244 params_to_send -= params_sent_thistime;
245 data_to_send -= data_sent_thistime;
251 if(params_to_send < 0 || data_to_send < 0) {
252 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
253 params_to_send, data_to_send));
254 exit_server_cleanly("send_nt_replies: internal error");
259 /****************************************************************************
260 Is it an NTFS stream name ?
261 An NTFS file name is <path>.<extention>:<stream name>:<stream type>
262 $DATA can be used as both a stream name and a stream type. A missing stream
263 name or type implies $DATA.
265 Both Windows stream names and POSIX files can contain the ':' character.
266 This function first checks for the existence of a colon in the last component
267 of the given name. If the name contains a colon we differentiate between a
268 stream and POSIX file by checking if the latter exists through a POSIX stat.
270 Function assumes we've already chdir() to the "root" directory of fname.
271 ****************************************************************************/
273 bool is_ntfs_stream_name(const char *fname)
275 const char *lastcomp;
276 SMB_STRUCT_STAT sbuf;
278 /* If all pathnames are treated as POSIX we ignore streams. */
279 if (lp_posix_pathnames()) {
283 /* Find the last component of the name. */
284 if ((lastcomp = strrchr_m(fname, '/')) != NULL)
289 /* If there is no colon in the last component, it's not a stream. */
290 if (strchr_m(lastcomp, ':') == NULL)
294 * If file already exists on disk, it's not a stream. The stat must
295 * bypass the vfs layer so streams modules don't intefere.
297 if (sys_stat(fname, &sbuf) == 0) {
298 DEBUG(5, ("is_ntfs_stream_name: file %s contains a ':' but is "
299 "not a stream\n", fname));
306 /****************************************************************************
307 Reply to an NT create and X call on a pipe
308 ****************************************************************************/
310 static void nt_open_pipe(char *fname, connection_struct *conn,
311 struct smb_request *req, int *ppnum)
316 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
318 /* Strip \\ off the name. */
321 status = open_np_file(req, fname, &fsp);
322 if (!NT_STATUS_IS_OK(status)) {
323 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
324 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
328 reply_nterror(req, status);
336 /****************************************************************************
337 Reply to an NT create and X call for pipes.
338 ****************************************************************************/
340 static void do_ntcreate_pipe_open(connection_struct *conn,
341 struct smb_request *req)
346 uint32 flags = IVAL(req->vwv+3, 1);
347 TALLOC_CTX *ctx = talloc_tos();
349 srvstr_pull_req_talloc(ctx, req, &fname, req->buf, STR_TERMINATE);
352 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
356 nt_open_pipe(fname, conn, req, &pnum);
364 * Deal with pipe return.
367 if (flags & EXTENDED_RESPONSE_REQUIRED) {
368 /* This is very strange. We
369 * return 50 words, but only set
370 * the wcnt to 42 ? It's definately
371 * what happens on the wire....
373 reply_outbuf(req, 50, 0);
374 SCVAL(req->outbuf,smb_wct,42);
376 reply_outbuf(req, 34, 0);
379 p = (char *)req->outbuf + smb_vwv2;
383 SIVAL(p,0,FILE_WAS_OPENED);
386 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
389 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
391 SSVAL(p,2, 0x5FF); /* ? */
394 if (flags & EXTENDED_RESPONSE_REQUIRED) {
396 SIVAL(p,0,FILE_GENERIC_ALL);
398 * For pipes W2K3 seems to return
400 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
402 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
405 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
410 /****************************************************************************
411 Reply to an NT create and X call.
412 ****************************************************************************/
414 void reply_ntcreate_and_X(struct smb_request *req)
416 connection_struct *conn = req->conn;
420 uint32 file_attributes;
422 uint32 create_disposition;
423 uint32 create_options;
425 uint64_t allocation_size;
426 /* Breakout the oplock request bits so we can set the
427 reply bits separately. */
429 SMB_OFF_T file_len = 0;
430 SMB_STRUCT_STAT sbuf;
432 files_struct *fsp = NULL;
434 struct timespec c_timespec;
435 struct timespec a_timespec;
436 struct timespec m_timespec;
439 uint8_t oplock_granted = NO_OPLOCK_RETURN;
440 TALLOC_CTX *ctx = talloc_tos();
442 START_PROFILE(SMBntcreateX);
445 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
449 flags = IVAL(req->vwv+3, 1);
450 access_mask = IVAL(req->vwv+7, 1);
451 file_attributes = IVAL(req->vwv+13, 1);
452 share_access = IVAL(req->vwv+15, 1);
453 create_disposition = IVAL(req->vwv+17, 1);
454 create_options = IVAL(req->vwv+19, 1);
455 root_dir_fid = (uint16)IVAL(req->vwv+5, 1);
457 allocation_size = (uint64_t)IVAL(req->vwv+9, 1);
458 #ifdef LARGE_SMB_OFF_T
459 allocation_size |= (((uint64_t)IVAL(req->vwv+11, 1)) << 32);
462 srvstr_get_path_req(ctx, req, &fname, (const char *)req->buf,
463 STR_TERMINATE, &status);
465 if (!NT_STATUS_IS_OK(status)) {
466 reply_nterror(req, status);
467 END_PROFILE(SMBntcreateX);
471 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
472 "file_attributes = 0x%x, share_access = 0x%x, "
473 "create_disposition = 0x%x create_options = 0x%x "
474 "root_dir_fid = 0x%x, fname = %s\n",
476 (unsigned int)access_mask,
477 (unsigned int)file_attributes,
478 (unsigned int)share_access,
479 (unsigned int)create_disposition,
480 (unsigned int)create_options,
481 (unsigned int)root_dir_fid,
485 * we need to remove ignored bits when they come directly from the client
486 * because we reuse some of them for internal stuff
488 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
491 * If it's an IPC, use the pipe handler.
495 if (lp_nt_pipe_support()) {
496 do_ntcreate_pipe_open(conn, req);
497 END_PROFILE(SMBntcreateX);
500 reply_doserror(req, ERRDOS, ERRnoaccess);
501 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 = SMB_VFS_CREATE_FILE(
514 root_dir_fid, /* root_dir_fid */
516 CFF_DOS_PATH, /* create_file_flags */
517 access_mask, /* access_mask */
518 share_access, /* share_access */
519 create_disposition, /* create_disposition*/
520 create_options, /* create_options */
521 file_attributes, /* file_attributes */
522 oplock_request, /* oplock_request */
523 allocation_size, /* allocation_size */
530 if (!NT_STATUS_IS_OK(status)) {
531 if (open_was_deferred(req->mid)) {
532 /* We have re-scheduled this call, no error. */
533 END_PROFILE(SMBntcreateX);
536 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
537 reply_botherror(req, status, ERRDOS, ERRfilexists);
540 reply_nterror(req, status);
542 END_PROFILE(SMBntcreateX);
547 * If the caller set the extended oplock request bit
548 * and we granted one (by whatever means) - set the
549 * correct bit for extended oplock reply.
552 if (oplock_request &&
553 (lp_fake_oplocks(SNUM(conn))
554 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
557 * Exclusive oplock granted
560 if (flags & REQUEST_BATCH_OPLOCK) {
561 oplock_granted = BATCH_OPLOCK_RETURN;
563 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
565 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
566 oplock_granted = LEVEL_II_OPLOCK_RETURN;
568 oplock_granted = NO_OPLOCK_RETURN;
571 file_len = sbuf.st_size;
572 fattr = dos_mode(conn,fsp->fsp_name,&sbuf);
574 fattr = FILE_ATTRIBUTE_NORMAL;
577 if (flags & EXTENDED_RESPONSE_REQUIRED) {
578 /* This is very strange. We
579 * return 50 words, but only set
580 * the wcnt to 42 ? It's definately
581 * what happens on the wire....
583 reply_outbuf(req, 50, 0);
584 SCVAL(req->outbuf,smb_wct,42);
586 reply_outbuf(req, 34, 0);
589 p = (char *)req->outbuf + smb_vwv2;
591 SCVAL(p, 0, oplock_granted);
594 SSVAL(p,0,fsp->fnum);
596 if ((create_disposition == FILE_SUPERSEDE)
597 && (info == FILE_WAS_OVERWRITTEN)) {
598 SIVAL(p,0,FILE_WAS_SUPERSEDED);
605 c_timespec = get_create_timespec(
606 &sbuf,lp_fake_dir_create_times(SNUM(conn)));
607 a_timespec = get_atimespec(&sbuf);
608 m_timespec = get_mtimespec(&sbuf);
610 if (lp_dos_filetime_resolution(SNUM(conn))) {
611 dos_filetime_timespec(&c_timespec);
612 dos_filetime_timespec(&a_timespec);
613 dos_filetime_timespec(&m_timespec);
616 put_long_date_timespec(p, c_timespec); /* create time. */
618 put_long_date_timespec(p, a_timespec); /* access time */
620 put_long_date_timespec(p, m_timespec); /* write time */
622 put_long_date_timespec(p, m_timespec); /* change time */
624 SIVAL(p,0,fattr); /* File Attributes. */
626 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn,fsp,&sbuf));
628 SOFF_T(p,0,file_len);
630 if (flags & EXTENDED_RESPONSE_REQUIRED) {
634 SCVAL(p,0,fsp->is_directory ? 1 : 0);
636 if (flags & EXTENDED_RESPONSE_REQUIRED) {
639 if (fsp->is_directory
640 || can_write_to_file(conn, fsp->fsp_name, &sbuf)) {
641 perms = FILE_GENERIC_ALL;
643 perms = FILE_GENERIC_READ|FILE_EXECUTE;
648 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n",
649 fsp->fnum, fsp->fsp_name));
652 END_PROFILE(SMBntcreateX);
656 /****************************************************************************
657 Reply to a NT_TRANSACT_CREATE call to open a pipe.
658 ****************************************************************************/
660 static void do_nt_transact_create_pipe(connection_struct *conn,
661 struct smb_request *req,
662 uint16 **ppsetup, uint32 setup_count,
663 char **ppparams, uint32 parameter_count,
664 char **ppdata, uint32 data_count)
667 char *params = *ppparams;
673 TALLOC_CTX *ctx = talloc_tos();
676 * Ensure minimum number of parameters sent.
679 if(parameter_count < 54) {
680 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
681 reply_doserror(req, ERRDOS, ERRnoaccess);
685 flags = IVAL(params,0);
687 srvstr_get_path(ctx, params, req->flags2, &fname, params+53,
688 parameter_count-53, STR_TERMINATE,
690 if (!NT_STATUS_IS_OK(status)) {
691 reply_nterror(req, status);
695 nt_open_pipe(fname, conn, req, &pnum);
702 /* Realloc the size of parameters and data we will return */
703 if (flags & EXTENDED_RESPONSE_REQUIRED) {
704 /* Extended response is 32 more byyes. */
709 params = nttrans_realloc(ppparams, param_len);
711 reply_doserror(req, ERRDOS, ERRnomem);
716 SCVAL(p,0,NO_OPLOCK_RETURN);
721 SIVAL(p,0,FILE_WAS_OPENED);
725 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
728 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
730 SSVAL(p,2, 0x5FF); /* ? */
733 if (flags & EXTENDED_RESPONSE_REQUIRED) {
735 SIVAL(p,0,FILE_GENERIC_ALL);
737 * For pipes W2K3 seems to return
739 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
741 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
744 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
746 /* Send the required number of replies */
747 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
752 /****************************************************************************
753 Internal fn to set security descriptors.
754 ****************************************************************************/
756 static NTSTATUS set_sd(files_struct *fsp, uint8 *data, uint32 sd_len,
757 uint32 security_info_sent)
759 SEC_DESC *psd = NULL;
762 if (sd_len == 0 || !lp_nt_acl_support(SNUM(fsp->conn))) {
766 status = unmarshall_sec_desc(talloc_tos(), data, sd_len, &psd);
768 if (!NT_STATUS_IS_OK(status)) {
772 if (psd->owner_sid == NULL) {
773 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
775 if (psd->group_sid == NULL) {
776 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
779 /* Convert all the generic bits. */
780 security_acl_map_generic(psd->dacl, &file_generic_mapping);
781 security_acl_map_generic(psd->sacl, &file_generic_mapping);
783 if (DEBUGLEVEL >= 10) {
784 DEBUG(10,("set_sd for file %s\n", fsp->fsp_name ));
785 NDR_PRINT_DEBUG(security_descriptor, psd);
788 status = SMB_VFS_FSET_NT_ACL(fsp, security_info_sent, psd);
795 /****************************************************************************
796 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
797 ****************************************************************************/
799 static struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
801 struct ea_list *ea_list_head = NULL;
808 while (offset + 4 <= data_size) {
809 size_t next_offset = IVAL(pdata,offset);
810 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
816 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
817 if (next_offset == 0) {
820 offset += next_offset;
826 /****************************************************************************
827 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
828 ****************************************************************************/
830 static void call_nt_transact_create(connection_struct *conn,
831 struct smb_request *req,
832 uint16 **ppsetup, uint32 setup_count,
833 char **ppparams, uint32 parameter_count,
834 char **ppdata, uint32 data_count,
835 uint32 max_data_count)
838 char *params = *ppparams;
839 char *data = *ppdata;
840 /* Breakout the oplock request bits so we can set the reply bits separately. */
842 SMB_OFF_T file_len = 0;
843 SMB_STRUCT_STAT sbuf;
845 files_struct *fsp = NULL;
849 uint32 file_attributes;
851 uint32 create_disposition;
852 uint32 create_options;
854 struct security_descriptor *sd = NULL;
857 struct timespec c_timespec;
858 struct timespec a_timespec;
859 struct timespec m_timespec;
860 struct ea_list *ea_list = NULL;
863 uint64_t allocation_size;
865 uint8_t oplock_granted;
866 TALLOC_CTX *ctx = talloc_tos();
868 DEBUG(5,("call_nt_transact_create\n"));
871 * If it's an IPC, use the pipe handler.
875 if (lp_nt_pipe_support()) {
876 do_nt_transact_create_pipe(
878 ppsetup, setup_count,
879 ppparams, parameter_count,
883 reply_doserror(req, ERRDOS, ERRnoaccess);
888 * Ensure minimum number of parameters sent.
891 if(parameter_count < 54) {
892 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
893 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
897 flags = IVAL(params,0);
898 access_mask = IVAL(params,8);
899 file_attributes = IVAL(params,20);
900 share_access = IVAL(params,24);
901 create_disposition = IVAL(params,28);
902 create_options = IVAL(params,32);
903 sd_len = IVAL(params,36);
904 ea_len = IVAL(params,40);
905 root_dir_fid = (uint16)IVAL(params,4);
906 allocation_size = (uint64_t)IVAL(params,12);
907 #ifdef LARGE_SMB_OFF_T
908 allocation_size |= (((uint64_t)IVAL(params,16)) << 32);
912 * we need to remove ignored bits when they come directly from the client
913 * because we reuse some of them for internal stuff
915 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
917 /* Ensure the data_len is correct for the sd and ea values given. */
918 if ((ea_len + sd_len > data_count)
919 || (ea_len > data_count) || (sd_len > data_count)
920 || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
921 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
922 "%u, data_count = %u\n", (unsigned int)ea_len,
923 (unsigned int)sd_len, (unsigned int)data_count));
924 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
929 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
932 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
934 if (!NT_STATUS_IS_OK(status)) {
935 DEBUG(10, ("call_nt_transact_create: "
936 "unmarshall_sec_desc failed: %s\n",
938 reply_nterror(req, status);
944 if (!lp_ea_support(SNUM(conn))) {
945 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
946 "EA's not supported.\n",
947 (unsigned int)ea_len));
948 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
953 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
954 "too small (should be more than 10)\n",
955 (unsigned int)ea_len ));
956 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
960 /* We have already checked that ea_len <= data_count here. */
961 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
963 if (ea_list == NULL) {
964 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
969 srvstr_get_path(ctx, params, req->flags2, &fname,
970 params+53, parameter_count-53,
971 STR_TERMINATE, &status);
972 if (!NT_STATUS_IS_OK(status)) {
973 reply_nterror(req, status);
977 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
978 if (oplock_request) {
979 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
983 status = SMB_VFS_CREATE_FILE(
986 root_dir_fid, /* root_dir_fid */
988 CFF_DOS_PATH, /* create_file_flags */
989 access_mask, /* access_mask */
990 share_access, /* share_access */
991 create_disposition, /* create_disposition*/
992 create_options, /* create_options */
993 file_attributes, /* file_attributes */
994 oplock_request, /* oplock_request */
995 allocation_size, /* allocation_size */
997 ea_list, /* ea_list */
1002 if(!NT_STATUS_IS_OK(status)) {
1003 if (open_was_deferred(req->mid)) {
1004 /* We have re-scheduled this call, no error. */
1007 reply_openerror(req, status);
1012 * If the caller set the extended oplock request bit
1013 * and we granted one (by whatever means) - set the
1014 * correct bit for extended oplock reply.
1017 if (oplock_request &&
1018 (lp_fake_oplocks(SNUM(conn))
1019 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
1022 * Exclusive oplock granted
1025 if (flags & REQUEST_BATCH_OPLOCK) {
1026 oplock_granted = BATCH_OPLOCK_RETURN;
1028 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
1030 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1031 oplock_granted = LEVEL_II_OPLOCK_RETURN;
1033 oplock_granted = NO_OPLOCK_RETURN;
1036 file_len = sbuf.st_size;
1037 fattr = dos_mode(conn,fsp->fsp_name,&sbuf);
1039 fattr = FILE_ATTRIBUTE_NORMAL;
1042 /* Realloc the size of parameters and data we will return */
1043 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1044 /* Extended response is 32 more byyes. */
1049 params = nttrans_realloc(ppparams, param_len);
1050 if(params == NULL) {
1051 reply_doserror(req, ERRDOS, ERRnomem);
1056 SCVAL(p, 0, oplock_granted);
1059 SSVAL(p,0,fsp->fnum);
1061 if ((create_disposition == FILE_SUPERSEDE)
1062 && (info == FILE_WAS_OVERWRITTEN)) {
1063 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1070 c_timespec = get_create_timespec(
1071 &sbuf,lp_fake_dir_create_times(SNUM(conn)));
1072 a_timespec = get_atimespec(&sbuf);
1073 m_timespec = get_mtimespec(&sbuf);
1075 if (lp_dos_filetime_resolution(SNUM(conn))) {
1076 dos_filetime_timespec(&c_timespec);
1077 dos_filetime_timespec(&a_timespec);
1078 dos_filetime_timespec(&m_timespec);
1081 put_long_date_timespec(p, c_timespec); /* create time. */
1083 put_long_date_timespec(p, a_timespec); /* access time */
1085 put_long_date_timespec(p, m_timespec); /* write time */
1087 put_long_date_timespec(p, m_timespec); /* change time */
1089 SIVAL(p,0,fattr); /* File Attributes. */
1091 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn,fsp,&sbuf));
1093 SOFF_T(p,0,file_len);
1095 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1099 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1101 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1104 if (fsp->is_directory
1105 || can_write_to_file(conn, fsp->fsp_name, &sbuf)) {
1106 perms = FILE_GENERIC_ALL;
1108 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1113 DEBUG(5,("call_nt_transact_create: open name = %s\n", fsp->fsp_name));
1115 /* Send the required number of replies */
1116 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1121 /****************************************************************************
1122 Reply to a NT CANCEL request.
1123 conn POINTER CAN BE NULL HERE !
1124 ****************************************************************************/
1126 void reply_ntcancel(struct smb_request *req)
1129 * Go through and cancel any pending change notifies.
1132 START_PROFILE(SMBntcancel);
1133 srv_cancel_sign_response(smbd_server_conn);
1134 remove_pending_change_notify_requests_by_mid(req->mid);
1135 remove_pending_lock_requests_by_mid(req->mid);
1137 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", req->mid));
1139 END_PROFILE(SMBntcancel);
1143 /****************************************************************************
1145 ****************************************************************************/
1147 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
1148 connection_struct *conn,
1149 struct smb_request *req,
1150 const char *oldname_in,
1151 const char *newname_in,
1154 SMB_STRUCT_STAT sbuf1, sbuf2;
1155 char *oldname = NULL;
1156 char *newname = NULL;
1157 char *last_component_oldname = NULL;
1158 char *last_component_newname = NULL;
1159 files_struct *fsp1,*fsp2;
1163 NTSTATUS status = NT_STATUS_OK;
1169 if (!CAN_WRITE(conn)) {
1170 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1173 status = unix_convert(ctx, conn, oldname_in, False, &oldname,
1174 &last_component_oldname, &sbuf1);
1175 if (!NT_STATUS_IS_OK(status)) {
1179 status = check_name(conn, oldname);
1180 if (!NT_STATUS_IS_OK(status)) {
1184 /* Source must already exist. */
1185 if (!VALID_STAT(sbuf1)) {
1186 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1188 /* Ensure attributes match. */
1189 fattr = dos_mode(conn,oldname,&sbuf1);
1190 if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
1191 return NT_STATUS_NO_SUCH_FILE;
1194 status = unix_convert(ctx, conn, newname_in, False, &newname,
1195 &last_component_newname, &sbuf2);
1196 if (!NT_STATUS_IS_OK(status)) {
1200 status = check_name(conn, newname);
1201 if (!NT_STATUS_IS_OK(status)) {
1205 /* Disallow if newname already exists. */
1206 if (VALID_STAT(sbuf2)) {
1207 return NT_STATUS_OBJECT_NAME_COLLISION;
1210 /* No links from a directory. */
1211 if (S_ISDIR(sbuf1.st_mode)) {
1212 return NT_STATUS_FILE_IS_A_DIRECTORY;
1215 /* Ensure this is within the share. */
1216 status = check_reduced_name(conn, oldname);
1217 if (!NT_STATUS_IS_OK(status)) {
1221 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1224 status = SMB_VFS_CREATE_FILE(
1227 0, /* root_dir_fid */
1228 oldname, /* fname */
1229 0, /* create_file_flags */
1230 FILE_READ_DATA, /* access_mask */
1231 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1233 FILE_OPEN, /* create_disposition*/
1234 0, /* create_options */
1235 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
1236 NO_OPLOCK, /* oplock_request */
1237 0, /* allocation_size */
1242 &sbuf1); /* psbuf */
1244 if (!NT_STATUS_IS_OK(status)) {
1248 status = SMB_VFS_CREATE_FILE(
1251 0, /* root_dir_fid */
1252 newname, /* fname */
1253 0, /* create_file_flags */
1254 FILE_WRITE_DATA, /* access_mask */
1255 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1257 FILE_CREATE, /* create_disposition*/
1258 0, /* create_options */
1259 fattr, /* file_attributes */
1260 NO_OPLOCK, /* oplock_request */
1261 0, /* allocation_size */
1266 &sbuf2); /* psbuf */
1268 if (!NT_STATUS_IS_OK(status)) {
1269 close_file(NULL, fsp1, ERROR_CLOSE);
1273 if (sbuf1.st_size) {
1274 ret = vfs_transfer_file(fsp1, fsp2, sbuf1.st_size);
1278 * As we are opening fsp1 read-only we only expect
1279 * an error on close on fsp2 if we are out of space.
1280 * Thus we don't look at the error return from the
1283 close_file(NULL, fsp1, NORMAL_CLOSE);
1285 /* Ensure the modtime is set correctly on the destination file. */
1286 set_close_write_time(fsp2, get_mtimespec(&sbuf1));
1288 status = close_file(NULL, fsp2, NORMAL_CLOSE);
1290 /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
1291 creates the file. This isn't the correct thing to do in the copy
1293 if (!parent_dirname(talloc_tos(), newname, &parent, NULL)) {
1294 return NT_STATUS_NO_MEMORY;
1296 file_set_dosmode(conn, newname, fattr, &sbuf2, parent, false);
1297 TALLOC_FREE(parent);
1299 if (ret < (SMB_OFF_T)sbuf1.st_size) {
1300 return NT_STATUS_DISK_FULL;
1303 if (!NT_STATUS_IS_OK(status)) {
1304 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1305 nt_errstr(status), oldname, newname));
1310 /****************************************************************************
1311 Reply to a NT rename request.
1312 ****************************************************************************/
1314 void reply_ntrename(struct smb_request *req)
1316 connection_struct *conn = req->conn;
1317 char *oldname = NULL;
1318 char *newname = NULL;
1321 bool src_has_wcard = False;
1322 bool dest_has_wcard = False;
1325 TALLOC_CTX *ctx = talloc_tos();
1327 START_PROFILE(SMBntrename);
1330 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1331 END_PROFILE(SMBntrename);
1335 attrs = SVAL(req->vwv+0, 0);
1336 rename_type = SVAL(req->vwv+1, 0);
1338 p = (const char *)req->buf + 1;
1339 p += srvstr_get_path_req_wcard(ctx, req, &oldname, p, STR_TERMINATE,
1340 &status, &src_has_wcard);
1341 if (!NT_STATUS_IS_OK(status)) {
1342 reply_nterror(req, status);
1343 END_PROFILE(SMBntrename);
1347 if (ms_has_wild(oldname)) {
1348 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1349 END_PROFILE(SMBntrename);
1354 p += srvstr_get_path_req_wcard(ctx, req, &newname, p, STR_TERMINATE,
1355 &status, &dest_has_wcard);
1356 if (!NT_STATUS_IS_OK(status)) {
1357 reply_nterror(req, status);
1358 END_PROFILE(SMBntrename);
1362 status = resolve_dfspath(ctx, conn,
1363 req->flags2 & FLAGS2_DFS_PATHNAMES,
1366 if (!NT_STATUS_IS_OK(status)) {
1367 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1368 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1369 ERRSRV, ERRbadpath);
1370 END_PROFILE(SMBntrename);
1373 reply_nterror(req, status);
1374 END_PROFILE(SMBntrename);
1378 status = resolve_dfspath(ctx, conn,
1379 req->flags2 & FLAGS2_DFS_PATHNAMES,
1382 if (!NT_STATUS_IS_OK(status)) {
1383 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1384 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1385 ERRSRV, ERRbadpath);
1386 END_PROFILE(SMBntrename);
1389 reply_nterror(req, status);
1390 END_PROFILE(SMBntrename);
1394 /* The new name must begin with a ':' if the old name is a stream. */
1395 if (is_ntfs_stream_name(oldname) && (newname[0] != ':')) {
1396 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1397 END_PROFILE(SMBntrename);
1401 DEBUG(3,("reply_ntrename : %s -> %s\n",oldname,newname));
1403 switch(rename_type) {
1404 case RENAME_FLAG_RENAME:
1405 status = rename_internals(ctx, conn, req, oldname,
1406 newname, attrs, False, src_has_wcard,
1407 dest_has_wcard, DELETE_ACCESS);
1409 case RENAME_FLAG_HARD_LINK:
1410 if (src_has_wcard || dest_has_wcard) {
1412 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1414 status = hardlink_internals(ctx,
1420 case RENAME_FLAG_COPY:
1421 if (src_has_wcard || dest_has_wcard) {
1423 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1425 status = copy_internals(ctx, conn, req, oldname,
1429 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1430 status = NT_STATUS_INVALID_PARAMETER;
1433 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1437 if (!NT_STATUS_IS_OK(status)) {
1438 if (open_was_deferred(req->mid)) {
1439 /* We have re-scheduled this call. */
1440 END_PROFILE(SMBntrename);
1444 reply_nterror(req, status);
1445 END_PROFILE(SMBntrename);
1449 reply_outbuf(req, 0, 0);
1451 END_PROFILE(SMBntrename);
1455 /****************************************************************************
1456 Reply to a notify change - queue the request and
1457 don't allow a directory to be opened.
1458 ****************************************************************************/
1460 static void call_nt_transact_notify_change(connection_struct *conn,
1461 struct smb_request *req,
1465 uint32 parameter_count,
1466 char **ppdata, uint32 data_count,
1467 uint32 max_data_count,
1468 uint32 max_param_count)
1470 uint16 *setup = *ppsetup;
1476 if(setup_count < 6) {
1477 reply_doserror(req, ERRDOS, ERRbadfunc);
1481 fsp = file_fsp(req, SVAL(setup,4));
1482 filter = IVAL(setup, 0);
1483 recursive = (SVAL(setup, 6) != 0) ? True : False;
1485 DEBUG(3,("call_nt_transact_notify_change\n"));
1488 reply_doserror(req, ERRDOS, ERRbadfid);
1493 char *filter_string;
1495 if (!(filter_string = notify_filter_string(NULL, filter))) {
1496 reply_nterror(req,NT_STATUS_NO_MEMORY);
1500 DEBUG(3,("call_nt_transact_notify_change: notify change "
1501 "called on %s, filter = %s, recursive = %d\n",
1502 fsp->fsp_name, filter_string, recursive));
1504 TALLOC_FREE(filter_string);
1507 if((!fsp->is_directory) || (conn != fsp->conn)) {
1508 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1512 if (fsp->notify == NULL) {
1514 status = change_notify_create(fsp, filter, recursive);
1516 if (!NT_STATUS_IS_OK(status)) {
1517 DEBUG(10, ("change_notify_create returned %s\n",
1518 nt_errstr(status)));
1519 reply_nterror(req, status);
1524 if (fsp->notify->num_changes != 0) {
1527 * We've got changes pending, respond immediately
1531 * TODO: write a torture test to check the filtering behaviour
1535 change_notify_reply(fsp->conn, req, max_param_count,
1539 * change_notify_reply() above has independently sent its
1546 * No changes pending, queue the request
1549 status = change_notify_add_request(req,
1553 if (!NT_STATUS_IS_OK(status)) {
1554 reply_nterror(req, status);
1559 /****************************************************************************
1560 Reply to an NT transact rename command.
1561 ****************************************************************************/
1563 static void call_nt_transact_rename(connection_struct *conn,
1564 struct smb_request *req,
1565 uint16 **ppsetup, uint32 setup_count,
1566 char **ppparams, uint32 parameter_count,
1567 char **ppdata, uint32 data_count,
1568 uint32 max_data_count)
1570 char *params = *ppparams;
1571 char *new_name = NULL;
1572 files_struct *fsp = NULL;
1573 bool dest_has_wcard = False;
1575 TALLOC_CTX *ctx = talloc_tos();
1577 if(parameter_count < 5) {
1578 reply_doserror(req, ERRDOS, ERRbadfunc);
1582 fsp = file_fsp(req, SVAL(params, 0));
1583 if (!check_fsp(conn, req, fsp)) {
1586 srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
1587 parameter_count - 4,
1588 STR_TERMINATE, &status, &dest_has_wcard);
1589 if (!NT_STATUS_IS_OK(status)) {
1590 reply_nterror(req, status);
1595 * W2K3 ignores this request as the RAW-RENAME test
1596 * demonstrates, so we do.
1598 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1600 DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1601 fsp->fsp_name, new_name));
1606 /******************************************************************************
1607 Fake up a completely empty SD.
1608 *******************************************************************************/
1610 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
1614 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1616 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1617 return NT_STATUS_NO_MEMORY;
1620 return NT_STATUS_OK;
1623 /****************************************************************************
1624 Reply to query a security descriptor.
1625 ****************************************************************************/
1627 static void call_nt_transact_query_security_desc(connection_struct *conn,
1628 struct smb_request *req,
1632 uint32 parameter_count,
1635 uint32 max_data_count)
1637 char *params = *ppparams;
1638 char *data = *ppdata;
1639 SEC_DESC *psd = NULL;
1641 uint32 security_info_wanted;
1642 files_struct *fsp = NULL;
1646 if(parameter_count < 8) {
1647 reply_doserror(req, ERRDOS, ERRbadfunc);
1651 fsp = file_fsp(req, SVAL(params,0));
1653 reply_doserror(req, ERRDOS, ERRbadfid);
1657 security_info_wanted = IVAL(params,4);
1659 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, info_wanted = 0x%x\n", fsp->fsp_name,
1660 (unsigned int)security_info_wanted ));
1662 params = nttrans_realloc(ppparams, 4);
1663 if(params == NULL) {
1664 reply_doserror(req, ERRDOS, ERRnomem);
1669 * Get the permissions to return.
1672 if (!lp_nt_acl_support(SNUM(conn))) {
1673 status = get_null_nt_acl(talloc_tos(), &psd);
1675 status = SMB_VFS_FGET_NT_ACL(
1676 fsp, security_info_wanted, &psd);
1678 if (!NT_STATUS_IS_OK(status)) {
1679 reply_nterror(req, status);
1683 /* If the SACL/DACL is NULL, but was requested, we mark that it is
1684 * present in the reply to match Windows behavior */
1685 if (psd->sacl == NULL &&
1686 security_info_wanted & SACL_SECURITY_INFORMATION)
1687 psd->type |= SEC_DESC_SACL_PRESENT;
1688 if (psd->dacl == NULL &&
1689 security_info_wanted & DACL_SECURITY_INFORMATION)
1690 psd->type |= SEC_DESC_DACL_PRESENT;
1692 sd_size = ndr_size_security_descriptor(psd, NULL, 0);
1694 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
1696 if (DEBUGLEVEL >= 10) {
1697 DEBUG(10,("call_nt_transact_query_security_desc for file %s\n", fsp->fsp_name));
1698 NDR_PRINT_DEBUG(security_descriptor, psd);
1701 SIVAL(params,0,(uint32)sd_size);
1703 if (max_data_count < sd_size) {
1704 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
1705 params, 4, *ppdata, 0);
1710 * Allocate the data we will point this at.
1713 data = nttrans_realloc(ppdata, sd_size);
1715 reply_doserror(req, ERRDOS, ERRnomem);
1719 status = marshall_sec_desc(talloc_tos(), psd,
1720 &blob.data, &blob.length);
1722 if (!NT_STATUS_IS_OK(status)) {
1723 reply_nterror(req, status);
1727 SMB_ASSERT(sd_size == blob.length);
1728 memcpy(data, blob.data, sd_size);
1730 send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
1735 /****************************************************************************
1736 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
1737 ****************************************************************************/
1739 static void call_nt_transact_set_security_desc(connection_struct *conn,
1740 struct smb_request *req,
1744 uint32 parameter_count,
1747 uint32 max_data_count)
1749 char *params= *ppparams;
1750 char *data = *ppdata;
1751 files_struct *fsp = NULL;
1752 uint32 security_info_sent = 0;
1755 if(parameter_count < 8) {
1756 reply_doserror(req, ERRDOS, ERRbadfunc);
1760 if((fsp = file_fsp(req, SVAL(params,0))) == NULL) {
1761 reply_doserror(req, ERRDOS, ERRbadfid);
1765 if(!lp_nt_acl_support(SNUM(conn))) {
1769 security_info_sent = IVAL(params,4);
1771 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
1772 (unsigned int)security_info_sent ));
1774 if (data_count == 0) {
1775 reply_doserror(req, ERRDOS, ERRnoaccess);
1779 status = set_sd(fsp, (uint8 *)data, data_count, security_info_sent);
1781 if (!NT_STATUS_IS_OK(status)) {
1782 reply_nterror(req, status);
1787 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1791 /****************************************************************************
1793 ****************************************************************************/
1795 static void call_nt_transact_ioctl(connection_struct *conn,
1796 struct smb_request *req,
1797 uint16 **ppsetup, uint32 setup_count,
1798 char **ppparams, uint32 parameter_count,
1799 char **ppdata, uint32 data_count,
1800 uint32 max_data_count)
1807 char *pdata = *ppdata;
1809 if (setup_count != 8) {
1810 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
1811 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1815 function = IVAL(*ppsetup, 0);
1816 fidnum = SVAL(*ppsetup, 4);
1817 isFSctl = CVAL(*ppsetup, 6);
1818 compfilter = CVAL(*ppsetup, 7);
1820 DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
1821 function, fidnum, isFSctl, compfilter));
1823 fsp=file_fsp(req, fidnum);
1824 /* this check is done in each implemented function case for now
1825 because I don't want to break anything... --metze
1826 FSP_BELONGS_CONN(fsp,conn);*/
1828 SMB_PERFCOUNT_SET_IOCTL(&req->pcd, function);
1831 case FSCTL_SET_SPARSE:
1832 /* pretend this succeeded - tho strictly we should
1833 mark the file sparse (if the local fs supports it)
1834 so we can know if we need to pre-allocate or not */
1836 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
1837 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1840 case FSCTL_CREATE_OR_GET_OBJECT_ID:
1842 unsigned char objid[16];
1844 /* This should return the object-id on this file.
1845 * I think I'll make this be the inode+dev. JRA.
1848 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
1850 if (!fsp_belongs_conn(conn, req, fsp)) {
1855 pdata = nttrans_realloc(ppdata, data_count);
1856 if (pdata == NULL) {
1857 reply_nterror(req, NT_STATUS_NO_MEMORY);
1861 /* For backwards compatibility only store the dev/inode. */
1862 push_file_id_16(pdata, &fsp->file_id);
1863 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
1864 push_file_id_16(pdata+32, &fsp->file_id);
1865 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
1870 case FSCTL_GET_REPARSE_POINT:
1871 /* pretend this fail - my winXP does it like this
1875 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1876 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
1879 case FSCTL_SET_REPARSE_POINT:
1880 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
1884 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1885 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
1888 case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
1891 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
1892 * and return their volume names. If max_data_count is 16, then it is just
1893 * asking for the number of volumes and length of the combined names.
1895 * pdata is the data allocated by our caller, but that uses
1896 * total_data_count (which is 0 in our case) rather than max_data_count.
1897 * Allocate the correct amount and return the pointer to let
1898 * it be deallocated when we return.
1900 SHADOW_COPY_DATA *shadow_data = NULL;
1901 TALLOC_CTX *shadow_mem_ctx = NULL;
1902 bool labels = False;
1903 uint32 labels_data_count = 0;
1907 if (!fsp_belongs_conn(conn, req, fsp)) {
1911 if (max_data_count < 16) {
1912 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
1914 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1918 if (max_data_count > 16) {
1922 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
1923 if (shadow_mem_ctx == NULL) {
1924 DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
1925 reply_nterror(req, NT_STATUS_NO_MEMORY);
1929 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
1930 if (shadow_data == NULL) {
1931 DEBUG(0,("TALLOC_ZERO() failed!\n"));
1932 talloc_destroy(shadow_mem_ctx);
1933 reply_nterror(req, NT_STATUS_NO_MEMORY);
1937 shadow_data->mem_ctx = shadow_mem_ctx;
1940 * Call the VFS routine to actually do the work.
1942 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
1943 talloc_destroy(shadow_data->mem_ctx);
1944 if (errno == ENOSYS) {
1945 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
1946 conn->connectpath));
1947 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1950 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
1951 conn->connectpath));
1952 reply_nterror(req, NT_STATUS_UNSUCCESSFUL);
1957 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
1962 data_count = 12+labels_data_count+4;
1965 if (max_data_count<data_count) {
1966 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
1967 max_data_count,data_count));
1968 talloc_destroy(shadow_data->mem_ctx);
1969 reply_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
1973 pdata = nttrans_realloc(ppdata, data_count);
1974 if (pdata == NULL) {
1975 talloc_destroy(shadow_data->mem_ctx);
1976 reply_nterror(req, NT_STATUS_NO_MEMORY);
1982 /* num_volumes 4 bytes */
1983 SIVAL(pdata,0,shadow_data->num_volumes);
1986 /* num_labels 4 bytes */
1987 SIVAL(pdata,4,shadow_data->num_volumes);
1990 /* needed_data_count 4 bytes */
1991 SIVAL(pdata,8,labels_data_count);
1995 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
1996 shadow_data->num_volumes,fsp->fsp_name));
1997 if (labels && shadow_data->labels) {
1998 for (i=0;i<shadow_data->num_volumes;i++) {
1999 srvstr_push(pdata, req->flags2,
2000 cur_pdata, shadow_data->labels[i],
2001 2*sizeof(SHADOW_COPY_LABEL),
2002 STR_UNICODE|STR_TERMINATE);
2003 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
2004 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
2008 talloc_destroy(shadow_data->mem_ctx);
2010 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
2016 case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
2018 /* pretend this succeeded -
2020 * we have to send back a list with all files owned by this SID
2022 * but I have to check that --metze
2026 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
2028 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
2030 if (!fsp_belongs_conn(conn, req, fsp)) {
2034 /* unknown 4 bytes: this is not the length of the sid :-( */
2035 /*unknown = IVAL(pdata,0);*/
2037 sid_parse(pdata+4,sid_len,&sid);
2038 DEBUGADD(10, ("for SID: %s\n", sid_string_dbg(&sid)));
2040 if (!sid_to_uid(&sid, &uid)) {
2041 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2042 sid_string_dbg(&sid),
2043 (unsigned long)sid_len));
2047 /* we can take a look at the find source :-)
2049 * find ./ -uid $uid -name '*' is what we need here
2052 * and send 4bytes len and then NULL terminated unicode strings
2055 * but I don't know how to deal with the paged results
2056 * (maybe we can hang the result anywhere in the fsp struct)
2058 * we don't send all files at once
2059 * and at the next we should *not* start from the beginning,
2060 * so we have to cache the result
2065 /* this works for now... */
2066 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2070 if (!logged_ioctl_message) {
2071 logged_ioctl_message = true; /* Only print this once... */
2072 DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
2077 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2081 #ifdef HAVE_SYS_QUOTAS
2082 /****************************************************************************
2083 Reply to get user quota
2084 ****************************************************************************/
2086 static void call_nt_transact_get_user_quota(connection_struct *conn,
2087 struct smb_request *req,
2091 uint32 parameter_count,
2094 uint32 max_data_count)
2096 NTSTATUS nt_status = NT_STATUS_OK;
2097 char *params = *ppparams;
2098 char *pdata = *ppdata;
2100 int data_len=0,param_len=0;
2103 files_struct *fsp = NULL;
2107 bool start_enum = True;
2108 SMB_NTQUOTA_STRUCT qt;
2109 SMB_NTQUOTA_LIST *tmp_list;
2110 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2115 if (conn->server_info->utok.uid != 0) {
2116 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2117 "[%s]\n", lp_servicename(SNUM(conn)),
2118 conn->server_info->unix_name));
2119 reply_doserror(req, ERRDOS, ERRnoaccess);
2124 * Ensure minimum number of parameters sent.
2127 if (parameter_count < 4) {
2128 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2129 reply_doserror(req, ERRDOS, ERRinvalidparam);
2133 /* maybe we can check the quota_fnum */
2134 fsp = file_fsp(req, SVAL(params,0));
2135 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2136 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2137 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2141 /* the NULL pointer checking for fsp->fake_file_handle->pd
2142 * is done by CHECK_NTQUOTA_HANDLE_OK()
2144 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->private_data;
2146 level = SVAL(params,2);
2148 /* unknown 12 bytes leading in params */
2151 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2152 /* seems that we should continue with the enum here --metze */
2154 if (qt_handle->quota_list!=NULL &&
2155 qt_handle->tmp_list==NULL) {
2158 free_ntquota_list(&(qt_handle->quota_list));
2160 /* Realloc the size of parameters and data we will return */
2162 params = nttrans_realloc(ppparams, param_len);
2163 if(params == NULL) {
2164 reply_doserror(req, ERRDOS, ERRnomem);
2169 SIVAL(params,0,data_len);
2176 case TRANSACT_GET_USER_QUOTA_LIST_START:
2178 if (qt_handle->quota_list==NULL &&
2179 qt_handle->tmp_list==NULL) {
2183 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2184 reply_doserror(req, ERRSRV, ERRerror);
2188 /* Realloc the size of parameters and data we will return */
2190 params = nttrans_realloc(ppparams, param_len);
2191 if(params == NULL) {
2192 reply_doserror(req, ERRDOS, ERRnomem);
2196 /* we should not trust the value in max_data_count*/
2197 max_data_count = MIN(max_data_count,2048);
2199 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2201 reply_doserror(req, ERRDOS, ERRnomem);
2207 /* set params Size of returned Quota Data 4 bytes*/
2208 /* but set it later when we know it */
2210 /* for each entry push the data */
2213 qt_handle->tmp_list = qt_handle->quota_list;
2216 tmp_list = qt_handle->tmp_list;
2218 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2219 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2221 sid_len = ndr_size_dom_sid(
2222 &tmp_list->quotas->sid, NULL, 0);
2223 entry_len = 40 + sid_len;
2225 /* nextoffset entry 4 bytes */
2226 SIVAL(entry,0,entry_len);
2228 /* then the len of the SID 4 bytes */
2229 SIVAL(entry,4,sid_len);
2231 /* unknown data 8 bytes uint64_t */
2232 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-metze*/
2234 /* the used disk space 8 bytes uint64_t */
2235 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2237 /* the soft quotas 8 bytes uint64_t */
2238 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2240 /* the hard quotas 8 bytes uint64_t */
2241 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2243 /* and now the SID */
2244 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2247 qt_handle->tmp_list = tmp_list;
2249 /* overwrite the offset of the last entry */
2250 SIVAL(entry-entry_len,0,0);
2252 data_len = 4+qt_len;
2253 /* overwrite the params quota_data_len */
2254 SIVAL(params,0,data_len);
2258 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2260 /* unknown 4 bytes IVAL(pdata,0) */
2262 if (data_count < 8) {
2263 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2264 reply_doserror(req, ERRDOS, ERRunknownlevel);
2268 sid_len = IVAL(pdata,4);
2269 /* Ensure this is less than 1mb. */
2270 if (sid_len > (1024*1024)) {
2271 reply_doserror(req, ERRDOS, ERRnomem);
2275 if (data_count < 8+sid_len) {
2276 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2277 reply_doserror(req, ERRDOS, ERRunknownlevel);
2281 data_len = 4+40+sid_len;
2283 if (max_data_count < data_len) {
2284 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2285 max_data_count, data_len));
2287 SIVAL(params,0,data_len);
2289 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2293 sid_parse(pdata+8,sid_len,&sid);
2295 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2298 * we have to return zero's in all fields
2299 * instead of returning an error here
2304 /* Realloc the size of parameters and data we will return */
2306 params = nttrans_realloc(ppparams, param_len);
2307 if(params == NULL) {
2308 reply_doserror(req, ERRDOS, ERRnomem);
2312 pdata = nttrans_realloc(ppdata, data_len);
2314 reply_doserror(req, ERRDOS, ERRnomem);
2320 /* set params Size of returned Quota Data 4 bytes*/
2321 SIVAL(params,0,data_len);
2323 /* nextoffset entry 4 bytes */
2326 /* then the len of the SID 4 bytes */
2327 SIVAL(entry,4,sid_len);
2329 /* unknown data 8 bytes uint64_t */
2330 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-mezte*/
2332 /* the used disk space 8 bytes uint64_t */
2333 SBIG_UINT(entry,16,qt.usedspace);
2335 /* the soft quotas 8 bytes uint64_t */
2336 SBIG_UINT(entry,24,qt.softlim);
2338 /* the hard quotas 8 bytes uint64_t */
2339 SBIG_UINT(entry,32,qt.hardlim);
2341 /* and now the SID */
2342 sid_linearize(entry+40, sid_len, &sid);
2347 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2348 reply_doserror(req, ERRSRV, ERRerror);
2353 send_nt_replies(conn, req, nt_status, params, param_len,
2357 /****************************************************************************
2358 Reply to set user quota
2359 ****************************************************************************/
2361 static void call_nt_transact_set_user_quota(connection_struct *conn,
2362 struct smb_request *req,
2366 uint32 parameter_count,
2369 uint32 max_data_count)
2371 char *params = *ppparams;
2372 char *pdata = *ppdata;
2373 int data_len=0,param_len=0;
2374 SMB_NTQUOTA_STRUCT qt;
2377 files_struct *fsp = NULL;
2382 if (conn->server_info->utok.uid != 0) {
2383 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2384 "[%s]\n", lp_servicename(SNUM(conn)),
2385 conn->server_info->unix_name));
2386 reply_doserror(req, ERRDOS, ERRnoaccess);
2391 * Ensure minimum number of parameters sent.
2394 if (parameter_count < 2) {
2395 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2396 reply_doserror(req, ERRDOS, ERRinvalidparam);
2400 /* maybe we can check the quota_fnum */
2401 fsp = file_fsp(req, SVAL(params,0));
2402 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2403 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2404 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2408 if (data_count < 40) {
2409 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2410 reply_doserror(req, ERRDOS, ERRunknownlevel);
2414 /* offset to next quota record.
2415 * 4 bytes IVAL(pdata,0)
2420 sid_len = IVAL(pdata,4);
2422 if (data_count < 40+sid_len) {
2423 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2424 reply_doserror(req, ERRDOS, ERRunknownlevel);
2428 /* unknown 8 bytes in pdata
2429 * maybe its the change time in NTTIME
2432 /* the used space 8 bytes (uint64_t)*/
2433 qt.usedspace = (uint64_t)IVAL(pdata,16);
2434 #ifdef LARGE_SMB_OFF_T
2435 qt.usedspace |= (((uint64_t)IVAL(pdata,20)) << 32);
2436 #else /* LARGE_SMB_OFF_T */
2437 if ((IVAL(pdata,20) != 0)&&
2438 ((qt.usedspace != 0xFFFFFFFF)||
2439 (IVAL(pdata,20)!=0xFFFFFFFF))) {
2440 /* more than 32 bits? */
2441 reply_doserror(req, ERRDOS, ERRunknownlevel);
2444 #endif /* LARGE_SMB_OFF_T */
2446 /* the soft quotas 8 bytes (uint64_t)*/
2447 qt.softlim = (uint64_t)IVAL(pdata,24);
2448 #ifdef LARGE_SMB_OFF_T
2449 qt.softlim |= (((uint64_t)IVAL(pdata,28)) << 32);
2450 #else /* LARGE_SMB_OFF_T */
2451 if ((IVAL(pdata,28) != 0)&&
2452 ((qt.softlim != 0xFFFFFFFF)||
2453 (IVAL(pdata,28)!=0xFFFFFFFF))) {
2454 /* more than 32 bits? */
2455 reply_doserror(req, ERRDOS, ERRunknownlevel);
2458 #endif /* LARGE_SMB_OFF_T */
2460 /* the hard quotas 8 bytes (uint64_t)*/
2461 qt.hardlim = (uint64_t)IVAL(pdata,32);
2462 #ifdef LARGE_SMB_OFF_T
2463 qt.hardlim |= (((uint64_t)IVAL(pdata,36)) << 32);
2464 #else /* LARGE_SMB_OFF_T */
2465 if ((IVAL(pdata,36) != 0)&&
2466 ((qt.hardlim != 0xFFFFFFFF)||
2467 (IVAL(pdata,36)!=0xFFFFFFFF))) {
2468 /* more than 32 bits? */
2469 reply_doserror(req, ERRDOS, ERRunknownlevel);
2472 #endif /* LARGE_SMB_OFF_T */
2474 sid_parse(pdata+40,sid_len,&sid);
2475 DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid)));
2477 /* 44 unknown bytes left... */
2479 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2480 reply_doserror(req, ERRSRV, ERRerror);
2484 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2487 #endif /* HAVE_SYS_QUOTAS */
2489 static void handle_nttrans(connection_struct *conn,
2490 struct trans_state *state,
2491 struct smb_request *req)
2493 if (Protocol >= PROTOCOL_NT1) {
2494 req->flags2 |= 0x40; /* IS_LONG_NAME */
2495 SSVAL(req->inbuf,smb_flg2,req->flags2);
2499 SMB_PERFCOUNT_SET_SUBOP(&req->pcd, state->call);
2501 /* Now we must call the relevant NT_TRANS function */
2502 switch(state->call) {
2503 case NT_TRANSACT_CREATE:
2505 START_PROFILE(NT_transact_create);
2506 call_nt_transact_create(
2508 &state->setup, state->setup_count,
2509 &state->param, state->total_param,
2510 &state->data, state->total_data,
2511 state->max_data_return);
2512 END_PROFILE(NT_transact_create);
2516 case NT_TRANSACT_IOCTL:
2518 START_PROFILE(NT_transact_ioctl);
2519 call_nt_transact_ioctl(
2521 &state->setup, state->setup_count,
2522 &state->param, state->total_param,
2523 &state->data, state->total_data,
2524 state->max_data_return);
2525 END_PROFILE(NT_transact_ioctl);
2529 case NT_TRANSACT_SET_SECURITY_DESC:
2531 START_PROFILE(NT_transact_set_security_desc);
2532 call_nt_transact_set_security_desc(
2534 &state->setup, state->setup_count,
2535 &state->param, state->total_param,
2536 &state->data, state->total_data,
2537 state->max_data_return);
2538 END_PROFILE(NT_transact_set_security_desc);
2542 case NT_TRANSACT_NOTIFY_CHANGE:
2544 START_PROFILE(NT_transact_notify_change);
2545 call_nt_transact_notify_change(
2547 &state->setup, state->setup_count,
2548 &state->param, state->total_param,
2549 &state->data, state->total_data,
2550 state->max_data_return,
2551 state->max_param_return);
2552 END_PROFILE(NT_transact_notify_change);
2556 case NT_TRANSACT_RENAME:
2558 START_PROFILE(NT_transact_rename);
2559 call_nt_transact_rename(
2561 &state->setup, state->setup_count,
2562 &state->param, state->total_param,
2563 &state->data, state->total_data,
2564 state->max_data_return);
2565 END_PROFILE(NT_transact_rename);
2569 case NT_TRANSACT_QUERY_SECURITY_DESC:
2571 START_PROFILE(NT_transact_query_security_desc);
2572 call_nt_transact_query_security_desc(
2574 &state->setup, state->setup_count,
2575 &state->param, state->total_param,
2576 &state->data, state->total_data,
2577 state->max_data_return);
2578 END_PROFILE(NT_transact_query_security_desc);
2582 #ifdef HAVE_SYS_QUOTAS
2583 case NT_TRANSACT_GET_USER_QUOTA:
2585 START_PROFILE(NT_transact_get_user_quota);
2586 call_nt_transact_get_user_quota(
2588 &state->setup, state->setup_count,
2589 &state->param, state->total_param,
2590 &state->data, state->total_data,
2591 state->max_data_return);
2592 END_PROFILE(NT_transact_get_user_quota);
2596 case NT_TRANSACT_SET_USER_QUOTA:
2598 START_PROFILE(NT_transact_set_user_quota);
2599 call_nt_transact_set_user_quota(
2601 &state->setup, state->setup_count,
2602 &state->param, state->total_param,
2603 &state->data, state->total_data,
2604 state->max_data_return);
2605 END_PROFILE(NT_transact_set_user_quota);
2608 #endif /* HAVE_SYS_QUOTAS */
2611 /* Error in request */
2612 DEBUG(0,("handle_nttrans: Unknown request %d in "
2613 "nttrans call\n", state->call));
2614 reply_doserror(req, ERRSRV, ERRerror);
2620 /****************************************************************************
2621 Reply to a SMBNTtrans.
2622 ****************************************************************************/
2624 void reply_nttrans(struct smb_request *req)
2626 connection_struct *conn = req->conn;
2631 uint16 function_code;
2633 struct trans_state *state;
2635 START_PROFILE(SMBnttrans);
2637 if (req->wct < 19) {
2638 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2639 END_PROFILE(SMBnttrans);
2643 pscnt = IVAL(req->vwv+9, 1);
2644 psoff = IVAL(req->vwv+11, 1);
2645 dscnt = IVAL(req->vwv+13, 1);
2646 dsoff = IVAL(req->vwv+15, 1);
2647 function_code = SVAL(req->vwv+18, 0);
2649 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2650 reply_doserror(req, ERRSRV, ERRaccess);
2651 END_PROFILE(SMBnttrans);
2655 result = allow_new_trans(conn->pending_trans, req->mid);
2656 if (!NT_STATUS_IS_OK(result)) {
2657 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
2658 reply_nterror(req, result);
2659 END_PROFILE(SMBnttrans);
2663 if ((state = TALLOC_P(conn, struct trans_state)) == NULL) {
2664 reply_doserror(req, ERRSRV, ERRaccess);
2665 END_PROFILE(SMBnttrans);
2669 state->cmd = SMBnttrans;
2671 state->mid = req->mid;
2672 state->vuid = req->vuid;
2673 state->total_data = IVAL(req->vwv+3, 1);
2675 state->total_param = IVAL(req->vwv+1, 1);
2676 state->param = NULL;
2677 state->max_data_return = IVAL(req->vwv+7, 1);
2678 state->max_param_return = IVAL(req->vwv+5, 1);
2680 /* setup count is in *words* */
2681 state->setup_count = 2*CVAL(req->vwv+17, 1);
2682 state->setup = NULL;
2683 state->call = function_code;
2685 DEBUG(10, ("num_setup=%u, "
2686 "param_total=%u, this_param=%u, max_param=%u, "
2687 "data_total=%u, this_data=%u, max_data=%u, "
2688 "param_offset=%u, data_offset=%u\n",
2689 (unsigned)state->setup_count,
2690 (unsigned)state->total_param, (unsigned)pscnt,
2691 (unsigned)state->max_param_return,
2692 (unsigned)state->total_data, (unsigned)dscnt,
2693 (unsigned)state->max_data_return,
2694 (unsigned)psoff, (unsigned)dsoff));
2697 * All nttrans messages we handle have smb_wct == 19 +
2698 * state->setup_count. Ensure this is so as a sanity check.
2701 if(req->wct != 19 + (state->setup_count/2)) {
2702 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2703 req->wct, 19 + (state->setup_count/2)));
2707 /* Don't allow more than 128mb for each value. */
2708 if ((state->total_data > (1024*1024*128)) ||
2709 (state->total_param > (1024*1024*128))) {
2710 reply_doserror(req, ERRDOS, ERRnomem);
2711 END_PROFILE(SMBnttrans);
2715 if ((dscnt > state->total_data) || (pscnt > state->total_param))
2718 if (state->total_data) {
2720 if (trans_oob(state->total_data, 0, dscnt)
2721 || trans_oob(smb_len(req->inbuf), dsoff, dscnt)) {
2725 /* Can't use talloc here, the core routines do realloc on the
2726 * params and data. */
2727 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
2728 DEBUG(0,("reply_nttrans: data malloc fail for %u "
2729 "bytes !\n", (unsigned int)state->total_data));
2731 reply_doserror(req, ERRDOS, ERRnomem);
2732 END_PROFILE(SMBnttrans);
2736 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
2739 if (state->total_param) {
2741 if (trans_oob(state->total_param, 0, pscnt)
2742 || trans_oob(smb_len(req->inbuf), psoff, pscnt)) {
2746 /* Can't use talloc here, the core routines do realloc on the
2747 * params and data. */
2748 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
2749 DEBUG(0,("reply_nttrans: param malloc fail for %u "
2750 "bytes !\n", (unsigned int)state->total_param));
2751 SAFE_FREE(state->data);
2753 reply_doserror(req, ERRDOS, ERRnomem);
2754 END_PROFILE(SMBnttrans);
2758 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
2761 state->received_data = dscnt;
2762 state->received_param = pscnt;
2764 if(state->setup_count > 0) {
2765 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2766 state->setup_count));
2769 * No overflow possible here, state->setup_count is an
2770 * unsigned int, being filled by a single byte from
2771 * CVAL(req->vwv+13, 0) above. The cast in the comparison
2772 * below is not necessary, it's here to clarify things. The
2773 * validity of req->vwv and req->wct has been checked in
2774 * init_smb_request already.
2776 if ((state->setup_count/2) + 19 > (unsigned int)req->wct) {
2780 state->setup = (uint16 *)TALLOC(state, state->setup_count);
2781 if (state->setup == NULL) {
2782 DEBUG(0,("reply_nttrans : Out of memory\n"));
2783 SAFE_FREE(state->data);
2784 SAFE_FREE(state->param);
2786 reply_doserror(req, ERRDOS, ERRnomem);
2787 END_PROFILE(SMBnttrans);
2791 memcpy(state->setup, req->vwv+19, state->setup_count);
2792 dump_data(10, (uint8 *)state->setup, state->setup_count);
2795 if ((state->received_data == state->total_data) &&
2796 (state->received_param == state->total_param)) {
2797 handle_nttrans(conn, state, req);
2798 SAFE_FREE(state->param);
2799 SAFE_FREE(state->data);
2801 END_PROFILE(SMBnttrans);
2805 DLIST_ADD(conn->pending_trans, state);
2807 /* We need to send an interim response then receive the rest
2808 of the parameter/data bytes */
2809 reply_outbuf(req, 0, 0);
2810 show_msg((char *)req->outbuf);
2811 END_PROFILE(SMBnttrans);
2816 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
2817 SAFE_FREE(state->data);
2818 SAFE_FREE(state->param);
2820 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2821 END_PROFILE(SMBnttrans);
2825 /****************************************************************************
2826 Reply to a SMBnttranss
2827 ****************************************************************************/
2829 void reply_nttranss(struct smb_request *req)
2831 connection_struct *conn = req->conn;
2832 uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
2833 struct trans_state *state;
2835 START_PROFILE(SMBnttranss);
2837 show_msg((char *)req->inbuf);
2839 if (req->wct < 18) {
2840 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2841 END_PROFILE(SMBnttranss);
2845 for (state = conn->pending_trans; state != NULL;
2846 state = state->next) {
2847 if (state->mid == req->mid) {
2852 if ((state == NULL) || (state->cmd != SMBnttrans)) {
2853 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2854 END_PROFILE(SMBnttranss);
2858 /* Revise state->total_param and state->total_data in case they have
2859 changed downwards */
2860 if (IVAL(req->vwv+1, 1) < state->total_param) {
2861 state->total_param = IVAL(req->vwv+1, 1);
2863 if (IVAL(req->vwv+3, 1) < state->total_data) {
2864 state->total_data = IVAL(req->vwv+3, 1);
2867 pcnt = IVAL(req->vwv+5, 1);
2868 poff = IVAL(req->vwv+7, 1);
2869 pdisp = IVAL(req->vwv+9, 1);
2871 dcnt = IVAL(req->vwv+11, 1);
2872 doff = IVAL(req->vwv+13, 1);
2873 ddisp = IVAL(req->vwv+15, 1);
2875 state->received_param += pcnt;
2876 state->received_data += dcnt;
2878 if ((state->received_data > state->total_data) ||
2879 (state->received_param > state->total_param))
2883 if (trans_oob(state->total_param, pdisp, pcnt)
2884 || trans_oob(smb_len(req->inbuf), poff, pcnt)) {
2887 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,pcnt);
2891 if (trans_oob(state->total_data, ddisp, dcnt)
2892 || trans_oob(smb_len(req->inbuf), doff, dcnt)) {
2895 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
2898 if ((state->received_param < state->total_param) ||
2899 (state->received_data < state->total_data)) {
2900 END_PROFILE(SMBnttranss);
2904 handle_nttrans(conn, state, req);
2906 DLIST_REMOVE(conn->pending_trans, state);
2907 SAFE_FREE(state->data);
2908 SAFE_FREE(state->param);
2910 END_PROFILE(SMBnttranss);
2915 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
2916 DLIST_REMOVE(conn->pending_trans, state);
2917 SAFE_FREE(state->data);
2918 SAFE_FREE(state->param);
2920 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2921 END_PROFILE(SMBnttranss);