smbd: Remove unused [push_pull]_file_id_24
[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
20
21 #define MODULE "crossrename"
22 static SMB_OFF_T module_sizelimit;
23
24 static int crossrename_connect(
25                 struct vfs_handle_struct *  handle,
26                 const char *                service,
27                 const char *                user)
28 {
29         int ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
30
31         if (ret < 0) {
32                 return ret;
33         }
34
35         module_sizelimit = (SMB_OFF_T) lp_parm_int(SNUM(handle->conn),
36                                         MODULE, "sizelimit", 20);
37         /* convert from MiB to byte: */
38         module_sizelimit *= 1048576;
39
40         return 0;
41 }
42
43 /*********************************************************
44  For rename across filesystems initial Patch from Warren Birnbaum
45  <warrenb@hpcvscdp.cv.hp.com>
46 **********************************************************/
47
48 static int copy_reg(const char *source, const char *dest)
49 {
50         SMB_STRUCT_STAT source_stats;
51         int saved_errno;
52         int ifd = -1;
53         int ofd = -1;
54
55         if (sys_lstat(source, &source_stats, false) == -1)
56                 return -1;
57
58         if (!S_ISREG (source_stats.st_ex_mode))
59                 return -1;
60
61         if (source_stats.st_ex_size > module_sizelimit) {
62                 DEBUG(5,
63                         ("%s: size of %s larger than sizelimit (%lld > %lld), rename prohititted\n",
64                         MODULE, source,
65                         (long long)source_stats.st_ex_size,
66                         (long long)module_sizelimit));
67                 return -1;
68         }
69
70         if((ifd = sys_open (source, O_RDONLY, 0)) < 0)
71                 return -1;
72
73         if (unlink (dest) && errno != ENOENT)
74                 return -1;
75
76 #ifdef O_NOFOLLOW
77         if((ofd = sys_open (dest, O_WRONLY | O_CREAT | O_TRUNC | O_NOFOLLOW, 0600)) < 0 )
78 #else
79         if((ofd = sys_open (dest, O_WRONLY | O_CREAT | O_TRUNC , 0600)) < 0 )
80 #endif
81                 goto err;
82
83         if (transfer_file(ifd, ofd, (size_t)-1) == -1)
84                 goto err;
85
86         /*
87          * Try to preserve ownership.  For non-root it might fail, but that's ok.
88          * But root probably wants to know, e.g. if NFS disallows it.
89          */
90
91 #ifdef HAVE_FCHOWN
92         if ((fchown(ofd, source_stats.st_ex_uid, source_stats.st_ex_gid) == -1) && (errno != EPERM))
93 #else
94         if ((chown(dest, source_stats.st_ex_uid, source_stats.st_ex_gid) == -1) && (errno != EPERM))
95 #endif
96                 goto err;
97
98         /*
99          * fchown turns off set[ug]id bits for non-root,
100          * so do the chmod last.
101          */
102
103 #if defined(HAVE_FCHMOD)
104         if (fchmod (ofd, source_stats.st_ex_mode & 07777))
105 #else
106         if (chmod (dest, source_stats.st_ex_mode & 07777))
107 #endif
108                 goto err;
109
110         if (close (ifd) == -1)
111                 goto err;
112
113         if (close (ofd) == -1)
114                 return -1;
115
116         /* Try to copy the old file's modtime and access time.  */
117 #if defined(HAVE_UTIMENSAT)
118         {
119                 struct timespec ts[2];
120
121                 ts[0] = source_stats.st_ex_atime;
122                 ts[1] = source_stats.st_ex_mtime;
123                 utimensat(AT_FDCWD, dest, ts, AT_SYMLINK_NOFOLLOW);
124         }
125 #elif defined(HAVE_UTIMES)
126         {
127                 struct timeval tv[2];
128
129                 tv[0] = convert_timespec_to_timeval(source_stats.st_ex_atime);
130                 tv[1] = convert_timespec_to_timeval(source_stats.st_ex_mtime);
131 #ifdef HAVE_LUTIMES
132                 lutimes(dest, tv);
133 #else
134                 utimes(dest, tv);
135 #endif
136         }
137 #elif defined(HAVE_UTIME)
138         {
139                 struct utimbuf tv;
140
141                 tv.actime = convert_timespec_to_time_t(source_stats.st_ex_atime);
142                 tv.modtime = convert_timespec_to_time_t(source_stats.st_ex_mtime);
143                 utime(dest, &tv);
144         }
145 #endif
146
147         if (unlink (source) == -1)
148                 return -1;
149
150         return 0;
151
152   err:
153
154         saved_errno = errno;
155         if (ifd != -1)
156                 close(ifd);
157         if (ofd != -1)
158                 close(ofd);
159         errno = saved_errno;
160         return -1;
161 }
162
163
164 static int crossrename_rename(vfs_handle_struct *handle,
165                           const struct smb_filename *smb_fname_src,
166                           const struct smb_filename *smb_fname_dst)
167 {
168         int result = -1;
169
170         START_PROFILE(syscall_rename);
171
172         if (smb_fname_src->stream_name || smb_fname_dst->stream_name) {
173                 errno = ENOENT;
174                 goto out;
175         }
176
177         result = rename(smb_fname_src->base_name, smb_fname_dst->base_name);
178         if ((result == -1) && (errno == EXDEV)) {
179                 /* Rename across filesystems needed. */
180                 result = copy_reg(smb_fname_src->base_name,
181                                   smb_fname_dst->base_name);
182         }
183
184  out:
185         END_PROFILE(syscall_rename);
186         return result;
187 }
188
189 static struct vfs_fn_pointers vfs_crossrename_fns = {
190         .connect_fn = crossrename_connect,
191         .rename = crossrename_rename
192 };
193
194 NTSTATUS vfs_crossrename_init(void);
195 NTSTATUS vfs_crossrename_init(void)
196 {
197         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, MODULE,
198                                 &vfs_crossrename_fns);
199 }
200