2 Unix SMB/CIFS implementation.
3 SMB NT transaction handling
4 Copyright (C) Jeremy Allison 1994-2007
5 Copyright (C) Stefan (metze) Metzmacher 2003
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 extern enum protocol_types Protocol;
25 extern struct current_user current_user;
27 static const char *known_nt_pipes[] = {
48 static char *nttrans_realloc(char **ptr, size_t size)
51 smb_panic("nttrans_realloc() called with NULL ptr");
54 *ptr = (char *)SMB_REALLOC(*ptr, size);
58 memset(*ptr,'\0',size);
62 /****************************************************************************
63 Send the required number of replies back.
64 We assume all fields other than the data fields are
65 set correctly for the type of call.
66 HACK ! Always assumes smb_setup field is zero.
67 ****************************************************************************/
69 void send_nt_replies(struct smb_request *req, NTSTATUS nt_error,
70 char *params, int paramsize,
71 char *pdata, int datasize)
73 int data_to_send = datasize;
74 int params_to_send = paramsize;
78 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
79 int alignment_offset = 3;
80 int data_alignment_offset = 0;
83 * If there genuinely are no parameters or data to send just send
87 if(params_to_send == 0 && data_to_send == 0) {
88 reply_outbuf(req, 18, 0);
89 show_msg((char *)req->outbuf);
94 * When sending params and data ensure that both are nicely aligned.
95 * Only do this alignment when there is also data to send - else
96 * can cause NT redirector problems.
99 if (((params_to_send % 4) != 0) && (data_to_send != 0)) {
100 data_alignment_offset = 4 - (params_to_send % 4);
104 * Space is bufsize minus Netbios over TCP header minus SMB header.
105 * The alignment_offset is to align the param bytes on a four byte
106 * boundary (2 bytes for data len, one byte pad).
107 * NT needs this to work correctly.
110 useable_space = max_send - (smb_size
113 + data_alignment_offset);
116 * useable_space can never be more than max_send minus the
120 useable_space = MIN(useable_space,
121 max_send - (alignment_offset+data_alignment_offset));
124 while (params_to_send || data_to_send) {
127 * Calculate whether we will totally or partially fill this packet.
130 total_sent_thistime = params_to_send + data_to_send +
131 alignment_offset + data_alignment_offset;
134 * We can never send more than useable_space.
137 total_sent_thistime = MIN(total_sent_thistime, useable_space);
139 reply_outbuf(req, 18, total_sent_thistime);
142 * Set total params and data to be sent.
145 SIVAL(req->outbuf,smb_ntr_TotalParameterCount,paramsize);
146 SIVAL(req->outbuf,smb_ntr_TotalDataCount,datasize);
149 * Calculate how many parameters and data we can fit into
150 * this packet. Parameters get precedence.
153 params_sent_thistime = MIN(params_to_send,useable_space);
154 data_sent_thistime = useable_space - params_sent_thistime;
155 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
157 SIVAL(req->outbuf, smb_ntr_ParameterCount,
158 params_sent_thistime);
160 if(params_sent_thistime == 0) {
161 SIVAL(req->outbuf,smb_ntr_ParameterOffset,0);
162 SIVAL(req->outbuf,smb_ntr_ParameterDisplacement,0);
165 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
166 * parameter bytes, however the first 4 bytes of outbuf are
167 * the Netbios over TCP header. Thus use smb_base() to subtract
168 * them from the calculation.
171 SIVAL(req->outbuf,smb_ntr_ParameterOffset,
172 ((smb_buf(req->outbuf)+alignment_offset)
173 - smb_base(req->outbuf)));
175 * Absolute displacement of param bytes sent in this packet.
178 SIVAL(req->outbuf, smb_ntr_ParameterDisplacement,
183 * Deal with the data portion.
186 SIVAL(req->outbuf, smb_ntr_DataCount, data_sent_thistime);
188 if(data_sent_thistime == 0) {
189 SIVAL(req->outbuf,smb_ntr_DataOffset,0);
190 SIVAL(req->outbuf,smb_ntr_DataDisplacement, 0);
193 * The offset of the data bytes is the offset of the
194 * parameter bytes plus the number of parameters being sent this time.
197 SIVAL(req->outbuf, smb_ntr_DataOffset,
198 ((smb_buf(req->outbuf)+alignment_offset) -
199 smb_base(req->outbuf))
200 + params_sent_thistime + data_alignment_offset);
201 SIVAL(req->outbuf,smb_ntr_DataDisplacement, pd - pdata);
205 * Copy the param bytes into the packet.
208 if(params_sent_thistime) {
209 if (alignment_offset != 0) {
210 memset(smb_buf(req->outbuf), 0,
213 memcpy((smb_buf(req->outbuf)+alignment_offset), pp,
214 params_sent_thistime);
218 * Copy in the data bytes
221 if(data_sent_thistime) {
222 if (data_alignment_offset != 0) {
223 memset((smb_buf(req->outbuf)+alignment_offset+
224 params_sent_thistime), 0,
225 data_alignment_offset);
227 memcpy(smb_buf(req->outbuf)+alignment_offset
228 +params_sent_thistime+data_alignment_offset,
229 pd,data_sent_thistime);
232 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
233 params_sent_thistime, data_sent_thistime, useable_space));
234 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
235 params_to_send, data_to_send, paramsize, datasize));
237 if (NT_STATUS_V(nt_error)) {
238 error_packet_set((char *)req->outbuf,
243 /* Send the packet */
244 show_msg((char *)req->outbuf);
245 if (!send_smb(smbd_server_fd(),(char *)req->outbuf)) {
246 exit_server_cleanly("send_nt_replies: send_smb failed.");
249 TALLOC_FREE(req->outbuf);
251 pp += params_sent_thistime;
252 pd += data_sent_thistime;
254 params_to_send -= params_sent_thistime;
255 data_to_send -= data_sent_thistime;
261 if(params_to_send < 0 || data_to_send < 0) {
262 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
263 params_to_send, data_to_send));
269 /****************************************************************************
270 Is it an NTFS stream name ?
271 ****************************************************************************/
273 bool is_ntfs_stream_name(const char *fname)
275 if (lp_posix_pathnames()) {
278 return (strchr_m(fname, ':') != NULL) ? True : False;
281 struct case_semantics_state {
282 connection_struct *conn;
285 bool short_case_preserve;
288 /****************************************************************************
289 Restore case semantics.
290 ****************************************************************************/
291 static int restore_case_semantics(struct case_semantics_state *state)
293 state->conn->case_sensitive = state->case_sensitive;
294 state->conn->case_preserve = state->case_preserve;
295 state->conn->short_case_preserve = state->short_case_preserve;
299 /****************************************************************************
301 ****************************************************************************/
302 static struct case_semantics_state *set_posix_case_semantics(TALLOC_CTX *mem_ctx,
303 connection_struct *conn)
305 struct case_semantics_state *result;
307 if (!(result = talloc(mem_ctx, struct case_semantics_state))) {
308 DEBUG(0, ("talloc failed\n"));
313 result->case_sensitive = conn->case_sensitive;
314 result->case_preserve = conn->case_preserve;
315 result->short_case_preserve = conn->short_case_preserve;
318 conn->case_sensitive = True;
319 conn->case_preserve = True;
320 conn->short_case_preserve = True;
322 talloc_set_destructor(result, restore_case_semantics);
327 /****************************************************************************
328 Reply to an NT create and X call on a pipe
329 ****************************************************************************/
331 static void nt_open_pipe(char *fname, connection_struct *conn,
332 struct smb_request *req, int *ppnum)
334 smb_np_struct *p = NULL;
337 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
339 /* See if it is one we want to handle. */
341 if (lp_disable_spoolss() && strequal(fname, "\\spoolss")) {
342 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
347 for( i = 0; known_nt_pipes[i]; i++ ) {
348 if( strequal(fname,known_nt_pipes[i])) {
353 if ( known_nt_pipes[i] == NULL ) {
354 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
359 /* Strip \\ off the name. */
362 DEBUG(3,("nt_open_pipe: Known pipe %s opening.\n", fname));
364 p = open_rpc_pipe_p(fname, conn, req->vuid);
366 reply_doserror(req, ERRSRV, ERRnofids);
370 /* TODO: Add pipe to db */
372 if ( !store_pipe_opendb( p ) ) {
373 DEBUG(3,("nt_open_pipe: failed to store %s pipe open.\n", fname));
380 /****************************************************************************
381 Reply to an NT create and X call for pipes.
382 ****************************************************************************/
384 static void do_ntcreate_pipe_open(connection_struct *conn,
385 struct smb_request *req)
390 uint32 flags = IVAL(req->inbuf,smb_ntcreate_Flags);
391 TALLOC_CTX *ctx = talloc_tos();
393 srvstr_pull_buf_talloc(ctx, (char *)req->inbuf, req->flags2, &fname,
394 smb_buf(req->inbuf), STR_TERMINATE);
397 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
401 nt_open_pipe(fname, conn, req, &pnum);
409 * Deal with pipe return.
412 if (flags & EXTENDED_RESPONSE_REQUIRED) {
413 /* This is very strange. We
414 * return 50 words, but only set
415 * the wcnt to 42 ? It's definately
416 * what happens on the wire....
418 reply_outbuf(req, 50, 0);
419 SCVAL(req->outbuf,smb_wct,42);
421 reply_outbuf(req, 34, 0);
424 p = (char *)req->outbuf + smb_vwv2;
428 SIVAL(p,0,FILE_WAS_OPENED);
431 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
434 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
436 SSVAL(p,2, 0x5FF); /* ? */
439 if (flags & EXTENDED_RESPONSE_REQUIRED) {
441 SIVAL(p,0,FILE_GENERIC_ALL);
443 * For pipes W2K3 seems to return
445 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
447 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
450 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
455 static NTSTATUS create_file(connection_struct *conn,
456 struct smb_request *req,
457 uint16_t root_dir_fid,
460 uint32_t access_mask,
461 uint32_t file_attributes,
462 uint32_t share_access,
463 uint32_t create_disposition,
464 uint32_t create_options,
466 SMB_BIG_UINT allocation_size,
467 struct security_descriptor *sd,
468 struct ea_list *ea_list,
470 files_struct **result,
472 uint8_t *poplock_granted,
473 SMB_STRUCT_STAT *psbuf,
474 SMB_OFF_T *pfile_len)
476 TALLOC_CTX *frame = talloc_stackframe();
477 struct case_semantics_state *case_state = NULL;
479 SMB_OFF_T file_len = 0;
480 SMB_STRUCT_STAT sbuf;
481 int info = FILE_WAS_OPENED;
482 files_struct *fsp = NULL;
483 uint8_t oplock_granted = NO_OPLOCK_RETURN;
486 SET_STAT_INVALID(sbuf);
488 if (create_options & FILE_OPEN_BY_FILE_ID) {
489 status = NT_STATUS_NOT_SUPPORTED;
497 if (root_dir_fid != 0) {
499 * This filename is relative to a directory fid.
501 char *parent_fname = NULL;
502 files_struct *dir_fsp = file_fsp(root_dir_fid);
504 if (dir_fsp == NULL) {
505 status = NT_STATUS_INVALID_HANDLE;
509 if (!dir_fsp->is_directory) {
512 * Check to see if this is a mac fork of some kind.
515 if (is_ntfs_stream_name(fname)) {
516 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
521 we need to handle the case when we get a
522 relative open relative to a file and the
523 pathname is blank - this is a reopen!
524 (hint from demyn plantenberg)
527 status = NT_STATUS_INVALID_HANDLE;
531 if (ISDOT(dir_fsp->fsp_name)) {
533 * We're at the toplevel dir, the final file name
534 * must not contain ./, as this is filtered out
535 * normally by srvstr_get_path and unix_convert
536 * explicitly rejects paths containing ./.
538 parent_fname = talloc_strdup(talloc_tos(), "");
539 if (parent_fname == NULL) {
540 status = NT_STATUS_NO_MEMORY;
544 size_t dir_name_len = strlen(dir_fsp->fsp_name);
547 * Copy in the base directory name.
550 parent_fname = TALLOC_ARRAY(talloc_tos(), char,
552 if (parent_fname == NULL) {
553 status = NT_STATUS_NO_MEMORY;
556 memcpy(parent_fname, dir_fsp->fsp_name,
560 * Ensure it ends in a '/'.
561 * We used TALLOC_SIZE +2 to add space for the '/'.
565 && (parent_fname[dir_name_len-1] != '\\')
566 && (parent_fname[dir_name_len-1] != '/')) {
567 parent_fname[dir_name_len] = '/';
568 parent_fname[dir_name_len+1] = '\0';
572 fname = talloc_asprintf(talloc_tos(), "%s%s", parent_fname,
575 status = NT_STATUS_NO_MEMORY;
580 * Check to see if this is a mac fork of some kind.
583 if (is_ntfs_stream_name(fname)) {
584 enum FAKE_FILE_TYPE fake_file_type;
586 fake_file_type = is_fake_file(fname);
588 if (fake_file_type == FAKE_FILE_TYPE_NONE) {
589 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
593 * Here we go! support for changing the disk quotas
596 * We need to fake up to open this MAGIC QUOTA file
597 * and return a valid FID.
599 * w2k close this file directly after openening xp
600 * also tries a QUERY_FILE_INFO on the file and then
603 status = open_fake_file(conn, fake_file_type, fname,
605 if (!NT_STATUS_IS_OK(status)) {
613 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
614 if (oplock_request) {
615 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
619 status = resolve_dfspath(
620 talloc_tos(), conn, req->flags2 & FLAGS2_DFS_PATHNAMES,
623 if (!NT_STATUS_IS_OK(status)) {
625 * For PATH_NOT_COVERED we had
626 * reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
627 * ERRSRV, ERRbadpath);
628 * Need to fix in callers
634 * Ordinary file or directory.
638 * Check if POSIX semantics are wanted.
641 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
642 case_state = set_posix_case_semantics(talloc_tos(), conn);
643 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
646 status = unix_convert(talloc_tos(), conn, fname, False, &fname, NULL,
648 if (!NT_STATUS_IS_OK(status)) {
652 /* All file access must go through check_name() */
654 status = check_name(conn, fname);
655 if (!NT_STATUS_IS_OK(status)) {
659 /* This is the correct thing to do (check every time) but can_delete
660 * is expensive (it may have to read the parent directory
661 * permissions). So for now we're not doing it unless we have a strong
662 * hint the client is really going to delete this file. If the client
663 * is forcing FILE_CREATE let the filesystem take care of the
666 /* Setting FILE_SHARE_DELETE is the hint. */
668 if (lp_acl_check_permissions(SNUM(conn))
669 && (create_disposition != FILE_CREATE)
670 && (share_access & FILE_SHARE_DELETE)
671 && (access_mask & DELETE_ACCESS)
672 && (((dos_mode(conn, fname, &sbuf) & FILE_ATTRIBUTE_READONLY)
673 && !lp_delete_readonly(SNUM(conn)))
674 || !can_delete_file_in_directory(conn, fname))) {
675 status = NT_STATUS_ACCESS_DENIED;
680 /* We need to support SeSecurityPrivilege for this. */
681 if ((access_mask & SEC_RIGHT_SYSTEM_SECURITY) &&
682 !user_has_privileges(current_user.nt_user_token,
684 status = NT_STATUS_PRIVILEGE_NOT_HELD;
690 * If it's a request for a directory open, deal with it separately.
693 if (create_options & FILE_DIRECTORY_FILE) {
695 /* Can't open a temp directory. IFS kit test. */
696 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
697 status = NT_STATUS_INVALID_PARAMETER;
702 * We will get a create directory here if the Win32
703 * app specified a security descriptor in the
704 * CreateDirectory() call.
708 status = open_directory(
709 conn, req, fname, &sbuf, access_mask, share_access,
710 create_disposition, create_options, file_attributes,
715 * Ordinary file case.
718 status = open_file_ntcreate(
719 conn, req, fname, &sbuf, access_mask, share_access,
720 create_disposition, create_options, file_attributes,
721 oplock_request, &info, &fsp);
723 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
726 * Fail the open if it was explicitly a non-directory
730 if (create_options & FILE_NON_DIRECTORY_FILE) {
731 status = NT_STATUS_FILE_IS_A_DIRECTORY;
736 status = open_directory(
737 conn, req, fname, &sbuf, access_mask,
738 share_access, create_disposition,
739 create_options, file_attributes,
744 TALLOC_FREE(case_state);
746 if (!NT_STATUS_IS_OK(status)) {
751 * According to the MS documentation, the only time the security
752 * descriptor is applied to the opened file is iff we *created* the
753 * file; an existing file stays the same.
755 * Also, it seems (from observation) that you can open the file with
756 * any access mask but you can still write the sd. We need to override
757 * the granted access before we call set_sd
758 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
761 if ((sd != NULL) && (info == FILE_WAS_CREATED)
762 && lp_nt_acl_support(SNUM(conn))) {
764 uint32_t sec_info_sent = ALL_SECURITY_INFORMATION;
765 uint32_t saved_access_mask = fsp->access_mask;
767 if (sd->owner_sid==0) {
768 sec_info_sent &= ~OWNER_SECURITY_INFORMATION;
770 if (sd->group_sid==0) {
771 sec_info_sent &= ~GROUP_SECURITY_INFORMATION;
774 sec_info_sent &= ~SACL_SECURITY_INFORMATION;
777 sec_info_sent &= ~DACL_SECURITY_INFORMATION;
780 fsp->access_mask = FILE_GENERIC_ALL;
782 status = SMB_VFS_FSET_NT_ACL(
783 fsp, fsp->fh->fd, sec_info_sent, sd);
785 fsp->access_mask = saved_access_mask;
787 if (!NT_STATUS_IS_OK(status)) {
792 if ((ea_list != NULL) && (info == FILE_WAS_CREATED)) {
793 status = set_ea(conn, fsp, fname, ea_list);
794 if (!NT_STATUS_IS_OK(status)) {
799 file_len = sbuf.st_size;
800 fattr = dos_mode(conn,fname,&sbuf);
802 fattr = FILE_ATTRIBUTE_NORMAL;
804 if (!fsp->is_directory && (fattr & aDIR)) {
805 status = NT_STATUS_ACCESS_DENIED;
809 /* Save the requested allocation size. */
810 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
812 && (allocation_size > (SMB_BIG_UINT)file_len)) {
813 fsp->initial_allocation_size = smb_roundup(
814 fsp->conn, allocation_size);
815 if (fsp->is_directory) {
816 /* Can't set allocation size on a directory. */
817 status = NT_STATUS_ACCESS_DENIED;
820 if (vfs_allocate_file_space(
821 fsp, fsp->initial_allocation_size) == -1) {
822 status = NT_STATUS_DISK_FULL;
826 fsp->initial_allocation_size = smb_roundup(fsp->conn,(SMB_BIG_UINT)file_len);
831 * If the caller set the extended oplock request bit
832 * and we granted one (by whatever means) - set the
833 * correct bit for extended oplock reply.
836 if (oplock_request &&
837 (lp_fake_oplocks(SNUM(conn))
838 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
841 * Exclusive oplock granted
844 if (flags & REQUEST_BATCH_OPLOCK) {
845 oplock_granted = BATCH_OPLOCK_RETURN;
847 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
849 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
850 oplock_granted = LEVEL_II_OPLOCK_RETURN;
852 oplock_granted = NO_OPLOCK_RETURN;
858 *poplock_granted = oplock_granted;
860 *pfile_len = file_len;
866 close_file(fsp, ERROR_CLOSE);
873 /****************************************************************************
874 Reply to an NT create and X call.
875 ****************************************************************************/
877 void reply_ntcreate_and_X(connection_struct *conn, struct smb_request *req)
882 uint32 file_attributes;
884 uint32 create_disposition;
885 uint32 create_options;
887 SMB_BIG_UINT allocation_size;
888 /* Breakout the oplock request bits so we can set the
889 reply bits separately. */
890 int oplock_request = 0;
892 SMB_OFF_T file_len = 0;
893 SMB_STRUCT_STAT sbuf;
895 files_struct *fsp = NULL;
897 struct timespec c_timespec;
898 struct timespec a_timespec;
899 struct timespec m_timespec;
901 uint8_t oplock_granted = NO_OPLOCK_RETURN;
902 TALLOC_CTX *ctx = talloc_tos();
904 START_PROFILE(SMBntcreateX);
907 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
911 flags = IVAL(req->inbuf,smb_ntcreate_Flags);
912 access_mask = IVAL(req->inbuf,smb_ntcreate_DesiredAccess);
913 file_attributes = IVAL(req->inbuf,smb_ntcreate_FileAttributes);
914 share_access = IVAL(req->inbuf,smb_ntcreate_ShareAccess);
915 create_disposition = IVAL(req->inbuf,smb_ntcreate_CreateDisposition);
916 create_options = IVAL(req->inbuf,smb_ntcreate_CreateOptions);
917 root_dir_fid = (uint16)IVAL(req->inbuf,smb_ntcreate_RootDirectoryFid);
919 allocation_size = (SMB_BIG_UINT)IVAL(req->inbuf,
920 smb_ntcreate_AllocationSize);
921 #ifdef LARGE_SMB_OFF_T
922 allocation_size |= (((SMB_BIG_UINT)IVAL(
924 smb_ntcreate_AllocationSize + 4)) << 32);
927 srvstr_get_path(ctx, (char *)req->inbuf, req->flags2, &fname,
928 smb_buf(req->inbuf), 0, STR_TERMINATE, &status);
930 if (!NT_STATUS_IS_OK(status)) {
931 reply_nterror(req, status);
932 END_PROFILE(SMBntcreateX);
936 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
937 "file_attributes = 0x%x, share_access = 0x%x, "
938 "create_disposition = 0x%x create_options = 0x%x "
939 "root_dir_fid = 0x%x, fname = %s\n",
941 (unsigned int)access_mask,
942 (unsigned int)file_attributes,
943 (unsigned int)share_access,
944 (unsigned int)create_disposition,
945 (unsigned int)create_options,
946 (unsigned int)root_dir_fid,
950 * If it's an IPC, use the pipe handler.
954 if (lp_nt_pipe_support()) {
955 do_ntcreate_pipe_open(conn, req);
956 END_PROFILE(SMBntcreateX);
959 reply_doserror(req, ERRDOS, ERRnoaccess);
960 END_PROFILE(SMBntcreateX);
965 status = create_file(conn, req, root_dir_fid, fname, flags,
966 access_mask, file_attributes, share_access,
967 create_disposition, create_options,
968 oplock_request, allocation_size, NULL, NULL,
969 &fsp, &info, &oplock_granted, &sbuf, &file_len);
971 if (!NT_STATUS_IS_OK(status)) {
972 if (open_was_deferred(req->mid)) {
973 /* We have re-scheduled this call, no error. */
974 END_PROFILE(SMBntcreateX);
977 reply_nterror(req, status);
978 END_PROFILE(SMBntcreateX);
982 DEBUG(10, ("create_file returned fsp %p\n", fsp));
984 if (flags & EXTENDED_RESPONSE_REQUIRED) {
985 /* This is very strange. We
986 * return 50 words, but only set
987 * the wcnt to 42 ? It's definately
988 * what happens on the wire....
990 reply_outbuf(req, 50, 0);
991 SCVAL(req->outbuf,smb_wct,42);
993 reply_outbuf(req, 34, 0);
996 p = (char *)req->outbuf + smb_vwv2;
998 SCVAL(p, 0, oplock_granted);
1001 SSVAL(p,0,fsp->fnum);
1003 if ((create_disposition == FILE_SUPERSEDE)
1004 && (info == FILE_WAS_OVERWRITTEN)) {
1005 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1012 c_timespec = get_create_timespec(
1013 &sbuf,lp_fake_dir_create_times(SNUM(conn)));
1014 a_timespec = get_atimespec(&sbuf);
1015 m_timespec = get_mtimespec(&sbuf);
1017 if (lp_dos_filetime_resolution(SNUM(conn))) {
1018 dos_filetime_timespec(&c_timespec);
1019 dos_filetime_timespec(&a_timespec);
1020 dos_filetime_timespec(&m_timespec);
1023 put_long_date_timespec(p, c_timespec); /* create time. */
1025 put_long_date_timespec(p, a_timespec); /* access time */
1027 put_long_date_timespec(p, m_timespec); /* write time */
1029 put_long_date_timespec(p, m_timespec); /* change time */
1031 SIVAL(p,0,fattr); /* File Attributes. */
1033 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
1035 SOFF_T(p,0,file_len);
1037 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1041 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1043 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1046 if (fsp->is_directory
1047 || can_write_to_file(conn, fname, &sbuf)) {
1048 perms = FILE_GENERIC_ALL;
1050 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1055 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n",
1056 fsp->fnum, fsp->fsp_name));
1059 END_PROFILE(SMBntcreateX);
1063 /****************************************************************************
1064 Reply to a NT_TRANSACT_CREATE call to open a pipe.
1065 ****************************************************************************/
1067 static void do_nt_transact_create_pipe(connection_struct *conn,
1068 struct smb_request *req,
1069 uint16 **ppsetup, uint32 setup_count,
1070 char **ppparams, uint32 parameter_count,
1071 char **ppdata, uint32 data_count)
1074 char *params = *ppparams;
1080 TALLOC_CTX *ctx = talloc_tos();
1083 * Ensure minimum number of parameters sent.
1086 if(parameter_count < 54) {
1087 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1088 reply_doserror(req, ERRDOS, ERRnoaccess);
1092 flags = IVAL(params,0);
1094 srvstr_get_path(ctx, params, req->flags2, &fname, params+53,
1095 parameter_count-53, STR_TERMINATE,
1097 if (!NT_STATUS_IS_OK(status)) {
1098 reply_nterror(req, status);
1102 nt_open_pipe(fname, conn, req, &pnum);
1109 /* Realloc the size of parameters and data we will return */
1110 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1111 /* Extended response is 32 more byyes. */
1116 params = nttrans_realloc(ppparams, param_len);
1117 if(params == NULL) {
1118 reply_doserror(req, ERRDOS, ERRnomem);
1123 SCVAL(p,0,NO_OPLOCK_RETURN);
1128 SIVAL(p,0,FILE_WAS_OPENED);
1132 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
1135 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
1137 SSVAL(p,2, 0x5FF); /* ? */
1140 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1142 SIVAL(p,0,FILE_GENERIC_ALL);
1144 * For pipes W2K3 seems to return
1146 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
1148 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
1151 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
1153 /* Send the required number of replies */
1154 send_nt_replies(req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1159 /****************************************************************************
1160 Internal fn to set security descriptors.
1161 ****************************************************************************/
1163 static NTSTATUS set_sd(files_struct *fsp, char *data, uint32 sd_len, uint32 security_info_sent)
1166 SEC_DESC *psd = NULL;
1167 TALLOC_CTX *mem_ctx;
1170 if (sd_len == 0 || !lp_nt_acl_support(SNUM(fsp->conn))) {
1171 return NT_STATUS_OK;
1175 * Init the parse struct we will unmarshall from.
1178 if ((mem_ctx = talloc_init("set_sd")) == NULL) {
1179 DEBUG(0,("set_sd: talloc_init failed.\n"));
1180 return NT_STATUS_NO_MEMORY;
1183 prs_init(&pd, 0, mem_ctx, UNMARSHALL);
1186 * Setup the prs_struct to point at the memory we just
1190 prs_give_memory( &pd, data, sd_len, False);
1193 * Finally, unmarshall from the data buffer.
1196 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
1197 DEBUG(0,("set_sd: Error in unmarshalling security descriptor.\n"));
1199 * Return access denied for want of a better error message..
1201 talloc_destroy(mem_ctx);
1202 return NT_STATUS_NO_MEMORY;
1205 if (psd->owner_sid==0) {
1206 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
1208 if (psd->group_sid==0) {
1209 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
1212 security_info_sent &= ~SACL_SECURITY_INFORMATION;
1215 security_info_sent &= ~DACL_SECURITY_INFORMATION;
1218 status = SMB_VFS_FSET_NT_ACL( fsp, fsp->fh->fd, security_info_sent, psd);
1220 talloc_destroy(mem_ctx);
1224 /****************************************************************************
1225 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
1226 ****************************************************************************/
1228 static struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
1230 struct ea_list *ea_list_head = NULL;
1233 if (data_size < 4) {
1237 while (offset + 4 <= data_size) {
1238 size_t next_offset = IVAL(pdata,offset);
1239 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
1245 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
1246 if (next_offset == 0) {
1249 offset += next_offset;
1252 return ea_list_head;
1255 /****************************************************************************
1256 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
1257 ****************************************************************************/
1259 static void call_nt_transact_create(connection_struct *conn,
1260 struct smb_request *req,
1261 uint16 **ppsetup, uint32 setup_count,
1262 char **ppparams, uint32 parameter_count,
1263 char **ppdata, uint32 data_count,
1264 uint32 max_data_count)
1267 char *params = *ppparams;
1268 char *data = *ppdata;
1269 /* Breakout the oplock request bits so we can set the reply bits separately. */
1270 int oplock_request = 0;
1272 SMB_OFF_T file_len = 0;
1273 SMB_STRUCT_STAT sbuf;
1275 files_struct *fsp = NULL;
1279 uint32 file_attributes;
1280 uint32 share_access;
1281 uint32 create_disposition;
1282 uint32 create_options;
1284 struct security_descriptor *sd;
1286 uint16 root_dir_fid;
1287 struct timespec c_timespec;
1288 struct timespec a_timespec;
1289 struct timespec m_timespec;
1290 struct ea_list *ea_list = NULL;
1293 SMB_BIG_UINT allocation_size;
1294 uint8_t oplock_granted;
1295 TALLOC_CTX *ctx = talloc_tos();
1297 DEBUG(5,("call_nt_transact_create\n"));
1300 * If it's an IPC, use the pipe handler.
1304 if (lp_nt_pipe_support()) {
1305 do_nt_transact_create_pipe(
1307 ppsetup, setup_count,
1308 ppparams, parameter_count,
1309 ppdata, data_count);
1312 reply_doserror(req, ERRDOS, ERRnoaccess);
1318 * Ensure minimum number of parameters sent.
1321 if(parameter_count < 54) {
1322 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1323 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1327 flags = IVAL(params,0);
1328 access_mask = IVAL(params,8);
1329 file_attributes = IVAL(params,20);
1330 share_access = IVAL(params,24);
1331 create_disposition = IVAL(params,28);
1332 create_options = IVAL(params,32);
1333 sd_len = IVAL(params,36);
1334 ea_len = IVAL(params,40);
1335 root_dir_fid = (uint16)IVAL(params,4);
1336 allocation_size = (SMB_BIG_UINT)IVAL(params,12);
1337 #ifdef LARGE_SMB_OFF_T
1338 allocation_size |= (((SMB_BIG_UINT)IVAL(params,16)) << 32);
1341 /* Ensure the data_len is correct for the sd and ea values given. */
1342 if ((ea_len + sd_len > data_count)
1343 || (ea_len > data_count) || (sd_len > data_count)
1344 || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
1345 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
1346 "%u, data_count = %u\n", (unsigned int)ea_len,
1347 (unsigned int)sd_len, (unsigned int)data_count));
1348 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1353 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
1355 if (!NT_STATUS_IS_OK(status)) {
1356 DEBUG(10, ("call_nt_transact_create: "
1357 "unmarshall_sec_desc failed: %s\n",
1358 nt_errstr(status)));
1359 reply_nterror(req, status);
1365 if (!lp_ea_support(SNUM(conn))) {
1366 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
1367 "EA's not supported.\n",
1368 (unsigned int)ea_len));
1369 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
1374 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
1375 "too small (should be more than 10)\n",
1376 (unsigned int)ea_len ));
1377 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1381 /* We have already checked that ea_len <= data_count here. */
1382 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
1384 if (ea_list == NULL) {
1385 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1390 srvstr_get_path(ctx, params, req->flags2, &fname,
1391 params+53, parameter_count-53,
1392 STR_TERMINATE, &status);
1393 if (!NT_STATUS_IS_OK(status)) {
1394 reply_nterror(req, status);
1398 status = create_file(conn, req, root_dir_fid, fname, flags,
1399 access_mask, file_attributes, share_access,
1400 create_disposition, create_options,
1401 oplock_request, allocation_size, sd, ea_list,
1402 &fsp, &info, &oplock_granted, &sbuf, &file_len);
1404 if(!NT_STATUS_IS_OK(status)) {
1405 if (open_was_deferred(req->mid)) {
1406 /* We have re-scheduled this call, no error. */
1409 reply_nterror(req, status);
1413 /* Realloc the size of parameters and data we will return */
1414 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1415 /* Extended response is 32 more byyes. */
1420 params = nttrans_realloc(ppparams, param_len);
1421 if(params == NULL) {
1422 reply_doserror(req, ERRDOS, ERRnomem);
1427 SCVAL(p, 0, oplock_granted);
1430 SSVAL(p,0,fsp->fnum);
1432 if ((create_disposition == FILE_SUPERSEDE)
1433 && (info == FILE_WAS_OVERWRITTEN)) {
1434 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1441 c_timespec = get_create_timespec(
1442 &sbuf,lp_fake_dir_create_times(SNUM(conn)));
1443 a_timespec = get_atimespec(&sbuf);
1444 m_timespec = get_mtimespec(&sbuf);
1446 if (lp_dos_filetime_resolution(SNUM(conn))) {
1447 dos_filetime_timespec(&c_timespec);
1448 dos_filetime_timespec(&a_timespec);
1449 dos_filetime_timespec(&m_timespec);
1452 put_long_date_timespec(p, c_timespec); /* create time. */
1454 put_long_date_timespec(p, a_timespec); /* access time */
1456 put_long_date_timespec(p, m_timespec); /* write time */
1458 put_long_date_timespec(p, m_timespec); /* change time */
1460 SIVAL(p,0,fattr); /* File Attributes. */
1462 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
1464 SOFF_T(p,0,file_len);
1466 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1470 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1472 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1475 if (fsp->is_directory
1476 || can_write_to_file(conn, fname, &sbuf)) {
1477 perms = FILE_GENERIC_ALL;
1479 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1484 DEBUG(5,("call_nt_transact_create: open name = %s\n", fname));
1486 /* Send the required number of replies */
1487 send_nt_replies(req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1492 /****************************************************************************
1493 Reply to a NT CANCEL request.
1494 conn POINTER CAN BE NULL HERE !
1495 ****************************************************************************/
1497 void reply_ntcancel(connection_struct *conn, struct smb_request *req)
1500 * Go through and cancel any pending change notifies.
1503 START_PROFILE(SMBntcancel);
1504 remove_pending_change_notify_requests_by_mid(req->mid);
1505 remove_pending_lock_requests_by_mid(req->mid);
1506 srv_cancel_sign_response(req->mid);
1508 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", req->mid));
1510 END_PROFILE(SMBntcancel);
1514 /****************************************************************************
1516 ****************************************************************************/
1518 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
1519 connection_struct *conn,
1520 struct smb_request *req,
1521 const char *oldname_in,
1522 const char *newname_in,
1525 SMB_STRUCT_STAT sbuf1, sbuf2;
1526 char *oldname = NULL;
1527 char *newname = NULL;
1528 char *last_component_oldname = NULL;
1529 char *last_component_newname = NULL;
1530 files_struct *fsp1,*fsp2;
1534 NTSTATUS status = NT_STATUS_OK;
1539 if (!CAN_WRITE(conn)) {
1540 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1543 status = unix_convert(ctx, conn, oldname_in, False, &oldname,
1544 &last_component_oldname, &sbuf1);
1545 if (!NT_STATUS_IS_OK(status)) {
1549 status = check_name(conn, oldname);
1550 if (!NT_STATUS_IS_OK(status)) {
1554 /* Source must already exist. */
1555 if (!VALID_STAT(sbuf1)) {
1556 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1558 /* Ensure attributes match. */
1559 fattr = dos_mode(conn,oldname,&sbuf1);
1560 if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
1561 return NT_STATUS_NO_SUCH_FILE;
1564 status = unix_convert(ctx, conn, newname_in, False, &newname,
1565 &last_component_newname, &sbuf2);
1566 if (!NT_STATUS_IS_OK(status)) {
1570 status = check_name(conn, newname);
1571 if (!NT_STATUS_IS_OK(status)) {
1575 /* Disallow if newname already exists. */
1576 if (VALID_STAT(sbuf2)) {
1577 return NT_STATUS_OBJECT_NAME_COLLISION;
1580 /* No links from a directory. */
1581 if (S_ISDIR(sbuf1.st_mode)) {
1582 return NT_STATUS_FILE_IS_A_DIRECTORY;
1585 /* Ensure this is within the share. */
1586 status = check_reduced_name(conn, oldname);
1587 if (!NT_STATUS_IS_OK(status)) {
1591 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1594 status = open_file_ntcreate(conn, req, oldname, &sbuf1,
1595 FILE_READ_DATA, /* Read-only. */
1596 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1598 0, /* No create options. */
1599 FILE_ATTRIBUTE_NORMAL,
1603 if (!NT_STATUS_IS_OK(status)) {
1607 status = open_file_ntcreate(conn, req, newname, &sbuf2,
1608 FILE_WRITE_DATA, /* Read-only. */
1609 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1611 0, /* No create options. */
1616 if (!NT_STATUS_IS_OK(status)) {
1617 close_file(fsp1,ERROR_CLOSE);
1621 if (sbuf1.st_size) {
1622 ret = vfs_transfer_file(fsp1, fsp2, sbuf1.st_size);
1626 * As we are opening fsp1 read-only we only expect
1627 * an error on close on fsp2 if we are out of space.
1628 * Thus we don't look at the error return from the
1631 close_file(fsp1,NORMAL_CLOSE);
1633 /* Ensure the modtime is set correctly on the destination file. */
1634 fsp_set_pending_modtime(fsp2, get_mtimespec(&sbuf1));
1636 status = close_file(fsp2,NORMAL_CLOSE);
1638 /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
1639 creates the file. This isn't the correct thing to do in the copy
1641 file_set_dosmode(conn, newname, fattr, &sbuf2,
1642 parent_dirname(newname),false);
1644 if (ret < (SMB_OFF_T)sbuf1.st_size) {
1645 return NT_STATUS_DISK_FULL;
1648 if (!NT_STATUS_IS_OK(status)) {
1649 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1650 nt_errstr(status), oldname, newname));
1655 /****************************************************************************
1656 Reply to a NT rename request.
1657 ****************************************************************************/
1659 void reply_ntrename(connection_struct *conn, struct smb_request *req)
1661 char *oldname = NULL;
1662 char *newname = NULL;
1665 bool src_has_wcard = False;
1666 bool dest_has_wcard = False;
1669 TALLOC_CTX *ctx = talloc_tos();
1671 START_PROFILE(SMBntrename);
1674 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1675 END_PROFILE(SMBntrename);
1679 attrs = SVAL(req->inbuf,smb_vwv0);
1680 rename_type = SVAL(req->inbuf,smb_vwv1);
1682 p = smb_buf(req->inbuf) + 1;
1683 p += srvstr_get_path_wcard(ctx, (char *)req->inbuf, req->flags2, &oldname, p,
1684 0, STR_TERMINATE, &status,
1686 if (!NT_STATUS_IS_OK(status)) {
1687 reply_nterror(req, status);
1688 END_PROFILE(SMBntrename);
1692 if( is_ntfs_stream_name(oldname)) {
1693 /* Can't rename a stream. */
1694 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1695 END_PROFILE(SMBntrename);
1699 if (ms_has_wild(oldname)) {
1700 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1701 END_PROFILE(SMBntrename);
1706 p += srvstr_get_path_wcard(ctx, (char *)req->inbuf, req->flags2, &newname, p,
1707 0, STR_TERMINATE, &status,
1709 if (!NT_STATUS_IS_OK(status)) {
1710 reply_nterror(req, status);
1711 END_PROFILE(SMBntrename);
1715 status = resolve_dfspath(ctx, conn,
1716 req->flags2 & FLAGS2_DFS_PATHNAMES,
1719 if (!NT_STATUS_IS_OK(status)) {
1720 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1721 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1722 ERRSRV, ERRbadpath);
1723 END_PROFILE(SMBntrename);
1726 reply_nterror(req, status);
1727 END_PROFILE(SMBntrename);
1731 status = resolve_dfspath(ctx, conn,
1732 req->flags2 & FLAGS2_DFS_PATHNAMES,
1735 if (!NT_STATUS_IS_OK(status)) {
1736 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1737 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1738 ERRSRV, ERRbadpath);
1739 END_PROFILE(SMBntrename);
1742 reply_nterror(req, status);
1743 END_PROFILE(SMBntrename);
1747 DEBUG(3,("reply_ntrename : %s -> %s\n",oldname,newname));
1749 switch(rename_type) {
1750 case RENAME_FLAG_RENAME:
1751 status = rename_internals(ctx, conn, req, oldname,
1752 newname, attrs, False, src_has_wcard,
1755 case RENAME_FLAG_HARD_LINK:
1756 if (src_has_wcard || dest_has_wcard) {
1758 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1760 status = hardlink_internals(ctx,
1766 case RENAME_FLAG_COPY:
1767 if (src_has_wcard || dest_has_wcard) {
1769 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1771 status = copy_internals(ctx, conn, req, oldname,
1775 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1776 status = NT_STATUS_INVALID_PARAMETER;
1779 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1783 if (!NT_STATUS_IS_OK(status)) {
1784 if (open_was_deferred(req->mid)) {
1785 /* We have re-scheduled this call. */
1786 END_PROFILE(SMBntrename);
1790 reply_nterror(req, status);
1791 END_PROFILE(SMBntrename);
1795 reply_outbuf(req, 0, 0);
1797 END_PROFILE(SMBntrename);
1801 /****************************************************************************
1802 Reply to a notify change - queue the request and
1803 don't allow a directory to be opened.
1804 ****************************************************************************/
1806 static void call_nt_transact_notify_change(connection_struct *conn,
1807 struct smb_request *req,
1811 uint32 parameter_count,
1812 char **ppdata, uint32 data_count,
1813 uint32 max_data_count,
1814 uint32 max_param_count)
1816 uint16 *setup = *ppsetup;
1822 if(setup_count < 6) {
1823 reply_doserror(req, ERRDOS, ERRbadfunc);
1827 fsp = file_fsp(SVAL(setup,4));
1828 filter = IVAL(setup, 0);
1829 recursive = (SVAL(setup, 6) != 0) ? True : False;
1831 DEBUG(3,("call_nt_transact_notify_change\n"));
1834 reply_doserror(req, ERRDOS, ERRbadfid);
1839 char *filter_string;
1841 if (!(filter_string = notify_filter_string(NULL, filter))) {
1842 reply_nterror(req,NT_STATUS_NO_MEMORY);
1846 DEBUG(3,("call_nt_transact_notify_change: notify change "
1847 "called on %s, filter = %s, recursive = %d\n",
1848 fsp->fsp_name, filter_string, recursive));
1850 TALLOC_FREE(filter_string);
1853 if((!fsp->is_directory) || (conn != fsp->conn)) {
1854 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1858 if (fsp->notify == NULL) {
1860 status = change_notify_create(fsp, filter, recursive);
1862 if (!NT_STATUS_IS_OK(status)) {
1863 DEBUG(10, ("change_notify_create returned %s\n",
1864 nt_errstr(status)));
1865 reply_nterror(req, status);
1870 if (fsp->notify->num_changes != 0) {
1873 * We've got changes pending, respond immediately
1877 * TODO: write a torture test to check the filtering behaviour
1881 change_notify_reply(req->inbuf, max_param_count, fsp->notify);
1884 * change_notify_reply() above has independently sent its
1891 * No changes pending, queue the request
1894 status = change_notify_add_request(req->inbuf, max_param_count, filter,
1896 if (!NT_STATUS_IS_OK(status)) {
1897 reply_nterror(req, status);
1902 /****************************************************************************
1903 Reply to an NT transact rename command.
1904 ****************************************************************************/
1906 static void call_nt_transact_rename(connection_struct *conn,
1907 struct smb_request *req,
1908 uint16 **ppsetup, uint32 setup_count,
1909 char **ppparams, uint32 parameter_count,
1910 char **ppdata, uint32 data_count,
1911 uint32 max_data_count)
1913 char *params = *ppparams;
1914 char *new_name = NULL;
1915 files_struct *fsp = NULL;
1916 bool replace_if_exists = False;
1917 bool dest_has_wcard = False;
1919 TALLOC_CTX *ctx = talloc_tos();
1921 if(parameter_count < 5) {
1922 reply_doserror(req, ERRDOS, ERRbadfunc);
1926 fsp = file_fsp(SVAL(params, 0));
1927 replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
1928 if (!check_fsp(conn, req, fsp, ¤t_user)) {
1931 srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
1932 parameter_count - 4,
1933 STR_TERMINATE, &status, &dest_has_wcard);
1934 if (!NT_STATUS_IS_OK(status)) {
1935 reply_nterror(req, status);
1939 status = rename_internals(ctx,
1949 if (!NT_STATUS_IS_OK(status)) {
1950 if (open_was_deferred(req->mid)) {
1951 /* We have re-scheduled this call. */
1954 reply_nterror(req, status);
1959 * Rename was successful.
1961 send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
1963 DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n",
1964 fsp->fsp_name, new_name));
1969 /******************************************************************************
1970 Fake up a completely empty SD.
1971 *******************************************************************************/
1973 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
1977 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1979 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1980 return NT_STATUS_NO_MEMORY;
1983 return NT_STATUS_OK;
1986 /****************************************************************************
1987 Reply to query a security descriptor.
1988 ****************************************************************************/
1990 static void call_nt_transact_query_security_desc(connection_struct *conn,
1991 struct smb_request *req,
1995 uint32 parameter_count,
1998 uint32 max_data_count)
2000 char *params = *ppparams;
2001 char *data = *ppdata;
2003 SEC_DESC *psd = NULL;
2005 uint32 security_info_wanted;
2006 TALLOC_CTX *mem_ctx;
2007 files_struct *fsp = NULL;
2010 if(parameter_count < 8) {
2011 reply_doserror(req, ERRDOS, ERRbadfunc);
2015 fsp = file_fsp(SVAL(params,0));
2017 reply_doserror(req, ERRDOS, ERRbadfid);
2021 security_info_wanted = IVAL(params,4);
2023 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, info_wanted = 0x%x\n", fsp->fsp_name,
2024 (unsigned int)security_info_wanted ));
2026 params = nttrans_realloc(ppparams, 4);
2027 if(params == NULL) {
2028 reply_doserror(req, ERRDOS, ERRnomem);
2032 if ((mem_ctx = talloc_init("call_nt_transact_query_security_desc")) == NULL) {
2033 DEBUG(0,("call_nt_transact_query_security_desc: talloc_init failed.\n"));
2034 reply_doserror(req, ERRDOS, ERRnomem);
2039 * Get the permissions to return.
2042 if (!lp_nt_acl_support(SNUM(conn))) {
2043 status = get_null_nt_acl(mem_ctx, &psd);
2045 status = SMB_VFS_FGET_NT_ACL(fsp, fsp->fh->fd,
2046 security_info_wanted, &psd);
2049 if (!NT_STATUS_IS_OK(status)) {
2050 talloc_destroy(mem_ctx);
2051 reply_nterror(req, status);
2055 sd_size = sec_desc_size(psd);
2057 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
2059 SIVAL(params,0,(uint32)sd_size);
2061 if(max_data_count < sd_size) {
2063 send_nt_replies(req, NT_STATUS_BUFFER_TOO_SMALL,
2064 params, 4, *ppdata, 0);
2065 talloc_destroy(mem_ctx);
2070 * Allocate the data we will point this at.
2073 data = nttrans_realloc(ppdata, sd_size);
2075 talloc_destroy(mem_ctx);
2076 reply_doserror(req, ERRDOS, ERRnomem);
2081 * Init the parse struct we will marshall into.
2084 prs_init(&pd, 0, mem_ctx, MARSHALL);
2087 * Setup the prs_struct to point at the memory we just
2091 prs_give_memory( &pd, data, (uint32)sd_size, False);
2094 * Finally, linearize into the outgoing buffer.
2097 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
2098 DEBUG(0,("call_nt_transact_query_security_desc: Error in marshalling \
2099 security descriptor.\n"));
2101 * Return access denied for want of a better error message..
2103 talloc_destroy(mem_ctx);
2104 reply_unixerror(req, ERRDOS, ERRnoaccess);
2109 * Now we can delete the security descriptor.
2112 talloc_destroy(mem_ctx);
2114 send_nt_replies(req, NT_STATUS_OK, params, 4, data, (int)sd_size);
2118 /****************************************************************************
2119 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
2120 ****************************************************************************/
2122 static void call_nt_transact_set_security_desc(connection_struct *conn,
2123 struct smb_request *req,
2127 uint32 parameter_count,
2130 uint32 max_data_count)
2132 char *params= *ppparams;
2133 char *data = *ppdata;
2134 files_struct *fsp = NULL;
2135 uint32 security_info_sent = 0;
2138 if(parameter_count < 8) {
2139 reply_doserror(req, ERRDOS, ERRbadfunc);
2143 if((fsp = file_fsp(SVAL(params,0))) == NULL) {
2144 reply_doserror(req, ERRDOS, ERRbadfid);
2148 if(!lp_nt_acl_support(SNUM(conn))) {
2152 security_info_sent = IVAL(params,4);
2154 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
2155 (unsigned int)security_info_sent ));
2157 if (data_count == 0) {
2158 reply_doserror(req, ERRDOS, ERRnoaccess);
2162 if (!NT_STATUS_IS_OK(nt_status = set_sd( fsp, data, data_count, security_info_sent))) {
2163 reply_nterror(req, nt_status);
2169 send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
2173 /****************************************************************************
2175 ****************************************************************************/
2177 static void call_nt_transact_ioctl(connection_struct *conn,
2178 struct smb_request *req,
2179 uint16 **ppsetup, uint32 setup_count,
2180 char **ppparams, uint32 parameter_count,
2181 char **ppdata, uint32 data_count,
2182 uint32 max_data_count)
2189 static bool logged_message;
2190 char *pdata = *ppdata;
2192 if (setup_count != 8) {
2193 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
2194 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2198 function = IVAL(*ppsetup, 0);
2199 fidnum = SVAL(*ppsetup, 4);
2200 isFSctl = CVAL(*ppsetup, 6);
2201 compfilter = CVAL(*ppsetup, 7);
2203 DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
2204 function, fidnum, isFSctl, compfilter));
2206 fsp=file_fsp(fidnum);
2207 /* this check is done in each implemented function case for now
2208 because I don't want to break anything... --metze
2209 FSP_BELONGS_CONN(fsp,conn);*/
2212 case FSCTL_SET_SPARSE:
2213 /* pretend this succeeded - tho strictly we should
2214 mark the file sparse (if the local fs supports it)
2215 so we can know if we need to pre-allocate or not */
2217 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
2218 send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
2221 case FSCTL_CREATE_OR_GET_OBJECT_ID:
2223 unsigned char objid[16];
2225 /* This should return the object-id on this file.
2226 * I think I'll make this be the inode+dev. JRA.
2229 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
2231 if (!fsp_belongs_conn(conn, req, fsp, ¤t_user)) {
2236 pdata = nttrans_realloc(ppdata, data_count);
2237 if (pdata == NULL) {
2238 reply_nterror(req, NT_STATUS_NO_MEMORY);
2241 push_file_id_16(pdata, &fsp->file_id);
2242 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
2243 push_file_id_16(pdata+32, &fsp->file_id);
2244 send_nt_replies(req, NT_STATUS_OK, NULL, 0,
2249 case FSCTL_GET_REPARSE_POINT:
2250 /* pretend this fail - my winXP does it like this
2254 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2255 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
2258 case FSCTL_SET_REPARSE_POINT:
2259 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
2263 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2264 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
2267 case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
2270 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
2271 * and return their volume names. If max_data_count is 16, then it is just
2272 * asking for the number of volumes and length of the combined names.
2274 * pdata is the data allocated by our caller, but that uses
2275 * total_data_count (which is 0 in our case) rather than max_data_count.
2276 * Allocate the correct amount and return the pointer to let
2277 * it be deallocated when we return.
2279 SHADOW_COPY_DATA *shadow_data = NULL;
2280 TALLOC_CTX *shadow_mem_ctx = NULL;
2281 bool labels = False;
2282 uint32 labels_data_count = 0;
2286 if (!fsp_belongs_conn(conn, req, fsp, ¤t_user)) {
2290 if (max_data_count < 16) {
2291 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
2293 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2297 if (max_data_count > 16) {
2301 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
2302 if (shadow_mem_ctx == NULL) {
2303 DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
2304 reply_nterror(req, NT_STATUS_NO_MEMORY);
2308 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
2309 if (shadow_data == NULL) {
2310 DEBUG(0,("TALLOC_ZERO() failed!\n"));
2311 talloc_destroy(shadow_mem_ctx);
2312 reply_nterror(req, NT_STATUS_NO_MEMORY);
2316 shadow_data->mem_ctx = shadow_mem_ctx;
2319 * Call the VFS routine to actually do the work.
2321 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
2322 talloc_destroy(shadow_data->mem_ctx);
2323 if (errno == ENOSYS) {
2324 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
2325 conn->connectpath));
2326 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2329 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
2330 conn->connectpath));
2331 reply_nterror(req, NT_STATUS_UNSUCCESSFUL);
2336 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
2341 data_count = 12+labels_data_count+4;
2344 if (max_data_count<data_count) {
2345 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
2346 max_data_count,data_count));
2347 talloc_destroy(shadow_data->mem_ctx);
2348 reply_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
2352 pdata = nttrans_realloc(ppdata, data_count);
2353 if (pdata == NULL) {
2354 talloc_destroy(shadow_data->mem_ctx);
2355 reply_nterror(req, NT_STATUS_NO_MEMORY);
2361 /* num_volumes 4 bytes */
2362 SIVAL(pdata,0,shadow_data->num_volumes);
2365 /* num_labels 4 bytes */
2366 SIVAL(pdata,4,shadow_data->num_volumes);
2369 /* needed_data_count 4 bytes */
2370 SIVAL(pdata,8,labels_data_count);
2374 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
2375 shadow_data->num_volumes,fsp->fsp_name));
2376 if (labels && shadow_data->labels) {
2377 for (i=0;i<shadow_data->num_volumes;i++) {
2378 srvstr_push(pdata, req->flags2,
2379 cur_pdata, shadow_data->labels[i],
2380 2*sizeof(SHADOW_COPY_LABEL),
2381 STR_UNICODE|STR_TERMINATE);
2382 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
2383 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
2387 talloc_destroy(shadow_data->mem_ctx);
2389 send_nt_replies(req, NT_STATUS_OK, NULL, 0,
2395 case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
2397 /* pretend this succeeded -
2399 * we have to send back a list with all files owned by this SID
2401 * but I have to check that --metze
2405 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
2407 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
2409 if (!fsp_belongs_conn(conn, req, fsp, ¤t_user)) {
2413 /* unknown 4 bytes: this is not the length of the sid :-( */
2414 /*unknown = IVAL(pdata,0);*/
2416 sid_parse(pdata+4,sid_len,&sid);
2417 DEBUGADD(10,("for SID: %s\n",sid_string_static(&sid)));
2419 if (!sid_to_uid(&sid, &uid)) {
2420 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2421 sid_string_static(&sid),(unsigned long)sid_len));
2425 /* we can take a look at the find source :-)
2427 * find ./ -uid $uid -name '*' is what we need here
2430 * and send 4bytes len and then NULL terminated unicode strings
2433 * but I don't know how to deal with the paged results
2434 * (maybe we can hang the result anywhere in the fsp struct)
2436 * we don't send all files at once
2437 * and at the next we should *not* start from the beginning,
2438 * so we have to cache the result
2443 /* this works for now... */
2444 send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
2448 if (!logged_message) {
2449 logged_message = True; /* Only print this once... */
2450 DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
2455 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2459 #ifdef HAVE_SYS_QUOTAS
2460 /****************************************************************************
2461 Reply to get user quota
2462 ****************************************************************************/
2464 static void call_nt_transact_get_user_quota(connection_struct *conn,
2465 struct smb_request *req,
2469 uint32 parameter_count,
2472 uint32 max_data_count)
2474 NTSTATUS nt_status = NT_STATUS_OK;
2475 char *params = *ppparams;
2476 char *pdata = *ppdata;
2478 int data_len=0,param_len=0;
2481 files_struct *fsp = NULL;
2485 bool start_enum = True;
2486 SMB_NTQUOTA_STRUCT qt;
2487 SMB_NTQUOTA_LIST *tmp_list;
2488 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2493 if (current_user.ut.uid != 0) {
2494 DEBUG(1,("get_user_quota: access_denied service [%s] user [%s]\n",
2495 lp_servicename(SNUM(conn)),conn->user));
2496 reply_doserror(req, ERRDOS, ERRnoaccess);
2501 * Ensure minimum number of parameters sent.
2504 if (parameter_count < 4) {
2505 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2506 reply_doserror(req, ERRDOS, ERRinvalidparam);
2510 /* maybe we can check the quota_fnum */
2511 fsp = file_fsp(SVAL(params,0));
2512 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2513 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2514 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2518 /* the NULL pointer checking for fsp->fake_file_handle->pd
2519 * is done by CHECK_NTQUOTA_HANDLE_OK()
2521 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->pd;
2523 level = SVAL(params,2);
2525 /* unknown 12 bytes leading in params */
2528 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2529 /* seems that we should continue with the enum here --metze */
2531 if (qt_handle->quota_list!=NULL &&
2532 qt_handle->tmp_list==NULL) {
2535 free_ntquota_list(&(qt_handle->quota_list));
2537 /* Realloc the size of parameters and data we will return */
2539 params = nttrans_realloc(ppparams, param_len);
2540 if(params == NULL) {
2541 reply_doserror(req, ERRDOS, ERRnomem);
2546 SIVAL(params,0,data_len);
2553 case TRANSACT_GET_USER_QUOTA_LIST_START:
2555 if (qt_handle->quota_list==NULL &&
2556 qt_handle->tmp_list==NULL) {
2560 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2561 reply_doserror(req, ERRSRV, ERRerror);
2565 /* Realloc the size of parameters and data we will return */
2567 params = nttrans_realloc(ppparams, param_len);
2568 if(params == NULL) {
2569 reply_doserror(req, ERRDOS, ERRnomem);
2573 /* we should not trust the value in max_data_count*/
2574 max_data_count = MIN(max_data_count,2048);
2576 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2578 reply_doserror(req, ERRDOS, ERRnomem);
2584 /* set params Size of returned Quota Data 4 bytes*/
2585 /* but set it later when we know it */
2587 /* for each entry push the data */
2590 qt_handle->tmp_list = qt_handle->quota_list;
2593 tmp_list = qt_handle->tmp_list;
2595 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2596 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2598 sid_len = sid_size(&tmp_list->quotas->sid);
2599 entry_len = 40 + sid_len;
2601 /* nextoffset entry 4 bytes */
2602 SIVAL(entry,0,entry_len);
2604 /* then the len of the SID 4 bytes */
2605 SIVAL(entry,4,sid_len);
2607 /* unknown data 8 bytes SMB_BIG_UINT */
2608 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-metze*/
2610 /* the used disk space 8 bytes SMB_BIG_UINT */
2611 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2613 /* the soft quotas 8 bytes SMB_BIG_UINT */
2614 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2616 /* the hard quotas 8 bytes SMB_BIG_UINT */
2617 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2619 /* and now the SID */
2620 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2623 qt_handle->tmp_list = tmp_list;
2625 /* overwrite the offset of the last entry */
2626 SIVAL(entry-entry_len,0,0);
2628 data_len = 4+qt_len;
2629 /* overwrite the params quota_data_len */
2630 SIVAL(params,0,data_len);
2634 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2636 /* unknown 4 bytes IVAL(pdata,0) */
2638 if (data_count < 8) {
2639 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2640 reply_doserror(req, ERRDOS, ERRunknownlevel);
2644 sid_len = IVAL(pdata,4);
2645 /* Ensure this is less than 1mb. */
2646 if (sid_len > (1024*1024)) {
2647 reply_doserror(req, ERRDOS, ERRnomem);
2651 if (data_count < 8+sid_len) {
2652 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2653 reply_doserror(req, ERRDOS, ERRunknownlevel);
2657 data_len = 4+40+sid_len;
2659 if (max_data_count < data_len) {
2660 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2661 max_data_count, data_len));
2663 SIVAL(params,0,data_len);
2665 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2669 sid_parse(pdata+8,sid_len,&sid);
2671 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2674 * we have to return zero's in all fields
2675 * instead of returning an error here
2680 /* Realloc the size of parameters and data we will return */
2682 params = nttrans_realloc(ppparams, param_len);
2683 if(params == NULL) {
2684 reply_doserror(req, ERRDOS, ERRnomem);
2688 pdata = nttrans_realloc(ppdata, data_len);
2690 reply_doserror(req, ERRDOS, ERRnomem);
2696 /* set params Size of returned Quota Data 4 bytes*/
2697 SIVAL(params,0,data_len);
2699 /* nextoffset entry 4 bytes */
2702 /* then the len of the SID 4 bytes */
2703 SIVAL(entry,4,sid_len);
2705 /* unknown data 8 bytes SMB_BIG_UINT */
2706 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-mezte*/
2708 /* the used disk space 8 bytes SMB_BIG_UINT */
2709 SBIG_UINT(entry,16,qt.usedspace);
2711 /* the soft quotas 8 bytes SMB_BIG_UINT */
2712 SBIG_UINT(entry,24,qt.softlim);
2714 /* the hard quotas 8 bytes SMB_BIG_UINT */
2715 SBIG_UINT(entry,32,qt.hardlim);
2717 /* and now the SID */
2718 sid_linearize(entry+40, sid_len, &sid);
2723 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2724 reply_doserror(req, ERRSRV, ERRerror);
2729 send_nt_replies(req, nt_status, params, param_len,
2733 /****************************************************************************
2734 Reply to set user quota
2735 ****************************************************************************/
2737 static void call_nt_transact_set_user_quota(connection_struct *conn,
2738 struct smb_request *req,
2742 uint32 parameter_count,
2745 uint32 max_data_count)
2747 char *params = *ppparams;
2748 char *pdata = *ppdata;
2749 int data_len=0,param_len=0;
2750 SMB_NTQUOTA_STRUCT qt;
2753 files_struct *fsp = NULL;
2758 if (current_user.ut.uid != 0) {
2759 DEBUG(1,("set_user_quota: access_denied service [%s] user [%s]\n",
2760 lp_servicename(SNUM(conn)),conn->user));
2761 reply_doserror(req, ERRDOS, ERRnoaccess);
2766 * Ensure minimum number of parameters sent.
2769 if (parameter_count < 2) {
2770 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2771 reply_doserror(req, ERRDOS, ERRinvalidparam);
2775 /* maybe we can check the quota_fnum */
2776 fsp = file_fsp(SVAL(params,0));
2777 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2778 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2779 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2783 if (data_count < 40) {
2784 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2785 reply_doserror(req, ERRDOS, ERRunknownlevel);
2789 /* offset to next quota record.
2790 * 4 bytes IVAL(pdata,0)
2795 sid_len = IVAL(pdata,4);
2797 if (data_count < 40+sid_len) {
2798 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2799 reply_doserror(req, ERRDOS, ERRunknownlevel);
2803 /* unknown 8 bytes in pdata
2804 * maybe its the change time in NTTIME
2807 /* the used space 8 bytes (SMB_BIG_UINT)*/
2808 qt.usedspace = (SMB_BIG_UINT)IVAL(pdata,16);
2809 #ifdef LARGE_SMB_OFF_T
2810 qt.usedspace |= (((SMB_BIG_UINT)IVAL(pdata,20)) << 32);
2811 #else /* LARGE_SMB_OFF_T */
2812 if ((IVAL(pdata,20) != 0)&&
2813 ((qt.usedspace != 0xFFFFFFFF)||
2814 (IVAL(pdata,20)!=0xFFFFFFFF))) {
2815 /* more than 32 bits? */
2816 reply_doserror(req, ERRDOS, ERRunknownlevel);
2819 #endif /* LARGE_SMB_OFF_T */
2821 /* the soft quotas 8 bytes (SMB_BIG_UINT)*/
2822 qt.softlim = (SMB_BIG_UINT)IVAL(pdata,24);
2823 #ifdef LARGE_SMB_OFF_T
2824 qt.softlim |= (((SMB_BIG_UINT)IVAL(pdata,28)) << 32);
2825 #else /* LARGE_SMB_OFF_T */
2826 if ((IVAL(pdata,28) != 0)&&
2827 ((qt.softlim != 0xFFFFFFFF)||
2828 (IVAL(pdata,28)!=0xFFFFFFFF))) {
2829 /* more than 32 bits? */
2830 reply_doserror(req, ERRDOS, ERRunknownlevel);
2833 #endif /* LARGE_SMB_OFF_T */
2835 /* the hard quotas 8 bytes (SMB_BIG_UINT)*/
2836 qt.hardlim = (SMB_BIG_UINT)IVAL(pdata,32);
2837 #ifdef LARGE_SMB_OFF_T
2838 qt.hardlim |= (((SMB_BIG_UINT)IVAL(pdata,36)) << 32);
2839 #else /* LARGE_SMB_OFF_T */
2840 if ((IVAL(pdata,36) != 0)&&
2841 ((qt.hardlim != 0xFFFFFFFF)||
2842 (IVAL(pdata,36)!=0xFFFFFFFF))) {
2843 /* more than 32 bits? */
2844 reply_doserror(req, ERRDOS, ERRunknownlevel);
2847 #endif /* LARGE_SMB_OFF_T */
2849 sid_parse(pdata+40,sid_len,&sid);
2850 DEBUGADD(8,("SID: %s\n",sid_string_static(&sid)));
2852 /* 44 unknown bytes left... */
2854 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2855 reply_doserror(req, ERRSRV, ERRerror);
2859 send_nt_replies(req, NT_STATUS_OK, params, param_len,
2862 #endif /* HAVE_SYS_QUOTAS */
2864 static void handle_nttrans(connection_struct *conn,
2865 struct trans_state *state,
2866 struct smb_request *req)
2868 if (Protocol >= PROTOCOL_NT1) {
2869 req->flags2 |= 0x40; /* IS_LONG_NAME */
2870 SSVAL(req->inbuf,smb_flg2,req->flags2);
2873 /* Now we must call the relevant NT_TRANS function */
2874 switch(state->call) {
2875 case NT_TRANSACT_CREATE:
2877 START_PROFILE(NT_transact_create);
2878 call_nt_transact_create(
2880 &state->setup, state->setup_count,
2881 &state->param, state->total_param,
2882 &state->data, state->total_data,
2883 state->max_data_return);
2884 END_PROFILE(NT_transact_create);
2888 case NT_TRANSACT_IOCTL:
2890 START_PROFILE(NT_transact_ioctl);
2891 call_nt_transact_ioctl(
2893 &state->setup, state->setup_count,
2894 &state->param, state->total_param,
2895 &state->data, state->total_data,
2896 state->max_data_return);
2897 END_PROFILE(NT_transact_ioctl);
2901 case NT_TRANSACT_SET_SECURITY_DESC:
2903 START_PROFILE(NT_transact_set_security_desc);
2904 call_nt_transact_set_security_desc(
2906 &state->setup, state->setup_count,
2907 &state->param, state->total_param,
2908 &state->data, state->total_data,
2909 state->max_data_return);
2910 END_PROFILE(NT_transact_set_security_desc);
2914 case NT_TRANSACT_NOTIFY_CHANGE:
2916 START_PROFILE(NT_transact_notify_change);
2917 call_nt_transact_notify_change(
2919 &state->setup, state->setup_count,
2920 &state->param, state->total_param,
2921 &state->data, state->total_data,
2922 state->max_data_return,
2923 state->max_param_return);
2924 END_PROFILE(NT_transact_notify_change);
2928 case NT_TRANSACT_RENAME:
2930 START_PROFILE(NT_transact_rename);
2931 call_nt_transact_rename(
2933 &state->setup, state->setup_count,
2934 &state->param, state->total_param,
2935 &state->data, state->total_data,
2936 state->max_data_return);
2937 END_PROFILE(NT_transact_rename);
2941 case NT_TRANSACT_QUERY_SECURITY_DESC:
2943 START_PROFILE(NT_transact_query_security_desc);
2944 call_nt_transact_query_security_desc(
2946 &state->setup, state->setup_count,
2947 &state->param, state->total_param,
2948 &state->data, state->total_data,
2949 state->max_data_return);
2950 END_PROFILE(NT_transact_query_security_desc);
2954 #ifdef HAVE_SYS_QUOTAS
2955 case NT_TRANSACT_GET_USER_QUOTA:
2957 START_PROFILE(NT_transact_get_user_quota);
2958 call_nt_transact_get_user_quota(
2960 &state->setup, state->setup_count,
2961 &state->param, state->total_param,
2962 &state->data, state->total_data,
2963 state->max_data_return);
2964 END_PROFILE(NT_transact_get_user_quota);
2968 case NT_TRANSACT_SET_USER_QUOTA:
2970 START_PROFILE(NT_transact_set_user_quota);
2971 call_nt_transact_set_user_quota(
2973 &state->setup, state->setup_count,
2974 &state->param, state->total_param,
2975 &state->data, state->total_data,
2976 state->max_data_return);
2977 END_PROFILE(NT_transact_set_user_quota);
2980 #endif /* HAVE_SYS_QUOTAS */
2983 /* Error in request */
2984 DEBUG(0,("handle_nttrans: Unknown request %d in "
2985 "nttrans call\n", state->call));
2986 reply_doserror(req, ERRSRV, ERRerror);
2992 /****************************************************************************
2993 Reply to a SMBNTtrans.
2994 ****************************************************************************/
2996 void reply_nttrans(connection_struct *conn, struct smb_request *req)
3002 uint16 function_code;
3004 struct trans_state *state;
3007 START_PROFILE(SMBnttrans);
3009 if (req->wct < 19) {
3010 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3011 END_PROFILE(SMBnttrans);
3015 size = smb_len(req->inbuf) + 4;
3016 pscnt = IVAL(req->inbuf,smb_nt_ParameterCount);
3017 psoff = IVAL(req->inbuf,smb_nt_ParameterOffset);
3018 dscnt = IVAL(req->inbuf,smb_nt_DataCount);
3019 dsoff = IVAL(req->inbuf,smb_nt_DataOffset);
3020 function_code = SVAL(req->inbuf, smb_nt_Function);
3022 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
3023 reply_doserror(req, ERRSRV, ERRaccess);
3024 END_PROFILE(SMBnttrans);
3028 result = allow_new_trans(conn->pending_trans, req->mid);
3029 if (!NT_STATUS_IS_OK(result)) {
3030 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
3031 reply_nterror(req, result);
3032 END_PROFILE(SMBnttrans);
3036 if ((state = TALLOC_P(conn->mem_ctx, struct trans_state)) == NULL) {
3037 reply_doserror(req, ERRSRV, ERRaccess);
3038 END_PROFILE(SMBnttrans);
3042 state->cmd = SMBnttrans;
3044 state->mid = req->mid;
3045 state->vuid = req->vuid;
3046 state->total_data = IVAL(req->inbuf, smb_nt_TotalDataCount);
3048 state->total_param = IVAL(req->inbuf, smb_nt_TotalParameterCount);
3049 state->param = NULL;
3050 state->max_data_return = IVAL(req->inbuf,smb_nt_MaxDataCount);
3051 state->max_param_return = IVAL(req->inbuf,smb_nt_MaxParameterCount);
3053 /* setup count is in *words* */
3054 state->setup_count = 2*CVAL(req->inbuf,smb_nt_SetupCount);
3055 state->setup = NULL;
3056 state->call = function_code;
3059 * All nttrans messages we handle have smb_wct == 19 +
3060 * state->setup_count. Ensure this is so as a sanity check.
3063 if(req->wct != 19 + (state->setup_count/2)) {
3064 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
3065 req->wct, 19 + (state->setup_count/2)));
3069 /* Don't allow more than 128mb for each value. */
3070 if ((state->total_data > (1024*1024*128)) ||
3071 (state->total_param > (1024*1024*128))) {
3072 reply_doserror(req, ERRDOS, ERRnomem);
3073 END_PROFILE(SMBnttrans);
3077 if ((dscnt > state->total_data) || (pscnt > state->total_param))
3080 if (state->total_data) {
3081 /* Can't use talloc here, the core routines do realloc on the
3082 * params and data. */
3083 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
3084 DEBUG(0,("reply_nttrans: data malloc fail for %u "
3085 "bytes !\n", (unsigned int)state->total_data));
3087 reply_doserror(req, ERRDOS, ERRnomem);
3088 END_PROFILE(SMBnttrans);
3091 if ((dsoff+dscnt < dsoff) || (dsoff+dscnt < dscnt))
3093 if ((smb_base(req->inbuf)+dsoff+dscnt
3094 > (char *)req->inbuf + size) ||
3095 (smb_base(req->inbuf)+dsoff+dscnt < smb_base(req->inbuf)))
3098 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
3101 if (state->total_param) {
3102 /* Can't use talloc here, the core routines do realloc on the
3103 * params and data. */
3104 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
3105 DEBUG(0,("reply_nttrans: param malloc fail for %u "
3106 "bytes !\n", (unsigned int)state->total_param));
3107 SAFE_FREE(state->data);
3109 reply_doserror(req, ERRDOS, ERRnomem);
3110 END_PROFILE(SMBnttrans);
3113 if ((psoff+pscnt < psoff) || (psoff+pscnt < pscnt))
3115 if ((smb_base(req->inbuf)+psoff+pscnt
3116 > (char *)req->inbuf + size) ||
3117 (smb_base(req->inbuf)+psoff+pscnt < smb_base(req->inbuf)))
3120 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
3123 state->received_data = dscnt;
3124 state->received_param = pscnt;
3126 if(state->setup_count > 0) {
3127 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
3128 state->setup_count));
3129 state->setup = (uint16 *)TALLOC(state, state->setup_count);
3130 if (state->setup == NULL) {
3131 DEBUG(0,("reply_nttrans : Out of memory\n"));
3132 SAFE_FREE(state->data);
3133 SAFE_FREE(state->param);
3135 reply_doserror(req, ERRDOS, ERRnomem);
3136 END_PROFILE(SMBnttrans);
3140 if ((smb_nt_SetupStart + state->setup_count < smb_nt_SetupStart) ||
3141 (smb_nt_SetupStart + state->setup_count < state->setup_count)) {
3144 if (smb_nt_SetupStart + state->setup_count > size) {
3148 memcpy( state->setup, &req->inbuf[smb_nt_SetupStart],
3149 state->setup_count);
3150 dump_data(10, (uint8 *)state->setup, state->setup_count);
3153 if ((state->received_data == state->total_data) &&
3154 (state->received_param == state->total_param)) {
3155 handle_nttrans(conn, state, req);
3156 SAFE_FREE(state->param);
3157 SAFE_FREE(state->data);
3159 END_PROFILE(SMBnttrans);
3163 DLIST_ADD(conn->pending_trans, state);
3165 /* We need to send an interim response then receive the rest
3166 of the parameter/data bytes */
3167 reply_outbuf(req, 0, 0);
3168 show_msg((char *)req->outbuf);
3169 END_PROFILE(SMBnttrans);
3174 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
3175 SAFE_FREE(state->data);
3176 SAFE_FREE(state->param);
3178 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3179 END_PROFILE(SMBnttrans);
3183 /****************************************************************************
3184 Reply to a SMBnttranss
3185 ****************************************************************************/
3187 void reply_nttranss(connection_struct *conn, struct smb_request *req)
3189 unsigned int pcnt,poff,dcnt,doff,pdisp,ddisp;
3190 struct trans_state *state;
3194 START_PROFILE(SMBnttranss);
3196 show_msg((char *)req->inbuf);
3198 if (req->wct < 18) {
3199 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3200 END_PROFILE(SMBnttranss);
3204 for (state = conn->pending_trans; state != NULL;
3205 state = state->next) {
3206 if (state->mid == req->mid) {
3211 if ((state == NULL) || (state->cmd != SMBnttrans)) {
3212 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3213 END_PROFILE(SMBnttranss);
3217 /* Revise state->total_param and state->total_data in case they have
3218 changed downwards */
3219 if (IVAL(req->inbuf, smb_nts_TotalParameterCount)
3220 < state->total_param) {
3221 state->total_param = IVAL(req->inbuf,
3222 smb_nts_TotalParameterCount);
3224 if (IVAL(req->inbuf, smb_nts_TotalDataCount) < state->total_data) {
3225 state->total_data = IVAL(req->inbuf, smb_nts_TotalDataCount);
3228 size = smb_len(req->inbuf) + 4;
3230 pcnt = IVAL(req->inbuf,smb_nts_ParameterCount);
3231 poff = IVAL(req->inbuf, smb_nts_ParameterOffset);
3232 pdisp = IVAL(req->inbuf, smb_nts_ParameterDisplacement);
3234 dcnt = IVAL(req->inbuf, smb_nts_DataCount);
3235 ddisp = IVAL(req->inbuf, smb_nts_DataDisplacement);
3236 doff = IVAL(req->inbuf, smb_nts_DataOffset);
3238 state->received_param += pcnt;
3239 state->received_data += dcnt;
3241 if ((state->received_data > state->total_data) ||
3242 (state->received_param > state->total_param))
3246 if (pdisp+pcnt > state->total_param)
3248 if ((pdisp+pcnt < pdisp) || (pdisp+pcnt < pcnt))
3250 if (pdisp > state->total_param)
3252 if ((smb_base(req->inbuf) + poff + pcnt
3253 > (char *)req->inbuf + size) ||
3254 (smb_base(req->inbuf) + poff + pcnt
3255 < smb_base(req->inbuf)))
3257 if (state->param + pdisp < state->param)
3260 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,
3265 if (ddisp+dcnt > state->total_data)
3267 if ((ddisp+dcnt < ddisp) || (ddisp+dcnt < dcnt))
3269 if (ddisp > state->total_data)
3271 if ((smb_base(req->inbuf) + doff + dcnt
3272 > (char *)req->inbuf + size) ||
3273 (smb_base(req->inbuf) + doff + dcnt
3274 < smb_base(req->inbuf)))
3276 if (state->data + ddisp < state->data)
3279 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,
3283 if ((state->received_param < state->total_param) ||
3284 (state->received_data < state->total_data)) {
3285 END_PROFILE(SMBnttranss);
3290 * construct_reply_common will copy smb_com from inbuf to
3291 * outbuf. SMBnttranss is wrong here.
3293 SCVAL(req->inbuf,smb_com,SMBnttrans);
3295 handle_nttrans(conn, state, req);
3297 DLIST_REMOVE(conn->pending_trans, state);
3298 SAFE_FREE(state->data);
3299 SAFE_FREE(state->param);
3301 END_PROFILE(SMBnttranss);
3306 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
3307 DLIST_REMOVE(conn->pending_trans, state);
3308 SAFE_FREE(state->data);
3309 SAFE_FREE(state->param);
3311 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3312 END_PROFILE(SMBnttranss);