smbd: reject FILE_ATTRIBUTE_TEMPORARY on directories
[Samba.git] / source3 / modules / vfs_crossrename.c
blob042144bfc4df9264a0f968fd9ca1a5c413316bf6
1 /*
2 * Copyright (c) Björn Jacke 2010
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 #include "includes.h"
19 #include "smbd/smbd.h"
20 #include "system/filesys.h"
21 #include "transfer_file.h"
22 #include "smbprofile.h"
24 #define MODULE "crossrename"
25 static off_t module_sizelimit;
27 static int crossrename_connect(
28 struct vfs_handle_struct * handle,
29 const char * service,
30 const char * user)
32 int ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
34 if (ret < 0) {
35 return ret;
38 module_sizelimit = (off_t) lp_parm_int(SNUM(handle->conn),
39 MODULE, "sizelimit", 20);
40 /* convert from MiB to byte: */
41 module_sizelimit *= 1048576;
43 return 0;
46 /*********************************************************
47 For rename across filesystems initial Patch from Warren Birnbaum
48 <warrenb@hpcvscdp.cv.hp.com>
49 **********************************************************/
51 static NTSTATUS copy_reg(vfs_handle_struct *handle,
52 struct files_struct *srcfsp,
53 const struct smb_filename *source,
54 struct files_struct *dstfsp,
55 const struct smb_filename *dest)
57 NTSTATUS status;
58 struct smb_filename *full_fname_src = NULL;
59 struct smb_filename *full_fname_dst = NULL;
60 int ret;
62 if (!VALID_STAT(source->st)) {
63 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
64 goto out;
66 if (!S_ISREG(source->st.st_ex_mode)) {
67 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
68 goto out;
71 if (source->st.st_ex_size > module_sizelimit) {
72 DBG_INFO("%s: size of %s larger than sizelimit (%lld > %lld), "
73 "rename prohibited\n",
74 MODULE,
75 source->base_name,
76 (long long)source->st.st_ex_size,
77 (long long)module_sizelimit);
78 status = NT_STATUS_INVALID_PARAMETER;
79 goto out;
82 full_fname_src = full_path_from_dirfsp_atname(talloc_tos(),
83 srcfsp,
84 source);
85 if (full_fname_src == NULL) {
86 status = NT_STATUS_NO_MEMORY;
87 goto out;
89 full_fname_dst = full_path_from_dirfsp_atname(talloc_tos(),
90 dstfsp,
91 dest);
92 if (full_fname_dst == NULL) {
93 status = NT_STATUS_NO_MEMORY;
94 goto out;
97 ret = SMB_VFS_NEXT_UNLINKAT(handle,
98 dstfsp,
99 dest,
101 if (ret == -1) {
102 status = map_nt_error_from_unix(errno);
103 goto out;
107 * copy_internals() takes attribute values from the NTrename call.
109 * From MS-CIFS:
111 * "If the attribute is 0x0000, then only normal files are renamed.
112 * If the system file or hidden attributes are specified, then the
113 * rename is inclusive of both special types."
115 status = copy_internals(talloc_tos(),
116 handle->conn,
117 NULL,
118 srcfsp, /* src_dirfsp */
119 full_fname_src,
120 dstfsp, /* dst_dirfsp */
121 full_fname_dst,
122 FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM);
123 if (!NT_STATUS_IS_OK(status)) {
124 goto out;
127 ret = SMB_VFS_NEXT_UNLINKAT(handle,
128 srcfsp,
129 source,
131 if (ret == -1) {
132 status = map_nt_error_from_unix(errno);
133 goto out;
136 out:
138 TALLOC_FREE(full_fname_src);
139 TALLOC_FREE(full_fname_dst);
140 return status;
143 static int crossrename_renameat(vfs_handle_struct *handle,
144 files_struct *srcfsp,
145 const struct smb_filename *smb_fname_src,
146 files_struct *dstfsp,
147 const struct smb_filename *smb_fname_dst)
149 int result = -1;
151 START_PROFILE(syscall_renameat);
153 if (smb_fname_src->stream_name || smb_fname_dst->stream_name) {
154 errno = ENOENT;
155 goto out;
158 result = SMB_VFS_NEXT_RENAMEAT(handle,
159 srcfsp,
160 smb_fname_src,
161 dstfsp,
162 smb_fname_dst);
164 if ((result == -1) && (errno == EXDEV)) {
165 /* Rename across filesystems needed. */
166 NTSTATUS status = copy_reg(handle,
167 srcfsp,
168 smb_fname_src,
169 dstfsp,
170 smb_fname_dst);
171 if (!NT_STATUS_IS_OK(status)) {
172 errno = map_errno_from_nt_status(status);
173 result = -1;
177 out:
178 END_PROFILE(syscall_renameat);
179 return result;
183 static struct vfs_fn_pointers vfs_crossrename_fns = {
184 .connect_fn = crossrename_connect,
185 .renameat_fn = crossrename_renameat
188 static_decl_vfs;
189 NTSTATUS vfs_crossrename_init(TALLOC_CTX *ctx)
191 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, MODULE,
192 &vfs_crossrename_fns);