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 = 1;
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 struct case_semantics_state *case_state = NULL;
441 TALLOC_CTX *ctx = talloc_tos();
443 START_PROFILE(SMBntcreateX);
445 SET_STAT_INVALID(sbuf);
448 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
452 flags = IVAL(req->vwv+3, 1);
453 access_mask = IVAL(req->vwv+7, 1);
454 file_attributes = IVAL(req->vwv+13, 1);
455 share_access = IVAL(req->vwv+15, 1);
456 create_disposition = IVAL(req->vwv+17, 1);
457 create_options = IVAL(req->vwv+19, 1);
458 root_dir_fid = (uint16)IVAL(req->vwv+5, 1);
460 allocation_size = (uint64_t)IVAL(req->vwv+9, 1);
461 #ifdef LARGE_SMB_OFF_T
462 allocation_size |= (((uint64_t)IVAL(req->vwv+11, 1)) << 32);
465 srvstr_get_path_req(ctx, req, &fname, (const char *)req->buf,
466 STR_TERMINATE, &status);
468 if (!NT_STATUS_IS_OK(status)) {
469 reply_nterror(req, status);
470 END_PROFILE(SMBntcreateX);
474 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
475 "file_attributes = 0x%x, share_access = 0x%x, "
476 "create_disposition = 0x%x create_options = 0x%x "
477 "root_dir_fid = 0x%x, fname = %s\n",
479 (unsigned int)access_mask,
480 (unsigned int)file_attributes,
481 (unsigned int)share_access,
482 (unsigned int)create_disposition,
483 (unsigned int)create_options,
484 (unsigned int)root_dir_fid,
488 * we need to remove ignored bits when they come directly from the client
489 * because we reuse some of them for internal stuff
491 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
494 * If it's an IPC, use the pipe handler.
498 if (lp_nt_pipe_support()) {
499 do_ntcreate_pipe_open(conn, req);
500 END_PROFILE(SMBntcreateX);
503 reply_doserror(req, ERRDOS, ERRnoaccess);
504 END_PROFILE(SMBntcreateX);
508 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
509 if (oplock_request) {
510 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
515 * Check if POSIX semantics are wanted.
518 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
519 case_state = set_posix_case_semantics(ctx, conn);
521 reply_nterror(req, NT_STATUS_NO_MEMORY);
522 END_PROFILE(SMBntcreateX);
526 * Bug #6898 - clients using Windows opens should
527 * never be able to set this attribute into the
530 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
533 status = SMB_VFS_CREATE_FILE(
536 root_dir_fid, /* root_dir_fid */
538 CFF_DOS_PATH, /* create_file_flags */
539 access_mask, /* access_mask */
540 share_access, /* share_access */
541 create_disposition, /* create_disposition*/
542 create_options, /* create_options */
543 file_attributes, /* file_attributes */
544 oplock_request, /* oplock_request */
545 allocation_size, /* allocation_size */
552 TALLOC_FREE(case_state);
554 if (!NT_STATUS_IS_OK(status)) {
555 if (open_was_deferred(req->mid)) {
556 /* We have re-scheduled this call, no error. */
557 END_PROFILE(SMBntcreateX);
560 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
561 reply_botherror(req, status, ERRDOS, ERRfilexists);
564 reply_nterror(req, status);
566 END_PROFILE(SMBntcreateX);
571 * If the caller set the extended oplock request bit
572 * and we granted one (by whatever means) - set the
573 * correct bit for extended oplock reply.
576 if (oplock_request &&
577 (lp_fake_oplocks(SNUM(conn))
578 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
581 * Exclusive oplock granted
584 if (flags & REQUEST_BATCH_OPLOCK) {
585 oplock_granted = BATCH_OPLOCK_RETURN;
587 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
589 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
590 oplock_granted = LEVEL_II_OPLOCK_RETURN;
592 oplock_granted = NO_OPLOCK_RETURN;
595 file_len = sbuf.st_ex_size;
596 fattr = dos_mode(conn,fsp->fsp_name,&sbuf);
598 fattr = FILE_ATTRIBUTE_NORMAL;
601 if (flags & EXTENDED_RESPONSE_REQUIRED) {
602 /* This is very strange. We
603 * return 50 words, but only set
604 * the wcnt to 42 ? It's definately
605 * what happens on the wire....
607 reply_outbuf(req, 50, 0);
608 SCVAL(req->outbuf,smb_wct,42);
610 reply_outbuf(req, 34, 0);
613 p = (char *)req->outbuf + smb_vwv2;
615 SCVAL(p, 0, oplock_granted);
618 SSVAL(p,0,fsp->fnum);
620 if ((create_disposition == FILE_SUPERSEDE)
621 && (info == FILE_WAS_OVERWRITTEN)) {
622 SIVAL(p,0,FILE_WAS_SUPERSEDED);
629 c_timespec = sbuf.st_ex_btime;
630 a_timespec = sbuf.st_ex_atime;
631 m_timespec = sbuf.st_ex_mtime;
633 if (lp_dos_filetime_resolution(SNUM(conn))) {
634 dos_filetime_timespec(&c_timespec);
635 dos_filetime_timespec(&a_timespec);
636 dos_filetime_timespec(&m_timespec);
639 put_long_date_timespec(conn->ts_res, p, c_timespec); /* create time. */
641 put_long_date_timespec(conn->ts_res, p, a_timespec); /* access time */
643 put_long_date_timespec(conn->ts_res, p, m_timespec); /* write time */
645 put_long_date_timespec(conn->ts_res, p, m_timespec); /* change time */
647 SIVAL(p,0,fattr); /* File Attributes. */
649 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn,fsp,&sbuf));
651 SOFF_T(p,0,file_len);
653 if (flags & EXTENDED_RESPONSE_REQUIRED) {
657 SCVAL(p,0,fsp->is_directory ? 1 : 0);
659 if (flags & EXTENDED_RESPONSE_REQUIRED) {
662 if (fsp->is_directory
663 || can_write_to_file(conn, fsp->fsp_name, &sbuf)) {
664 perms = FILE_GENERIC_ALL;
666 perms = FILE_GENERIC_READ|FILE_EXECUTE;
671 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n",
672 fsp->fnum, fsp->fsp_name));
675 END_PROFILE(SMBntcreateX);
679 /****************************************************************************
680 Reply to a NT_TRANSACT_CREATE call to open a pipe.
681 ****************************************************************************/
683 static void do_nt_transact_create_pipe(connection_struct *conn,
684 struct smb_request *req,
685 uint16 **ppsetup, uint32 setup_count,
686 char **ppparams, uint32 parameter_count,
687 char **ppdata, uint32 data_count)
690 char *params = *ppparams;
696 TALLOC_CTX *ctx = talloc_tos();
699 * Ensure minimum number of parameters sent.
702 if(parameter_count < 54) {
703 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
704 reply_doserror(req, ERRDOS, ERRnoaccess);
708 flags = IVAL(params,0);
710 srvstr_get_path(ctx, params, req->flags2, &fname, params+53,
711 parameter_count-53, STR_TERMINATE,
713 if (!NT_STATUS_IS_OK(status)) {
714 reply_nterror(req, status);
718 nt_open_pipe(fname, conn, req, &pnum);
725 /* Realloc the size of parameters and data we will return */
726 if (flags & EXTENDED_RESPONSE_REQUIRED) {
727 /* Extended response is 32 more byyes. */
732 params = nttrans_realloc(ppparams, param_len);
734 reply_doserror(req, ERRDOS, ERRnomem);
739 SCVAL(p,0,NO_OPLOCK_RETURN);
744 SIVAL(p,0,FILE_WAS_OPENED);
748 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
751 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
753 SSVAL(p,2, 0x5FF); /* ? */
756 if (flags & EXTENDED_RESPONSE_REQUIRED) {
758 SIVAL(p,0,FILE_GENERIC_ALL);
760 * For pipes W2K3 seems to return
762 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
764 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
767 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
769 /* Send the required number of replies */
770 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
775 /****************************************************************************
776 Internal fn to set security descriptors.
777 ****************************************************************************/
779 static NTSTATUS set_sd(files_struct *fsp, uint8 *data, uint32 sd_len,
780 uint32 security_info_sent)
782 SEC_DESC *psd = NULL;
785 if (sd_len == 0 || !lp_nt_acl_support(SNUM(fsp->conn))) {
789 status = unmarshall_sec_desc(talloc_tos(), data, sd_len, &psd);
791 if (!NT_STATUS_IS_OK(status)) {
795 if (psd->owner_sid == NULL) {
796 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
798 if (psd->group_sid == NULL) {
799 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
802 /* Convert all the generic bits. */
803 security_acl_map_generic(psd->dacl, &file_generic_mapping);
804 security_acl_map_generic(psd->sacl, &file_generic_mapping);
806 if (DEBUGLEVEL >= 10) {
807 DEBUG(10,("set_sd for file %s\n", fsp->fsp_name ));
808 NDR_PRINT_DEBUG(security_descriptor, psd);
811 status = SMB_VFS_FSET_NT_ACL(fsp, security_info_sent, psd);
818 /****************************************************************************
819 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
820 ****************************************************************************/
822 static struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
824 struct ea_list *ea_list_head = NULL;
831 while (offset + 4 <= data_size) {
832 size_t next_offset = IVAL(pdata,offset);
833 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
839 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
840 if (next_offset == 0) {
843 offset += next_offset;
849 /****************************************************************************
850 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
851 ****************************************************************************/
853 static void call_nt_transact_create(connection_struct *conn,
854 struct smb_request *req,
855 uint16 **ppsetup, uint32 setup_count,
856 char **ppparams, uint32 parameter_count,
857 char **ppdata, uint32 data_count,
858 uint32 max_data_count)
861 char *params = *ppparams;
862 char *data = *ppdata;
863 /* Breakout the oplock request bits so we can set the reply bits separately. */
865 SMB_OFF_T file_len = 0;
866 SMB_STRUCT_STAT sbuf;
868 files_struct *fsp = NULL;
872 uint32 file_attributes;
874 uint32 create_disposition;
875 uint32 create_options;
877 struct security_descriptor *sd = NULL;
880 struct timespec c_timespec;
881 struct timespec a_timespec;
882 struct timespec m_timespec;
883 struct ea_list *ea_list = NULL;
886 uint64_t allocation_size;
888 uint8_t oplock_granted;
889 struct case_semantics_state *case_state = NULL;
890 TALLOC_CTX *ctx = talloc_tos();
892 SET_STAT_INVALID(sbuf);
894 DEBUG(5,("call_nt_transact_create\n"));
897 * If it's an IPC, use the pipe handler.
901 if (lp_nt_pipe_support()) {
902 do_nt_transact_create_pipe(
904 ppsetup, setup_count,
905 ppparams, parameter_count,
909 reply_doserror(req, ERRDOS, ERRnoaccess);
914 * Ensure minimum number of parameters sent.
917 if(parameter_count < 54) {
918 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
919 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
923 flags = IVAL(params,0);
924 access_mask = IVAL(params,8);
925 file_attributes = IVAL(params,20);
926 share_access = IVAL(params,24);
927 create_disposition = IVAL(params,28);
928 create_options = IVAL(params,32);
929 sd_len = IVAL(params,36);
930 ea_len = IVAL(params,40);
931 root_dir_fid = (uint16)IVAL(params,4);
932 allocation_size = (uint64_t)IVAL(params,12);
933 #ifdef LARGE_SMB_OFF_T
934 allocation_size |= (((uint64_t)IVAL(params,16)) << 32);
938 * we need to remove ignored bits when they come directly from the client
939 * because we reuse some of them for internal stuff
941 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
943 /* Ensure the data_len is correct for the sd and ea values given. */
944 if ((ea_len + sd_len > data_count)
945 || (ea_len > data_count) || (sd_len > data_count)
946 || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
947 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
948 "%u, data_count = %u\n", (unsigned int)ea_len,
949 (unsigned int)sd_len, (unsigned int)data_count));
950 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
955 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
958 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
960 if (!NT_STATUS_IS_OK(status)) {
961 DEBUG(10, ("call_nt_transact_create: "
962 "unmarshall_sec_desc failed: %s\n",
964 reply_nterror(req, status);
970 if (!lp_ea_support(SNUM(conn))) {
971 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
972 "EA's not supported.\n",
973 (unsigned int)ea_len));
974 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
979 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
980 "too small (should be more than 10)\n",
981 (unsigned int)ea_len ));
982 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
986 /* We have already checked that ea_len <= data_count here. */
987 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
989 if (ea_list == NULL) {
990 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
995 srvstr_get_path(ctx, params, req->flags2, &fname,
996 params+53, parameter_count-53,
997 STR_TERMINATE, &status);
998 if (!NT_STATUS_IS_OK(status)) {
999 reply_nterror(req, status);
1003 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1004 if (oplock_request) {
1005 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
1010 * Check if POSIX semantics are wanted.
1013 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1014 case_state = set_posix_case_semantics(ctx, conn);
1016 reply_nterror(req, NT_STATUS_NO_MEMORY);
1020 * Bug #6898 - clients using Windows opens should
1021 * never be able to set this attribute into the
1024 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
1027 status = SMB_VFS_CREATE_FILE(
1030 root_dir_fid, /* root_dir_fid */
1032 CFF_DOS_PATH, /* create_file_flags */
1033 access_mask, /* access_mask */
1034 share_access, /* share_access */
1035 create_disposition, /* create_disposition*/
1036 create_options, /* create_options */
1037 file_attributes, /* file_attributes */
1038 oplock_request, /* oplock_request */
1039 allocation_size, /* allocation_size */
1041 ea_list, /* ea_list */
1046 TALLOC_FREE(case_state);
1048 if(!NT_STATUS_IS_OK(status)) {
1049 if (open_was_deferred(req->mid)) {
1050 /* We have re-scheduled this call, no error. */
1053 reply_openerror(req, status);
1058 * If the caller set the extended oplock request bit
1059 * and we granted one (by whatever means) - set the
1060 * correct bit for extended oplock reply.
1063 if (oplock_request &&
1064 (lp_fake_oplocks(SNUM(conn))
1065 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
1068 * Exclusive oplock granted
1071 if (flags & REQUEST_BATCH_OPLOCK) {
1072 oplock_granted = BATCH_OPLOCK_RETURN;
1074 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
1076 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1077 oplock_granted = LEVEL_II_OPLOCK_RETURN;
1079 oplock_granted = NO_OPLOCK_RETURN;
1082 file_len = sbuf.st_ex_size;
1083 fattr = dos_mode(conn,fsp->fsp_name,&sbuf);
1085 fattr = FILE_ATTRIBUTE_NORMAL;
1088 /* Realloc the size of parameters and data we will return */
1089 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1090 /* Extended response is 32 more byyes. */
1095 params = nttrans_realloc(ppparams, param_len);
1096 if(params == NULL) {
1097 reply_doserror(req, ERRDOS, ERRnomem);
1102 SCVAL(p, 0, oplock_granted);
1105 SSVAL(p,0,fsp->fnum);
1107 if ((create_disposition == FILE_SUPERSEDE)
1108 && (info == FILE_WAS_OVERWRITTEN)) {
1109 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1116 c_timespec = sbuf.st_ex_btime;
1117 a_timespec = sbuf.st_ex_atime;
1118 m_timespec = sbuf.st_ex_mtime;
1120 if (lp_dos_filetime_resolution(SNUM(conn))) {
1121 dos_filetime_timespec(&c_timespec);
1122 dos_filetime_timespec(&a_timespec);
1123 dos_filetime_timespec(&m_timespec);
1126 put_long_date_timespec(conn->ts_res, p, c_timespec); /* create time. */
1128 put_long_date_timespec(conn->ts_res, p, a_timespec); /* access time */
1130 put_long_date_timespec(conn->ts_res, p, m_timespec); /* write time */
1132 put_long_date_timespec(conn->ts_res, p, m_timespec); /* change time */
1134 SIVAL(p,0,fattr); /* File Attributes. */
1136 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn,fsp,&sbuf));
1138 SOFF_T(p,0,file_len);
1140 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1144 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1146 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1149 if (fsp->is_directory
1150 || can_write_to_file(conn, fsp->fsp_name, &sbuf)) {
1151 perms = FILE_GENERIC_ALL;
1153 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1158 DEBUG(5,("call_nt_transact_create: open name = %s\n", fsp->fsp_name));
1160 /* Send the required number of replies */
1161 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1166 /****************************************************************************
1167 Reply to a NT CANCEL request.
1168 conn POINTER CAN BE NULL HERE !
1169 ****************************************************************************/
1171 void reply_ntcancel(struct smb_request *req)
1174 * Go through and cancel any pending change notifies.
1177 START_PROFILE(SMBntcancel);
1178 srv_cancel_sign_response(smbd_server_conn);
1179 remove_pending_change_notify_requests_by_mid(req->mid);
1180 remove_pending_lock_requests_by_mid(req->mid);
1182 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", req->mid));
1184 END_PROFILE(SMBntcancel);
1188 /****************************************************************************
1190 ****************************************************************************/
1192 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
1193 connection_struct *conn,
1194 struct smb_request *req,
1195 const char *oldname_in,
1196 const char *newname_in,
1199 SMB_STRUCT_STAT sbuf1, sbuf2;
1200 char *oldname = NULL;
1201 char *newname = NULL;
1202 char *last_component_oldname = NULL;
1203 char *last_component_newname = NULL;
1204 files_struct *fsp1,*fsp2;
1208 NTSTATUS status = NT_STATUS_OK;
1214 if (!CAN_WRITE(conn)) {
1215 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1218 status = unix_convert(ctx, conn, oldname_in, False, &oldname,
1219 &last_component_oldname, &sbuf1);
1220 if (!NT_STATUS_IS_OK(status)) {
1224 status = check_name(conn, oldname);
1225 if (!NT_STATUS_IS_OK(status)) {
1229 /* Source must already exist. */
1230 if (!VALID_STAT(sbuf1)) {
1231 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1233 /* Ensure attributes match. */
1234 fattr = dos_mode(conn,oldname,&sbuf1);
1235 if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
1236 return NT_STATUS_NO_SUCH_FILE;
1239 status = unix_convert(ctx, conn, newname_in, False, &newname,
1240 &last_component_newname, &sbuf2);
1241 if (!NT_STATUS_IS_OK(status)) {
1245 status = check_name(conn, newname);
1246 if (!NT_STATUS_IS_OK(status)) {
1250 /* Disallow if newname already exists. */
1251 if (VALID_STAT(sbuf2)) {
1252 return NT_STATUS_OBJECT_NAME_COLLISION;
1255 /* No links from a directory. */
1256 if (S_ISDIR(sbuf1.st_ex_mode)) {
1257 return NT_STATUS_FILE_IS_A_DIRECTORY;
1260 /* Ensure this is within the share. */
1261 status = check_reduced_name(conn, oldname);
1262 if (!NT_STATUS_IS_OK(status)) {
1266 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1269 status = SMB_VFS_CREATE_FILE(
1272 0, /* root_dir_fid */
1273 oldname, /* fname */
1274 0, /* create_file_flags */
1275 FILE_READ_DATA, /* access_mask */
1276 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1278 FILE_OPEN, /* create_disposition*/
1279 0, /* create_options */
1280 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
1281 NO_OPLOCK, /* oplock_request */
1282 0, /* allocation_size */
1287 &sbuf1); /* psbuf */
1289 if (!NT_STATUS_IS_OK(status)) {
1293 status = SMB_VFS_CREATE_FILE(
1296 0, /* root_dir_fid */
1297 newname, /* fname */
1298 0, /* create_file_flags */
1299 FILE_WRITE_DATA, /* access_mask */
1300 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1302 FILE_CREATE, /* create_disposition*/
1303 0, /* create_options */
1304 fattr, /* file_attributes */
1305 NO_OPLOCK, /* oplock_request */
1306 0, /* allocation_size */
1311 &sbuf2); /* psbuf */
1313 if (!NT_STATUS_IS_OK(status)) {
1314 close_file(NULL, fsp1, ERROR_CLOSE);
1318 if (sbuf1.st_ex_size) {
1319 ret = vfs_transfer_file(fsp1, fsp2, sbuf1.st_ex_size);
1323 * As we are opening fsp1 read-only we only expect
1324 * an error on close on fsp2 if we are out of space.
1325 * Thus we don't look at the error return from the
1328 close_file(NULL, fsp1, NORMAL_CLOSE);
1330 /* Ensure the modtime is set correctly on the destination file. */
1331 set_close_write_time(fsp2, sbuf1.st_ex_mtime);
1333 status = close_file(NULL, fsp2, NORMAL_CLOSE);
1335 /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
1336 creates the file. This isn't the correct thing to do in the copy
1338 if (!parent_dirname(talloc_tos(), newname, &parent, NULL)) {
1339 return NT_STATUS_NO_MEMORY;
1341 file_set_dosmode(conn, newname, fattr, &sbuf2, parent, false);
1342 TALLOC_FREE(parent);
1344 if (ret < (SMB_OFF_T)sbuf1.st_ex_size) {
1345 return NT_STATUS_DISK_FULL;
1348 if (!NT_STATUS_IS_OK(status)) {
1349 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1350 nt_errstr(status), oldname, newname));
1355 /****************************************************************************
1356 Reply to a NT rename request.
1357 ****************************************************************************/
1359 void reply_ntrename(struct smb_request *req)
1361 connection_struct *conn = req->conn;
1362 char *oldname = NULL;
1363 char *newname = NULL;
1366 bool src_has_wcard = False;
1367 bool dest_has_wcard = False;
1370 TALLOC_CTX *ctx = talloc_tos();
1372 START_PROFILE(SMBntrename);
1375 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1376 END_PROFILE(SMBntrename);
1380 attrs = SVAL(req->vwv+0, 0);
1381 rename_type = SVAL(req->vwv+1, 0);
1383 p = (const char *)req->buf + 1;
1384 p += srvstr_get_path_req_wcard(ctx, req, &oldname, p, STR_TERMINATE,
1385 &status, &src_has_wcard);
1386 if (!NT_STATUS_IS_OK(status)) {
1387 reply_nterror(req, status);
1388 END_PROFILE(SMBntrename);
1392 if (ms_has_wild(oldname)) {
1393 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1394 END_PROFILE(SMBntrename);
1399 p += srvstr_get_path_req_wcard(ctx, req, &newname, p, STR_TERMINATE,
1400 &status, &dest_has_wcard);
1401 if (!NT_STATUS_IS_OK(status)) {
1402 reply_nterror(req, status);
1403 END_PROFILE(SMBntrename);
1407 status = resolve_dfspath(ctx, conn,
1408 req->flags2 & FLAGS2_DFS_PATHNAMES,
1411 if (!NT_STATUS_IS_OK(status)) {
1412 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1413 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1414 ERRSRV, ERRbadpath);
1415 END_PROFILE(SMBntrename);
1418 reply_nterror(req, status);
1419 END_PROFILE(SMBntrename);
1423 status = resolve_dfspath(ctx, conn,
1424 req->flags2 & FLAGS2_DFS_PATHNAMES,
1427 if (!NT_STATUS_IS_OK(status)) {
1428 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1429 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1430 ERRSRV, ERRbadpath);
1431 END_PROFILE(SMBntrename);
1434 reply_nterror(req, status);
1435 END_PROFILE(SMBntrename);
1439 /* The new name must begin with a ':' if the old name is a stream. */
1440 if (is_ntfs_stream_name(oldname) && (newname[0] != ':')) {
1441 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1442 END_PROFILE(SMBntrename);
1446 DEBUG(3,("reply_ntrename : %s -> %s\n",oldname,newname));
1448 switch(rename_type) {
1449 case RENAME_FLAG_RENAME:
1450 status = rename_internals(ctx, conn, req, oldname,
1451 newname, attrs, False, src_has_wcard,
1452 dest_has_wcard, DELETE_ACCESS);
1454 case RENAME_FLAG_HARD_LINK:
1455 if (src_has_wcard || dest_has_wcard) {
1457 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1459 status = hardlink_internals(ctx,
1465 case RENAME_FLAG_COPY:
1466 if (src_has_wcard || dest_has_wcard) {
1468 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1470 status = copy_internals(ctx, conn, req, oldname,
1474 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1475 status = NT_STATUS_INVALID_PARAMETER;
1478 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1482 if (!NT_STATUS_IS_OK(status)) {
1483 if (open_was_deferred(req->mid)) {
1484 /* We have re-scheduled this call. */
1485 END_PROFILE(SMBntrename);
1489 reply_nterror(req, status);
1490 END_PROFILE(SMBntrename);
1494 reply_outbuf(req, 0, 0);
1496 END_PROFILE(SMBntrename);
1500 /****************************************************************************
1501 Reply to a notify change - queue the request and
1502 don't allow a directory to be opened.
1503 ****************************************************************************/
1505 static void call_nt_transact_notify_change(connection_struct *conn,
1506 struct smb_request *req,
1510 uint32 parameter_count,
1511 char **ppdata, uint32 data_count,
1512 uint32 max_data_count,
1513 uint32 max_param_count)
1515 uint16 *setup = *ppsetup;
1521 if(setup_count < 6) {
1522 reply_doserror(req, ERRDOS, ERRbadfunc);
1526 fsp = file_fsp(req, SVAL(setup,4));
1527 filter = IVAL(setup, 0);
1528 recursive = (SVAL(setup, 6) != 0) ? True : False;
1530 DEBUG(3,("call_nt_transact_notify_change\n"));
1533 reply_doserror(req, ERRDOS, ERRbadfid);
1538 char *filter_string;
1540 if (!(filter_string = notify_filter_string(NULL, filter))) {
1541 reply_nterror(req,NT_STATUS_NO_MEMORY);
1545 DEBUG(3,("call_nt_transact_notify_change: notify change "
1546 "called on %s, filter = %s, recursive = %d\n",
1547 fsp->fsp_name, filter_string, recursive));
1549 TALLOC_FREE(filter_string);
1552 if((!fsp->is_directory) || (conn != fsp->conn)) {
1553 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1557 if (fsp->notify == NULL) {
1559 status = change_notify_create(fsp, filter, recursive);
1561 if (!NT_STATUS_IS_OK(status)) {
1562 DEBUG(10, ("change_notify_create returned %s\n",
1563 nt_errstr(status)));
1564 reply_nterror(req, status);
1569 if (fsp->notify->num_changes != 0) {
1572 * We've got changes pending, respond immediately
1576 * TODO: write a torture test to check the filtering behaviour
1580 change_notify_reply(fsp->conn, req, max_param_count,
1584 * change_notify_reply() above has independently sent its
1591 * No changes pending, queue the request
1594 status = change_notify_add_request(req,
1598 if (!NT_STATUS_IS_OK(status)) {
1599 reply_nterror(req, status);
1604 /****************************************************************************
1605 Reply to an NT transact rename command.
1606 ****************************************************************************/
1608 static void call_nt_transact_rename(connection_struct *conn,
1609 struct smb_request *req,
1610 uint16 **ppsetup, uint32 setup_count,
1611 char **ppparams, uint32 parameter_count,
1612 char **ppdata, uint32 data_count,
1613 uint32 max_data_count)
1615 char *params = *ppparams;
1616 char *new_name = NULL;
1617 files_struct *fsp = NULL;
1618 bool dest_has_wcard = False;
1620 TALLOC_CTX *ctx = talloc_tos();
1622 if(parameter_count < 5) {
1623 reply_doserror(req, ERRDOS, ERRbadfunc);
1627 fsp = file_fsp(req, SVAL(params, 0));
1628 if (!check_fsp(conn, req, fsp)) {
1631 srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
1632 parameter_count - 4,
1633 STR_TERMINATE, &status, &dest_has_wcard);
1634 if (!NT_STATUS_IS_OK(status)) {
1635 reply_nterror(req, status);
1640 * W2K3 ignores this request as the RAW-RENAME test
1641 * demonstrates, so we do.
1643 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1645 DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1646 fsp->fsp_name, new_name));
1651 /******************************************************************************
1652 Fake up a completely empty SD.
1653 *******************************************************************************/
1655 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
1659 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1661 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1662 return NT_STATUS_NO_MEMORY;
1665 return NT_STATUS_OK;
1668 /****************************************************************************
1669 Reply to query a security descriptor.
1670 ****************************************************************************/
1672 static void call_nt_transact_query_security_desc(connection_struct *conn,
1673 struct smb_request *req,
1677 uint32 parameter_count,
1680 uint32 max_data_count)
1682 char *params = *ppparams;
1683 char *data = *ppdata;
1684 SEC_DESC *psd = NULL;
1686 uint32 security_info_wanted;
1687 files_struct *fsp = NULL;
1691 if(parameter_count < 8) {
1692 reply_doserror(req, ERRDOS, ERRbadfunc);
1696 fsp = file_fsp(req, SVAL(params,0));
1698 reply_doserror(req, ERRDOS, ERRbadfid);
1702 security_info_wanted = IVAL(params,4);
1704 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, info_wanted = 0x%x\n", fsp->fsp_name,
1705 (unsigned int)security_info_wanted ));
1707 params = nttrans_realloc(ppparams, 4);
1708 if(params == NULL) {
1709 reply_doserror(req, ERRDOS, ERRnomem);
1714 * Get the permissions to return.
1717 if (!lp_nt_acl_support(SNUM(conn))) {
1718 status = get_null_nt_acl(talloc_tos(), &psd);
1720 status = SMB_VFS_FGET_NT_ACL(
1721 fsp, security_info_wanted, &psd);
1723 if (!NT_STATUS_IS_OK(status)) {
1724 reply_nterror(req, status);
1728 /* If the SACL/DACL is NULL, but was requested, we mark that it is
1729 * present in the reply to match Windows behavior */
1730 if (psd->sacl == NULL &&
1731 security_info_wanted & SACL_SECURITY_INFORMATION)
1732 psd->type |= SEC_DESC_SACL_PRESENT;
1733 if (psd->dacl == NULL &&
1734 security_info_wanted & DACL_SECURITY_INFORMATION)
1735 psd->type |= SEC_DESC_DACL_PRESENT;
1737 sd_size = ndr_size_security_descriptor(psd, NULL, 0);
1739 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
1741 if (DEBUGLEVEL >= 10) {
1742 DEBUG(10,("call_nt_transact_query_security_desc for file %s\n", fsp->fsp_name));
1743 NDR_PRINT_DEBUG(security_descriptor, psd);
1746 SIVAL(params,0,(uint32)sd_size);
1748 if (max_data_count < sd_size) {
1749 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
1750 params, 4, *ppdata, 0);
1755 * Allocate the data we will point this at.
1758 data = nttrans_realloc(ppdata, sd_size);
1760 reply_doserror(req, ERRDOS, ERRnomem);
1764 status = marshall_sec_desc(talloc_tos(), psd,
1765 &blob.data, &blob.length);
1767 if (!NT_STATUS_IS_OK(status)) {
1768 reply_nterror(req, status);
1772 SMB_ASSERT(sd_size == blob.length);
1773 memcpy(data, blob.data, sd_size);
1775 send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
1780 /****************************************************************************
1781 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
1782 ****************************************************************************/
1784 static void call_nt_transact_set_security_desc(connection_struct *conn,
1785 struct smb_request *req,
1789 uint32 parameter_count,
1792 uint32 max_data_count)
1794 char *params= *ppparams;
1795 char *data = *ppdata;
1796 files_struct *fsp = NULL;
1797 uint32 security_info_sent = 0;
1800 if(parameter_count < 8) {
1801 reply_doserror(req, ERRDOS, ERRbadfunc);
1805 if((fsp = file_fsp(req, SVAL(params,0))) == NULL) {
1806 reply_doserror(req, ERRDOS, ERRbadfid);
1810 if(!lp_nt_acl_support(SNUM(conn))) {
1814 security_info_sent = IVAL(params,4);
1816 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
1817 (unsigned int)security_info_sent ));
1819 if (data_count == 0) {
1820 reply_doserror(req, ERRDOS, ERRnoaccess);
1824 status = set_sd(fsp, (uint8 *)data, data_count, security_info_sent);
1826 if (!NT_STATUS_IS_OK(status)) {
1827 reply_nterror(req, status);
1832 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1836 /****************************************************************************
1838 ****************************************************************************/
1840 static void call_nt_transact_ioctl(connection_struct *conn,
1841 struct smb_request *req,
1842 uint16 **ppsetup, uint32 setup_count,
1843 char **ppparams, uint32 parameter_count,
1844 char **ppdata, uint32 data_count,
1845 uint32 max_data_count)
1852 char *pdata = *ppdata;
1854 if (setup_count != 8) {
1855 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
1856 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1860 function = IVAL(*ppsetup, 0);
1861 fidnum = SVAL(*ppsetup, 4);
1862 isFSctl = CVAL(*ppsetup, 6);
1863 compfilter = CVAL(*ppsetup, 7);
1865 DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
1866 function, fidnum, isFSctl, compfilter));
1868 fsp=file_fsp(req, fidnum);
1869 /* this check is done in each implemented function case for now
1870 because I don't want to break anything... --metze
1871 FSP_BELONGS_CONN(fsp,conn);*/
1873 SMB_PERFCOUNT_SET_IOCTL(&req->pcd, function);
1876 case FSCTL_SET_SPARSE:
1877 /* pretend this succeeded - tho strictly we should
1878 mark the file sparse (if the local fs supports it)
1879 so we can know if we need to pre-allocate or not */
1881 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
1882 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1885 case FSCTL_CREATE_OR_GET_OBJECT_ID:
1887 unsigned char objid[16];
1889 /* This should return the object-id on this file.
1890 * I think I'll make this be the inode+dev. JRA.
1893 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
1895 if (!fsp_belongs_conn(conn, req, fsp)) {
1900 pdata = nttrans_realloc(ppdata, data_count);
1901 if (pdata == NULL) {
1902 reply_nterror(req, NT_STATUS_NO_MEMORY);
1906 /* For backwards compatibility only store the dev/inode. */
1907 push_file_id_16(pdata, &fsp->file_id);
1908 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
1909 push_file_id_16(pdata+32, &fsp->file_id);
1910 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
1915 case FSCTL_GET_REPARSE_POINT:
1916 /* pretend this fail - my winXP does it like this
1920 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1921 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
1924 case FSCTL_SET_REPARSE_POINT:
1925 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
1929 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1930 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
1933 case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
1936 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
1937 * and return their volume names. If max_data_count is 16, then it is just
1938 * asking for the number of volumes and length of the combined names.
1940 * pdata is the data allocated by our caller, but that uses
1941 * total_data_count (which is 0 in our case) rather than max_data_count.
1942 * Allocate the correct amount and return the pointer to let
1943 * it be deallocated when we return.
1945 SHADOW_COPY_DATA *shadow_data = NULL;
1946 TALLOC_CTX *shadow_mem_ctx = NULL;
1947 bool labels = False;
1948 uint32 labels_data_count = 0;
1952 if (!fsp_belongs_conn(conn, req, fsp)) {
1956 if (max_data_count < 16) {
1957 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
1959 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1963 if (max_data_count > 16) {
1967 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
1968 if (shadow_mem_ctx == NULL) {
1969 DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
1970 reply_nterror(req, NT_STATUS_NO_MEMORY);
1974 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
1975 if (shadow_data == NULL) {
1976 DEBUG(0,("TALLOC_ZERO() failed!\n"));
1977 talloc_destroy(shadow_mem_ctx);
1978 reply_nterror(req, NT_STATUS_NO_MEMORY);
1982 shadow_data->mem_ctx = shadow_mem_ctx;
1985 * Call the VFS routine to actually do the work.
1987 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
1988 talloc_destroy(shadow_data->mem_ctx);
1989 if (errno == ENOSYS) {
1990 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
1991 conn->connectpath));
1992 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1995 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
1996 conn->connectpath));
1997 reply_nterror(req, NT_STATUS_UNSUCCESSFUL);
2002 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
2007 data_count = 12+labels_data_count+4;
2010 if (max_data_count<data_count) {
2011 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
2012 max_data_count,data_count));
2013 talloc_destroy(shadow_data->mem_ctx);
2014 reply_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
2018 pdata = nttrans_realloc(ppdata, data_count);
2019 if (pdata == NULL) {
2020 talloc_destroy(shadow_data->mem_ctx);
2021 reply_nterror(req, NT_STATUS_NO_MEMORY);
2027 /* num_volumes 4 bytes */
2028 SIVAL(pdata,0,shadow_data->num_volumes);
2031 /* num_labels 4 bytes */
2032 SIVAL(pdata,4,shadow_data->num_volumes);
2035 /* needed_data_count 4 bytes */
2036 SIVAL(pdata, 8, labels_data_count+4);
2040 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
2041 shadow_data->num_volumes,fsp->fsp_name));
2042 if (labels && shadow_data->labels) {
2043 for (i=0;i<shadow_data->num_volumes;i++) {
2044 srvstr_push(pdata, req->flags2,
2045 cur_pdata, shadow_data->labels[i],
2046 2*sizeof(SHADOW_COPY_LABEL),
2047 STR_UNICODE|STR_TERMINATE);
2048 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
2049 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
2053 talloc_destroy(shadow_data->mem_ctx);
2055 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
2061 case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
2063 /* pretend this succeeded -
2065 * we have to send back a list with all files owned by this SID
2067 * but I have to check that --metze
2071 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
2073 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
2075 if (!fsp_belongs_conn(conn, req, fsp)) {
2079 /* unknown 4 bytes: this is not the length of the sid :-( */
2080 /*unknown = IVAL(pdata,0);*/
2082 if (!sid_parse(pdata+4,sid_len,&sid)) {
2083 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2087 DEBUGADD(10, ("for SID: %s\n", sid_string_dbg(&sid)));
2089 if (!sid_to_uid(&sid, &uid)) {
2090 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2091 sid_string_dbg(&sid),
2092 (unsigned long)sid_len));
2096 /* we can take a look at the find source :-)
2098 * find ./ -uid $uid -name '*' is what we need here
2101 * and send 4bytes len and then NULL terminated unicode strings
2104 * but I don't know how to deal with the paged results
2105 * (maybe we can hang the result anywhere in the fsp struct)
2107 * we don't send all files at once
2108 * and at the next we should *not* start from the beginning,
2109 * so we have to cache the result
2114 /* this works for now... */
2115 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2118 case FSCTL_IS_VOLUME_DIRTY:
2119 DEBUG(10,("FSCTL_IS_VOLUME_DIRTY: called on FID[0x%04X] "
2120 "(but not implemented)\n", (int)fidnum));
2122 * http://msdn.microsoft.com/en-us/library/cc232128%28PROT.10%29.aspx
2123 * says we have to respond with NT_STATUS_INVALID_PARAMETER
2125 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2128 if (!logged_ioctl_message) {
2129 logged_ioctl_message = true; /* Only print this once... */
2130 DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
2135 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2139 #ifdef HAVE_SYS_QUOTAS
2140 /****************************************************************************
2141 Reply to get user quota
2142 ****************************************************************************/
2144 static void call_nt_transact_get_user_quota(connection_struct *conn,
2145 struct smb_request *req,
2149 uint32 parameter_count,
2152 uint32 max_data_count)
2154 NTSTATUS nt_status = NT_STATUS_OK;
2155 char *params = *ppparams;
2156 char *pdata = *ppdata;
2158 int data_len=0,param_len=0;
2161 files_struct *fsp = NULL;
2165 bool start_enum = True;
2166 SMB_NTQUOTA_STRUCT qt;
2167 SMB_NTQUOTA_LIST *tmp_list;
2168 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2173 if (conn->server_info->utok.uid != 0) {
2174 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2175 "[%s]\n", lp_servicename(SNUM(conn)),
2176 conn->server_info->unix_name));
2177 reply_doserror(req, ERRDOS, ERRnoaccess);
2182 * Ensure minimum number of parameters sent.
2185 if (parameter_count < 4) {
2186 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2187 reply_doserror(req, ERRDOS, ERRinvalidparam);
2191 /* maybe we can check the quota_fnum */
2192 fsp = file_fsp(req, SVAL(params,0));
2193 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2194 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2195 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2199 /* the NULL pointer checking for fsp->fake_file_handle->pd
2200 * is done by CHECK_NTQUOTA_HANDLE_OK()
2202 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->private_data;
2204 level = SVAL(params,2);
2206 /* unknown 12 bytes leading in params */
2209 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2210 /* seems that we should continue with the enum here --metze */
2212 if (qt_handle->quota_list!=NULL &&
2213 qt_handle->tmp_list==NULL) {
2216 free_ntquota_list(&(qt_handle->quota_list));
2218 /* Realloc the size of parameters and data we will return */
2220 params = nttrans_realloc(ppparams, param_len);
2221 if(params == NULL) {
2222 reply_doserror(req, ERRDOS, ERRnomem);
2227 SIVAL(params,0,data_len);
2234 case TRANSACT_GET_USER_QUOTA_LIST_START:
2236 if (qt_handle->quota_list==NULL &&
2237 qt_handle->tmp_list==NULL) {
2241 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2242 reply_doserror(req, ERRSRV, ERRerror);
2246 /* Realloc the size of parameters and data we will return */
2248 params = nttrans_realloc(ppparams, param_len);
2249 if(params == NULL) {
2250 reply_doserror(req, ERRDOS, ERRnomem);
2254 /* we should not trust the value in max_data_count*/
2255 max_data_count = MIN(max_data_count,2048);
2257 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2259 reply_doserror(req, ERRDOS, ERRnomem);
2265 /* set params Size of returned Quota Data 4 bytes*/
2266 /* but set it later when we know it */
2268 /* for each entry push the data */
2271 qt_handle->tmp_list = qt_handle->quota_list;
2274 tmp_list = qt_handle->tmp_list;
2276 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2277 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2279 sid_len = ndr_size_dom_sid(
2280 &tmp_list->quotas->sid, NULL, 0);
2281 entry_len = 40 + sid_len;
2283 /* nextoffset entry 4 bytes */
2284 SIVAL(entry,0,entry_len);
2286 /* then the len of the SID 4 bytes */
2287 SIVAL(entry,4,sid_len);
2289 /* unknown data 8 bytes uint64_t */
2290 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-metze*/
2292 /* the used disk space 8 bytes uint64_t */
2293 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2295 /* the soft quotas 8 bytes uint64_t */
2296 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2298 /* the hard quotas 8 bytes uint64_t */
2299 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2301 /* and now the SID */
2302 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2305 qt_handle->tmp_list = tmp_list;
2307 /* overwrite the offset of the last entry */
2308 SIVAL(entry-entry_len,0,0);
2310 data_len = 4+qt_len;
2311 /* overwrite the params quota_data_len */
2312 SIVAL(params,0,data_len);
2316 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2318 /* unknown 4 bytes IVAL(pdata,0) */
2320 if (data_count < 8) {
2321 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2322 reply_doserror(req, ERRDOS, ERRunknownlevel);
2326 sid_len = IVAL(pdata,4);
2327 /* Ensure this is less than 1mb. */
2328 if (sid_len > (1024*1024)) {
2329 reply_doserror(req, ERRDOS, ERRnomem);
2333 if (data_count < 8+sid_len) {
2334 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2335 reply_doserror(req, ERRDOS, ERRunknownlevel);
2339 data_len = 4+40+sid_len;
2341 if (max_data_count < data_len) {
2342 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2343 max_data_count, data_len));
2345 SIVAL(params,0,data_len);
2347 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2351 if (!sid_parse(pdata+8,sid_len,&sid)) {
2352 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2356 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2359 * we have to return zero's in all fields
2360 * instead of returning an error here
2365 /* Realloc the size of parameters and data we will return */
2367 params = nttrans_realloc(ppparams, param_len);
2368 if(params == NULL) {
2369 reply_doserror(req, ERRDOS, ERRnomem);
2373 pdata = nttrans_realloc(ppdata, data_len);
2375 reply_doserror(req, ERRDOS, ERRnomem);
2381 /* set params Size of returned Quota Data 4 bytes*/
2382 SIVAL(params,0,data_len);
2384 /* nextoffset entry 4 bytes */
2387 /* then the len of the SID 4 bytes */
2388 SIVAL(entry,4,sid_len);
2390 /* unknown data 8 bytes uint64_t */
2391 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-mezte*/
2393 /* the used disk space 8 bytes uint64_t */
2394 SBIG_UINT(entry,16,qt.usedspace);
2396 /* the soft quotas 8 bytes uint64_t */
2397 SBIG_UINT(entry,24,qt.softlim);
2399 /* the hard quotas 8 bytes uint64_t */
2400 SBIG_UINT(entry,32,qt.hardlim);
2402 /* and now the SID */
2403 sid_linearize(entry+40, sid_len, &sid);
2408 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2409 reply_doserror(req, ERRSRV, ERRerror);
2414 send_nt_replies(conn, req, nt_status, params, param_len,
2418 /****************************************************************************
2419 Reply to set user quota
2420 ****************************************************************************/
2422 static void call_nt_transact_set_user_quota(connection_struct *conn,
2423 struct smb_request *req,
2427 uint32 parameter_count,
2430 uint32 max_data_count)
2432 char *params = *ppparams;
2433 char *pdata = *ppdata;
2434 int data_len=0,param_len=0;
2435 SMB_NTQUOTA_STRUCT qt;
2438 files_struct *fsp = NULL;
2443 if (conn->server_info->utok.uid != 0) {
2444 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2445 "[%s]\n", lp_servicename(SNUM(conn)),
2446 conn->server_info->unix_name));
2447 reply_doserror(req, ERRDOS, ERRnoaccess);
2452 * Ensure minimum number of parameters sent.
2455 if (parameter_count < 2) {
2456 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2457 reply_doserror(req, ERRDOS, ERRinvalidparam);
2461 /* maybe we can check the quota_fnum */
2462 fsp = file_fsp(req, SVAL(params,0));
2463 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2464 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2465 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2469 if (data_count < 40) {
2470 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2471 reply_doserror(req, ERRDOS, ERRunknownlevel);
2475 /* offset to next quota record.
2476 * 4 bytes IVAL(pdata,0)
2481 sid_len = IVAL(pdata,4);
2483 if (data_count < 40+sid_len) {
2484 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2485 reply_doserror(req, ERRDOS, ERRunknownlevel);
2489 /* unknown 8 bytes in pdata
2490 * maybe its the change time in NTTIME
2493 /* the used space 8 bytes (uint64_t)*/
2494 qt.usedspace = (uint64_t)IVAL(pdata,16);
2495 #ifdef LARGE_SMB_OFF_T
2496 qt.usedspace |= (((uint64_t)IVAL(pdata,20)) << 32);
2497 #else /* LARGE_SMB_OFF_T */
2498 if ((IVAL(pdata,20) != 0)&&
2499 ((qt.usedspace != 0xFFFFFFFF)||
2500 (IVAL(pdata,20)!=0xFFFFFFFF))) {
2501 /* more than 32 bits? */
2502 reply_doserror(req, ERRDOS, ERRunknownlevel);
2505 #endif /* LARGE_SMB_OFF_T */
2507 /* the soft quotas 8 bytes (uint64_t)*/
2508 qt.softlim = (uint64_t)IVAL(pdata,24);
2509 #ifdef LARGE_SMB_OFF_T
2510 qt.softlim |= (((uint64_t)IVAL(pdata,28)) << 32);
2511 #else /* LARGE_SMB_OFF_T */
2512 if ((IVAL(pdata,28) != 0)&&
2513 ((qt.softlim != 0xFFFFFFFF)||
2514 (IVAL(pdata,28)!=0xFFFFFFFF))) {
2515 /* more than 32 bits? */
2516 reply_doserror(req, ERRDOS, ERRunknownlevel);
2519 #endif /* LARGE_SMB_OFF_T */
2521 /* the hard quotas 8 bytes (uint64_t)*/
2522 qt.hardlim = (uint64_t)IVAL(pdata,32);
2523 #ifdef LARGE_SMB_OFF_T
2524 qt.hardlim |= (((uint64_t)IVAL(pdata,36)) << 32);
2525 #else /* LARGE_SMB_OFF_T */
2526 if ((IVAL(pdata,36) != 0)&&
2527 ((qt.hardlim != 0xFFFFFFFF)||
2528 (IVAL(pdata,36)!=0xFFFFFFFF))) {
2529 /* more than 32 bits? */
2530 reply_doserror(req, ERRDOS, ERRunknownlevel);
2533 #endif /* LARGE_SMB_OFF_T */
2535 if (!sid_parse(pdata+40,sid_len,&sid)) {
2536 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2540 DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid)));
2542 /* 44 unknown bytes left... */
2544 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2545 reply_doserror(req, ERRSRV, ERRerror);
2549 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2552 #endif /* HAVE_SYS_QUOTAS */
2554 static void handle_nttrans(connection_struct *conn,
2555 struct trans_state *state,
2556 struct smb_request *req)
2558 if (Protocol >= PROTOCOL_NT1) {
2559 req->flags2 |= 0x40; /* IS_LONG_NAME */
2560 SSVAL(req->inbuf,smb_flg2,req->flags2);
2564 SMB_PERFCOUNT_SET_SUBOP(&req->pcd, state->call);
2566 /* Now we must call the relevant NT_TRANS function */
2567 switch(state->call) {
2568 case NT_TRANSACT_CREATE:
2570 START_PROFILE(NT_transact_create);
2571 call_nt_transact_create(
2573 &state->setup, state->setup_count,
2574 &state->param, state->total_param,
2575 &state->data, state->total_data,
2576 state->max_data_return);
2577 END_PROFILE(NT_transact_create);
2581 case NT_TRANSACT_IOCTL:
2583 START_PROFILE(NT_transact_ioctl);
2584 call_nt_transact_ioctl(
2586 &state->setup, state->setup_count,
2587 &state->param, state->total_param,
2588 &state->data, state->total_data,
2589 state->max_data_return);
2590 END_PROFILE(NT_transact_ioctl);
2594 case NT_TRANSACT_SET_SECURITY_DESC:
2596 START_PROFILE(NT_transact_set_security_desc);
2597 call_nt_transact_set_security_desc(
2599 &state->setup, state->setup_count,
2600 &state->param, state->total_param,
2601 &state->data, state->total_data,
2602 state->max_data_return);
2603 END_PROFILE(NT_transact_set_security_desc);
2607 case NT_TRANSACT_NOTIFY_CHANGE:
2609 START_PROFILE(NT_transact_notify_change);
2610 call_nt_transact_notify_change(
2612 &state->setup, state->setup_count,
2613 &state->param, state->total_param,
2614 &state->data, state->total_data,
2615 state->max_data_return,
2616 state->max_param_return);
2617 END_PROFILE(NT_transact_notify_change);
2621 case NT_TRANSACT_RENAME:
2623 START_PROFILE(NT_transact_rename);
2624 call_nt_transact_rename(
2626 &state->setup, state->setup_count,
2627 &state->param, state->total_param,
2628 &state->data, state->total_data,
2629 state->max_data_return);
2630 END_PROFILE(NT_transact_rename);
2634 case NT_TRANSACT_QUERY_SECURITY_DESC:
2636 START_PROFILE(NT_transact_query_security_desc);
2637 call_nt_transact_query_security_desc(
2639 &state->setup, state->setup_count,
2640 &state->param, state->total_param,
2641 &state->data, state->total_data,
2642 state->max_data_return);
2643 END_PROFILE(NT_transact_query_security_desc);
2647 #ifdef HAVE_SYS_QUOTAS
2648 case NT_TRANSACT_GET_USER_QUOTA:
2650 START_PROFILE(NT_transact_get_user_quota);
2651 call_nt_transact_get_user_quota(
2653 &state->setup, state->setup_count,
2654 &state->param, state->total_param,
2655 &state->data, state->total_data,
2656 state->max_data_return);
2657 END_PROFILE(NT_transact_get_user_quota);
2661 case NT_TRANSACT_SET_USER_QUOTA:
2663 START_PROFILE(NT_transact_set_user_quota);
2664 call_nt_transact_set_user_quota(
2666 &state->setup, state->setup_count,
2667 &state->param, state->total_param,
2668 &state->data, state->total_data,
2669 state->max_data_return);
2670 END_PROFILE(NT_transact_set_user_quota);
2673 #endif /* HAVE_SYS_QUOTAS */
2676 /* Error in request */
2677 DEBUG(0,("handle_nttrans: Unknown request %d in "
2678 "nttrans call\n", state->call));
2679 reply_doserror(req, ERRSRV, ERRerror);
2685 /****************************************************************************
2686 Reply to a SMBNTtrans.
2687 ****************************************************************************/
2689 void reply_nttrans(struct smb_request *req)
2691 connection_struct *conn = req->conn;
2696 uint16 function_code;
2698 struct trans_state *state;
2700 START_PROFILE(SMBnttrans);
2702 if (req->wct < 19) {
2703 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2704 END_PROFILE(SMBnttrans);
2708 pscnt = IVAL(req->vwv+9, 1);
2709 psoff = IVAL(req->vwv+11, 1);
2710 dscnt = IVAL(req->vwv+13, 1);
2711 dsoff = IVAL(req->vwv+15, 1);
2712 function_code = SVAL(req->vwv+18, 0);
2714 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2715 reply_doserror(req, ERRSRV, ERRaccess);
2716 END_PROFILE(SMBnttrans);
2720 result = allow_new_trans(conn->pending_trans, req->mid);
2721 if (!NT_STATUS_IS_OK(result)) {
2722 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
2723 reply_nterror(req, result);
2724 END_PROFILE(SMBnttrans);
2728 if ((state = TALLOC_P(conn, struct trans_state)) == NULL) {
2729 reply_doserror(req, ERRSRV, ERRaccess);
2730 END_PROFILE(SMBnttrans);
2734 state->cmd = SMBnttrans;
2736 state->mid = req->mid;
2737 state->vuid = req->vuid;
2738 state->total_data = IVAL(req->vwv+3, 1);
2740 state->total_param = IVAL(req->vwv+1, 1);
2741 state->param = NULL;
2742 state->max_data_return = IVAL(req->vwv+7, 1);
2743 state->max_param_return = IVAL(req->vwv+5, 1);
2745 /* setup count is in *words* */
2746 state->setup_count = 2*CVAL(req->vwv+17, 1);
2747 state->setup = NULL;
2748 state->call = function_code;
2750 DEBUG(10, ("num_setup=%u, "
2751 "param_total=%u, this_param=%u, max_param=%u, "
2752 "data_total=%u, this_data=%u, max_data=%u, "
2753 "param_offset=%u, data_offset=%u\n",
2754 (unsigned)state->setup_count,
2755 (unsigned)state->total_param, (unsigned)pscnt,
2756 (unsigned)state->max_param_return,
2757 (unsigned)state->total_data, (unsigned)dscnt,
2758 (unsigned)state->max_data_return,
2759 (unsigned)psoff, (unsigned)dsoff));
2762 * All nttrans messages we handle have smb_wct == 19 +
2763 * state->setup_count. Ensure this is so as a sanity check.
2766 if(req->wct != 19 + (state->setup_count/2)) {
2767 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2768 req->wct, 19 + (state->setup_count/2)));
2772 /* Don't allow more than 128mb for each value. */
2773 if ((state->total_data > (1024*1024*128)) ||
2774 (state->total_param > (1024*1024*128))) {
2775 reply_doserror(req, ERRDOS, ERRnomem);
2776 END_PROFILE(SMBnttrans);
2780 if ((dscnt > state->total_data) || (pscnt > state->total_param))
2783 if (state->total_data) {
2785 if (trans_oob(state->total_data, 0, dscnt)
2786 || trans_oob(smb_len(req->inbuf), dsoff, dscnt)) {
2790 /* Can't use talloc here, the core routines do realloc on the
2791 * params and data. */
2792 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
2793 DEBUG(0,("reply_nttrans: data malloc fail for %u "
2794 "bytes !\n", (unsigned int)state->total_data));
2796 reply_doserror(req, ERRDOS, ERRnomem);
2797 END_PROFILE(SMBnttrans);
2801 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
2804 if (state->total_param) {
2806 if (trans_oob(state->total_param, 0, pscnt)
2807 || trans_oob(smb_len(req->inbuf), psoff, pscnt)) {
2811 /* Can't use talloc here, the core routines do realloc on the
2812 * params and data. */
2813 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
2814 DEBUG(0,("reply_nttrans: param malloc fail for %u "
2815 "bytes !\n", (unsigned int)state->total_param));
2816 SAFE_FREE(state->data);
2818 reply_doserror(req, ERRDOS, ERRnomem);
2819 END_PROFILE(SMBnttrans);
2823 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
2826 state->received_data = dscnt;
2827 state->received_param = pscnt;
2829 if(state->setup_count > 0) {
2830 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2831 state->setup_count));
2834 * No overflow possible here, state->setup_count is an
2835 * unsigned int, being filled by a single byte from
2836 * CVAL(req->vwv+13, 0) above. The cast in the comparison
2837 * below is not necessary, it's here to clarify things. The
2838 * validity of req->vwv and req->wct has been checked in
2839 * init_smb_request already.
2841 if ((state->setup_count/2) + 19 > (unsigned int)req->wct) {
2845 state->setup = (uint16 *)TALLOC(state, state->setup_count);
2846 if (state->setup == NULL) {
2847 DEBUG(0,("reply_nttrans : Out of memory\n"));
2848 SAFE_FREE(state->data);
2849 SAFE_FREE(state->param);
2851 reply_doserror(req, ERRDOS, ERRnomem);
2852 END_PROFILE(SMBnttrans);
2856 memcpy(state->setup, req->vwv+19, state->setup_count);
2857 dump_data(10, (uint8 *)state->setup, state->setup_count);
2860 if ((state->received_data == state->total_data) &&
2861 (state->received_param == state->total_param)) {
2862 handle_nttrans(conn, state, req);
2863 SAFE_FREE(state->param);
2864 SAFE_FREE(state->data);
2866 END_PROFILE(SMBnttrans);
2870 DLIST_ADD(conn->pending_trans, state);
2872 /* We need to send an interim response then receive the rest
2873 of the parameter/data bytes */
2874 reply_outbuf(req, 0, 0);
2875 show_msg((char *)req->outbuf);
2876 END_PROFILE(SMBnttrans);
2881 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
2882 SAFE_FREE(state->data);
2883 SAFE_FREE(state->param);
2885 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2886 END_PROFILE(SMBnttrans);
2890 /****************************************************************************
2891 Reply to a SMBnttranss
2892 ****************************************************************************/
2894 void reply_nttranss(struct smb_request *req)
2896 connection_struct *conn = req->conn;
2897 uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
2898 struct trans_state *state;
2900 START_PROFILE(SMBnttranss);
2902 show_msg((char *)req->inbuf);
2904 if (req->wct < 18) {
2905 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2906 END_PROFILE(SMBnttranss);
2910 for (state = conn->pending_trans; state != NULL;
2911 state = state->next) {
2912 if (state->mid == req->mid) {
2917 if ((state == NULL) || (state->cmd != SMBnttrans)) {
2918 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2919 END_PROFILE(SMBnttranss);
2923 /* Revise state->total_param and state->total_data in case they have
2924 changed downwards */
2925 if (IVAL(req->vwv+1, 1) < state->total_param) {
2926 state->total_param = IVAL(req->vwv+1, 1);
2928 if (IVAL(req->vwv+3, 1) < state->total_data) {
2929 state->total_data = IVAL(req->vwv+3, 1);
2932 pcnt = IVAL(req->vwv+5, 1);
2933 poff = IVAL(req->vwv+7, 1);
2934 pdisp = IVAL(req->vwv+9, 1);
2936 dcnt = IVAL(req->vwv+11, 1);
2937 doff = IVAL(req->vwv+13, 1);
2938 ddisp = IVAL(req->vwv+15, 1);
2940 state->received_param += pcnt;
2941 state->received_data += dcnt;
2943 if ((state->received_data > state->total_data) ||
2944 (state->received_param > state->total_param))
2948 if (trans_oob(state->total_param, pdisp, pcnt)
2949 || trans_oob(smb_len(req->inbuf), poff, pcnt)) {
2952 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,pcnt);
2956 if (trans_oob(state->total_data, ddisp, dcnt)
2957 || trans_oob(smb_len(req->inbuf), doff, dcnt)) {
2960 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
2963 if ((state->received_param < state->total_param) ||
2964 (state->received_data < state->total_data)) {
2965 END_PROFILE(SMBnttranss);
2969 handle_nttrans(conn, state, req);
2971 DLIST_REMOVE(conn->pending_trans, state);
2972 SAFE_FREE(state->data);
2973 SAFE_FREE(state->param);
2975 END_PROFILE(SMBnttranss);
2980 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
2981 DLIST_REMOVE(conn->pending_trans, state);
2982 SAFE_FREE(state->data);
2983 SAFE_FREE(state->param);
2985 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2986 END_PROFILE(SMBnttranss);