| 1 | /* |
| 2 | * Syscall wrappers to ensure that nothing gets done in dry_run mode |
| 3 | * and to handle system peculiarities. |
| 4 | * |
| 5 | * Copyright (C) 1998 Andrew Tridgell |
| 6 | * Copyright (C) 2002 Martin Pool |
| 7 | * Copyright (C) 2003-2009 Wayne Davison |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 3 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, visit the http://fsf.org website. |
| 21 | */ |
| 22 | |
| 23 | #include "rsync.h" |
| 24 | |
| 25 | #if !defined MKNOD_CREATES_SOCKETS && defined HAVE_SYS_UN_H |
| 26 | #include <sys/un.h> |
| 27 | #endif |
| 28 | #ifdef HAVE_SYS_ATTR_H |
| 29 | #include <sys/attr.h> |
| 30 | #endif |
| 31 | |
| 32 | extern int dry_run; |
| 33 | extern int am_root; |
| 34 | extern int am_sender; |
| 35 | extern int read_only; |
| 36 | extern int list_only; |
| 37 | extern int preserve_perms; |
| 38 | extern int preserve_executability; |
| 39 | |
| 40 | #define RETURN_ERROR_IF(x,e) \ |
| 41 | do { \ |
| 42 | if (x) { \ |
| 43 | errno = (e); \ |
| 44 | return -1; \ |
| 45 | } \ |
| 46 | } while (0) |
| 47 | |
| 48 | #define RETURN_ERROR_IF_RO_OR_LO RETURN_ERROR_IF(read_only || list_only, EROFS) |
| 49 | |
| 50 | int do_unlink(const char *fname) |
| 51 | { |
| 52 | if (dry_run) return 0; |
| 53 | RETURN_ERROR_IF_RO_OR_LO; |
| 54 | return unlink(fname); |
| 55 | } |
| 56 | |
| 57 | #ifdef SUPPORT_LINKS |
| 58 | int do_symlink(const char *lnk, const char *fname) |
| 59 | { |
| 60 | if (dry_run) return 0; |
| 61 | RETURN_ERROR_IF_RO_OR_LO; |
| 62 | |
| 63 | #ifdef NO_SYMLINK_XATTRS |
| 64 | /* For --fake-super, we create a normal file with mode 0600 |
| 65 | * and write the lnk into it. */ |
| 66 | if (am_root < 0) { |
| 67 | int ok, len = strlen(lnk); |
| 68 | int fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, S_IWUSR|S_IRUSR); |
| 69 | if (fd < 0) |
| 70 | return -1; |
| 71 | ok = write(fd, lnk, len) == len; |
| 72 | if (close(fd) < 0) |
| 73 | ok = 0; |
| 74 | return ok ? 0 : -1; |
| 75 | } |
| 76 | #endif |
| 77 | |
| 78 | return symlink(lnk, fname); |
| 79 | } |
| 80 | |
| 81 | #ifdef NO_SYMLINK_XATTRS |
| 82 | ssize_t do_readlink(const char *path, char *buf, size_t bufsiz) |
| 83 | { |
| 84 | /* For --fake-super, we read the link from the file. */ |
| 85 | if (am_root < 0) { |
| 86 | int fd = open(path, O_RDONLY|O_NOFOLLOW); |
| 87 | if (fd >= 0) { |
| 88 | int len = read(fd, buf, bufsiz); |
| 89 | close(fd); |
| 90 | return len; |
| 91 | } |
| 92 | if (errno != ELOOP) |
| 93 | return -1; |
| 94 | /* A real symlink needs to be turned into a fake one on the receiving |
| 95 | * side, so tell the generator that the link has no length. */ |
| 96 | if (!am_sender) |
| 97 | return 0; |
| 98 | /* Otherwise fall through and let the sender report the real length. */ |
| 99 | } |
| 100 | |
| 101 | return readlink(path, buf, bufsiz); |
| 102 | } |
| 103 | #endif |
| 104 | #endif |
| 105 | |
| 106 | #ifdef HAVE_LINK |
| 107 | int do_link(const char *fname1, const char *fname2) |
| 108 | { |
| 109 | if (dry_run) return 0; |
| 110 | RETURN_ERROR_IF_RO_OR_LO; |
| 111 | return link(fname1, fname2); |
| 112 | } |
| 113 | #endif |
| 114 | |
| 115 | int do_lchown(const char *path, uid_t owner, gid_t group) |
| 116 | { |
| 117 | if (dry_run) return 0; |
| 118 | RETURN_ERROR_IF_RO_OR_LO; |
| 119 | #ifndef HAVE_LCHOWN |
| 120 | #define lchown chown |
| 121 | #endif |
| 122 | return lchown(path, owner, group); |
| 123 | } |
| 124 | |
| 125 | int do_mknod(const char *pathname, mode_t mode, dev_t dev) |
| 126 | { |
| 127 | if (dry_run) return 0; |
| 128 | RETURN_ERROR_IF_RO_OR_LO; |
| 129 | |
| 130 | /* For --fake-super, we create a normal file with mode 0600. */ |
| 131 | if (am_root < 0) { |
| 132 | int fd = open(pathname, O_WRONLY|O_CREAT|O_TRUNC, S_IWUSR|S_IRUSR); |
| 133 | if (fd < 0 || close(fd) < 0) |
| 134 | return -1; |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | #if !defined MKNOD_CREATES_FIFOS && defined HAVE_MKFIFO |
| 139 | if (S_ISFIFO(mode)) |
| 140 | return mkfifo(pathname, mode); |
| 141 | #endif |
| 142 | #if !defined MKNOD_CREATES_SOCKETS && defined HAVE_SYS_UN_H |
| 143 | if (S_ISSOCK(mode)) { |
| 144 | int sock; |
| 145 | struct sockaddr_un saddr; |
| 146 | #ifdef HAVE_SOCKADDR_UN_LEN |
| 147 | unsigned int len = |
| 148 | #endif |
| 149 | strlcpy(saddr.sun_path, pathname, sizeof saddr.sun_path); |
| 150 | #ifdef HAVE_SOCKADDR_UN_LEN |
| 151 | saddr.sun_len = len >= sizeof saddr.sun_path |
| 152 | ? sizeof saddr.sun_path : len + 1; |
| 153 | #endif |
| 154 | saddr.sun_family = AF_UNIX; |
| 155 | |
| 156 | if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0 |
| 157 | || (unlink(pathname) < 0 && errno != ENOENT) |
| 158 | || (bind(sock, (struct sockaddr*)&saddr, sizeof saddr)) < 0) |
| 159 | return -1; |
| 160 | close(sock); |
| 161 | #ifdef HAVE_CHMOD |
| 162 | return do_chmod(pathname, mode); |
| 163 | #else |
| 164 | return 0; |
| 165 | #endif |
| 166 | } |
| 167 | #endif |
| 168 | #ifdef HAVE_MKNOD |
| 169 | return mknod(pathname, mode, dev); |
| 170 | #else |
| 171 | return -1; |
| 172 | #endif |
| 173 | } |
| 174 | |
| 175 | int do_rmdir(const char *pathname) |
| 176 | { |
| 177 | if (dry_run) return 0; |
| 178 | RETURN_ERROR_IF_RO_OR_LO; |
| 179 | return rmdir(pathname); |
| 180 | } |
| 181 | |
| 182 | int do_open(const char *pathname, int flags, mode_t mode) |
| 183 | { |
| 184 | if (flags != O_RDONLY) { |
| 185 | RETURN_ERROR_IF(dry_run, 0); |
| 186 | RETURN_ERROR_IF_RO_OR_LO; |
| 187 | } |
| 188 | |
| 189 | return open(pathname, flags | O_BINARY, mode); |
| 190 | } |
| 191 | |
| 192 | #ifdef HAVE_CHMOD |
| 193 | int do_chmod(const char *path, mode_t mode) |
| 194 | { |
| 195 | int code; |
| 196 | if (dry_run) return 0; |
| 197 | RETURN_ERROR_IF_RO_OR_LO; |
| 198 | if (S_ISLNK(mode)) { |
| 199 | #ifdef HAVE_LCHMOD |
| 200 | code = lchmod(path, mode & CHMOD_BITS); |
| 201 | #elif defined HAVE_SETATTRLIST |
| 202 | struct attrlist attrList; |
| 203 | uint32_t m = mode & CHMOD_BITS; /* manpage is wrong: not mode_t! */ |
| 204 | |
| 205 | memset(&attrList, 0, sizeof attrList); |
| 206 | attrList.bitmapcount = ATTR_BIT_MAP_COUNT; |
| 207 | attrList.commonattr = ATTR_CMN_ACCESSMASK; |
| 208 | code = setattrlist(path, &attrList, &m, sizeof m, FSOPT_NOFOLLOW); |
| 209 | #else |
| 210 | code = 1; |
| 211 | #endif |
| 212 | } else |
| 213 | code = chmod(path, mode & CHMOD_BITS); /* DISCOURAGED FUNCTION */ |
| 214 | if (code != 0 && (preserve_perms || preserve_executability)) |
| 215 | return code; |
| 216 | return 0; |
| 217 | } |
| 218 | #endif |
| 219 | |
| 220 | int do_rename(const char *fname1, const char *fname2) |
| 221 | { |
| 222 | if (dry_run) return 0; |
| 223 | RETURN_ERROR_IF_RO_OR_LO; |
| 224 | return rename(fname1, fname2); |
| 225 | } |
| 226 | |
| 227 | void trim_trailing_slashes(char *name) |
| 228 | { |
| 229 | int l; |
| 230 | /* Some BSD systems cannot make a directory if the name |
| 231 | * contains a trailing slash. |
| 232 | * <http://www.opensource.apple.com/bugs/X/BSD%20Kernel/2734739.html> */ |
| 233 | |
| 234 | /* Don't change empty string; and also we can't improve on |
| 235 | * "/" */ |
| 236 | |
| 237 | l = strlen(name); |
| 238 | while (l > 1) { |
| 239 | if (name[--l] != '/') |
| 240 | break; |
| 241 | name[l] = '\0'; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | int do_mkdir(char *fname, mode_t mode) |
| 246 | { |
| 247 | if (dry_run) return 0; |
| 248 | RETURN_ERROR_IF_RO_OR_LO; |
| 249 | trim_trailing_slashes(fname); |
| 250 | return mkdir(fname, mode); |
| 251 | } |
| 252 | |
| 253 | /* like mkstemp but forces permissions */ |
| 254 | int do_mkstemp(char *template, mode_t perms) |
| 255 | { |
| 256 | RETURN_ERROR_IF(dry_run, 0); |
| 257 | RETURN_ERROR_IF(read_only, EROFS); |
| 258 | perms |= S_IWUSR; |
| 259 | |
| 260 | #if defined HAVE_SECURE_MKSTEMP && defined HAVE_FCHMOD && (!defined HAVE_OPEN64 || defined HAVE_MKSTEMP64) |
| 261 | { |
| 262 | int fd = mkstemp(template); |
| 263 | if (fd == -1) |
| 264 | return -1; |
| 265 | if (fchmod(fd, perms) != 0 && preserve_perms) { |
| 266 | int errno_save = errno; |
| 267 | close(fd); |
| 268 | unlink(template); |
| 269 | errno = errno_save; |
| 270 | return -1; |
| 271 | } |
| 272 | #if defined HAVE_SETMODE && O_BINARY |
| 273 | setmode(fd, O_BINARY); |
| 274 | #endif |
| 275 | return fd; |
| 276 | } |
| 277 | #else |
| 278 | if (!mktemp(template)) |
| 279 | return -1; |
| 280 | return do_open(template, O_RDWR|O_EXCL|O_CREAT, perms); |
| 281 | #endif |
| 282 | } |
| 283 | |
| 284 | int do_stat(const char *fname, STRUCT_STAT *st) |
| 285 | { |
| 286 | #ifdef USE_STAT64_FUNCS |
| 287 | return stat64(fname, st); |
| 288 | #else |
| 289 | return stat(fname, st); |
| 290 | #endif |
| 291 | } |
| 292 | |
| 293 | int do_lstat(const char *fname, STRUCT_STAT *st) |
| 294 | { |
| 295 | #ifdef SUPPORT_LINKS |
| 296 | # ifdef USE_STAT64_FUNCS |
| 297 | return lstat64(fname, st); |
| 298 | # else |
| 299 | return lstat(fname, st); |
| 300 | # endif |
| 301 | #else |
| 302 | return do_stat(fname, st); |
| 303 | #endif |
| 304 | } |
| 305 | |
| 306 | int do_fstat(int fd, STRUCT_STAT *st) |
| 307 | { |
| 308 | #ifdef USE_STAT64_FUNCS |
| 309 | return fstat64(fd, st); |
| 310 | #else |
| 311 | return fstat(fd, st); |
| 312 | #endif |
| 313 | } |
| 314 | |
| 315 | OFF_T do_lseek(int fd, OFF_T offset, int whence) |
| 316 | { |
| 317 | #ifdef HAVE_LSEEK64 |
| 318 | #if !SIZEOF_OFF64_T |
| 319 | OFF_T lseek64(); |
| 320 | #else |
| 321 | off64_t lseek64(); |
| 322 | #endif |
| 323 | return lseek64(fd, offset, whence); |
| 324 | #else |
| 325 | return lseek(fd, offset, whence); |
| 326 | #endif |
| 327 | } |