Remove existing file @dest and reopen, creating a new file with @mode */
| 321 | |
| 322 | /* Remove existing file @dest and reopen, creating a new file with @mode */ |
| 323 | static int unlink_and_reopen(const char *dest, mode_t mode) |
| 324 | { |
| 325 | int ofd; |
| 326 | |
| 327 | if (robust_unlink(dest) && errno != ENOENT) { |
| 328 | int save_errno = errno; |
| 329 | rsyserr(FERROR_XFER, errno, "unlink %s", full_fname(dest)); |
| 330 | errno = save_errno; |
| 331 | return -1; |
| 332 | } |
| 333 | |
| 334 | #ifdef SUPPORT_XATTRS |
| 335 | if (preserve_xattrs) |
| 336 | mode |= S_IWUSR; |
| 337 | #endif |
| 338 | mode &= INITACCESSPERMS; |
| 339 | /* Use do_open_at so the create/truncate goes through a secure |
| 340 | * parent dirfd in the daemon-no-chroot deployment. Otherwise |
| 341 | * an attacker could swap a parent component with a symlink in |
| 342 | * the window between robust_unlink (which uses do_unlink_at, |
| 343 | * already secure) and the create here, and redirect the new |
| 344 | * file outside the module. */ |
| 345 | if ((ofd = do_open_at(dest, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, mode)) < 0) { |
| 346 | int save_errno = errno; |
| 347 | rsyserr(FERROR_XFER, save_errno, "open %s", full_fname(dest)); |
| 348 | errno = save_errno; |
| 349 | return -1; |
| 350 | } |
| 351 | return ofd; |
| 352 | } |
| 353 | |
| 354 | /* Copy contents of file @source to file @dest with mode @mode. |
| 355 | * |
no test coverage detected