| 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 SMB_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 = (SMB_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 | <[email protected]>
|
|---|
| 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 = sys_open (source, O_RDONLY, 0)) < 0)
|
|---|
| 74 | return -1;
|
|---|
| 75 |
|
|---|
| 76 | if (unlink (dest) && errno != ENOENT)
|
|---|
| 77 | return -1;
|
|---|
| 78 |
|
|---|
| 79 | #ifdef O_NOFOLLOW
|
|---|
| 80 | if((ofd = sys_open (dest, O_WRONLY | O_CREAT | O_TRUNC | O_NOFOLLOW, 0600)) < 0 )
|
|---|
| 81 | #else
|
|---|
| 82 | if((ofd = sys_open (dest, O_WRONLY | O_CREAT | O_TRUNC , 0600)) < 0 )
|
|---|
| 83 | #endif
|
|---|
| 84 | goto err;
|
|---|
| 85 |
|
|---|
| 86 | if (transfer_file(ifd, ofd, source_stats.st_ex_size) == -1)
|
|---|
| 87 | goto err;
|
|---|
| 88 |
|
|---|
| 89 | /*
|
|---|
| 90 | * Try to preserve ownership. For non-root it might fail, but that's ok.
|
|---|
| 91 | * But root probably wants to know, e.g. if NFS disallows it.
|
|---|
| 92 | */
|
|---|
| 93 |
|
|---|
| 94 | #ifdef HAVE_FCHOWN
|
|---|
| 95 | if ((fchown(ofd, source_stats.st_ex_uid, source_stats.st_ex_gid) == -1) && (errno != EPERM))
|
|---|
| 96 | #else
|
|---|
| 97 | if ((chown(dest, source_stats.st_ex_uid, source_stats.st_ex_gid) == -1) && (errno != EPERM))
|
|---|
| 98 | #endif
|
|---|
| 99 | goto err;
|
|---|
| 100 |
|
|---|
| 101 | /*
|
|---|
| 102 | * fchown turns off set[ug]id bits for non-root,
|
|---|
| 103 | * so do the chmod last.
|
|---|
| 104 | */
|
|---|
| 105 |
|
|---|
| 106 | #if defined(HAVE_FCHMOD)
|
|---|
| 107 | if (fchmod (ofd, source_stats.st_ex_mode & 07777))
|
|---|
| 108 | #else
|
|---|
| 109 | if (chmod (dest, source_stats.st_ex_mode & 07777))
|
|---|
| 110 | #endif
|
|---|
| 111 | goto err;
|
|---|
| 112 |
|
|---|
| 113 | if (close (ifd) == -1)
|
|---|
| 114 | goto err;
|
|---|
| 115 |
|
|---|
| 116 | if (close (ofd) == -1)
|
|---|
| 117 | return -1;
|
|---|
| 118 |
|
|---|
| 119 | /* Try to copy the old file's modtime and access time. */
|
|---|
| 120 | #if defined(HAVE_UTIMENSAT)
|
|---|
| 121 | {
|
|---|
|
|---|