s3: VFS: vfs_crossrename. Implement renameat().
[samba.git] / source3 / modules / vfs_crossrename.c
1 /*
2  * Copyright (c) Björn Jacke 2010
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 #include "includes.h"
19 #include "smbd/smbd.h"
20 #include "system/filesys.h"
21 #include "transfer_file.h"
22 #include "smbprofile.h"
23
24 #define MODULE "crossrename"
25 static off_t module_sizelimit;
26
27 static int crossrename_connect(
28                 struct vfs_handle_struct *  handle,
29                 const char *                service,
30                 const char *                user)
31 {
32         int ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
33
34         if (ret < 0) {
35                 return ret;
36         }
37
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;
42
43         return 0;
44 }
45
46 /*********************************************************
47  For rename across filesystems initial Patch from Warren Birnbaum
48  <warrenb@hpcvscdp.cv.hp.com>
49 **********************************************************/
50
51 static int copy_reg(const char *source, const char *dest)
52 {
53         SMB_STRUCT_STAT source_stats;
54         int saved_errno;
55         int ifd = -1;
56         int ofd = -1;
57
58         if (sys_lstat(source, &source_stats, false) == -1)
59                 return -1;
60
61         if (!S_ISREG (source_stats.st_ex_mode))
62                 return -1;
63
64         if (source_stats.st_ex_size > module_sizelimit) {
65                 DEBUG(5,
66                         ("%s: size of %s larger than sizelimit (%lld > %lld), rename prohititted\n",
67                         MODULE, source,
68                         (long long)source_stats.st_ex_size,
69                         (long long)module_sizelimit));
70                 return -1;
71         }
72
73         if((ifd = open (source, O_RDONLY, 0)) < 0)
74                 return -1;
75
76         if (unlink (dest) && errno != ENOENT) {
77                 close(ifd);
78                 return -1;
79         }
80
81 #ifdef O_NOFOLLOW
82         if((ofd = open (dest, O_WRONLY | O_CREAT | O_TRUNC | O_NOFOLLOW, 0600)) < 0 )
83 #else
84         if((ofd = open (dest, O_WRONLY | O_CREAT | O_TRUNC , 0600)) < 0 )
85 #endif
86                 goto err;
87
88         if (transfer_file(ifd, ofd, source_stats.st_ex_size) == -1)
89                 goto err;
90
91         /*
92          * Try to preserve ownership.  For non-root it might fail, but that's ok.
93          * But root probably wants to know, e.g. if NFS disallows it.
94          */
95
96 #ifdef HAVE_FCHOWN
97         if ((fchown(ofd, source_stats.st_ex_uid, source_stats.st_ex_gid) == -1) && (errno != EPERM))
98 #else
99         if ((chown(dest, source_stats.st_ex_uid, source_stats.st_ex_gid) == -1) && (errno != EPERM))
100 #endif
101                 goto err;
102
103         /*
104          * fchown turns off set[ug]id bits for non-root,
105          * so do the chmod last.
106          */
107
108 #if defined(HAVE_FCHMOD)
109         if ((fchmod (ofd, source_stats.st_ex_mode & 07777) == -1) &&
110                         (errno != EPERM))
111 #else
112         if ((chmod (dest, source_stats.st_ex_mode & 07777) == -1) &&
113                         (errno != EPERM))
114 #endif
115                 goto err;
116
117         if (close (ifd) == -1)
118                 goto err;
119
120         if (close (ofd) == -1)
121                 return -1;
122
123         /* Try to copy the old file's modtime and access time.  */
124 #if defined(HAVE_UTIMENSAT)
125         {
126                 struct timespec ts[2];
127
128                 ts[0] = source_stats.st_ex_atime;
129                 ts[1] = source_stats.st_ex_mtime;
130                 utimensat(AT_FDCWD, dest, ts, AT_SYMLINK_NOFOLLOW);
131         }
132 #elif defined(HAVE_UTIMES)
133         {
134                 struct timeval tv[2];
135
136                 tv[0] = convert_timespec_to_timeval(source_stats.st_ex_atime);
137                 tv[1] = convert_timespec_to_timeval(source_stats.st_ex_mtime);
138 #ifdef HAVE_LUTIMES
139                 lutimes(dest, tv);
140 #else
141                 utimes(dest, tv);
142 #endif
143         }
144 #elif defined(HAVE_UTIME)
145         {
146                 struct utimbuf tv;
147
148                 tv.actime = convert_timespec_to_time_t(source_stats.st_ex_atime);
149                 tv.modtime = convert_timespec_to_time_t(source_stats.st_ex_mtime);
150                 utime(dest, &tv);
151         }
152 #endif
153
154         if (unlink (source) == -1)
155                 return -1;
156
157         return 0;
158
159   err:
160
161         saved_errno = errno;
162         if (ifd != -1)
163                 close(ifd);
164         if (ofd != -1)
165                 close(ofd);
166         errno = saved_errno;
167         return -1;
168 }
169
170
171 static int crossrename_rename(vfs_handle_struct *handle,
172                           const struct smb_filename *smb_fname_src,
173                           const struct smb_filename *smb_fname_dst)
174 {
175         int result = -1;
176
177         START_PROFILE(syscall_rename);
178
179         if (smb_fname_src->stream_name || smb_fname_dst->stream_name) {
180                 errno = ENOENT;
181                 goto out;
182         }
183
184         result = rename(smb_fname_src->base_name, smb_fname_dst->base_name);
185         if ((result == -1) && (errno == EXDEV)) {
186                 /* Rename across filesystems needed. */
187                 result = copy_reg(smb_fname_src->base_name,
188                                   smb_fname_dst->base_name);
189         }
190
191  out:
192         END_PROFILE(syscall_rename);
193         return result;
194 }
195
196 static int crossrename_renameat(vfs_handle_struct *handle,
197                         files_struct *srcfsp,
198                         const struct smb_filename *smb_fname_src,
199                         files_struct *dstfsp,
200                         const struct smb_filename *smb_fname_dst)
201 {
202         int result = -1;
203
204         START_PROFILE(syscall_renameat);
205
206         if (smb_fname_src->stream_name || smb_fname_dst->stream_name) {
207                 errno = ENOENT;
208                 goto out;
209         }
210
211         result = rename(smb_fname_src->base_name, smb_fname_dst->base_name);
212         if ((result == -1) && (errno == EXDEV)) {
213                 /* Rename across filesystems needed. */
214                 result = copy_reg(smb_fname_src->base_name,
215                                   smb_fname_dst->base_name);
216         }
217
218  out:
219         END_PROFILE(syscall_renameat);
220         return result;
221 }
222
223
224 static struct vfs_fn_pointers vfs_crossrename_fns = {
225         .connect_fn = crossrename_connect,
226         .rename_fn = crossrename_rename,
227         .renameat_fn = crossrename_renameat
228 };
229
230 static_decl_vfs;
231 NTSTATUS vfs_crossrename_init(TALLOC_CTX *ctx)
232 {
233         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, MODULE,
234                                 &vfs_crossrename_fns);
235 }
236