| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include "rsync.h" |
| 22 |
#include "ifuncs.h" |
| 23 |
|
| 24 |
extern int am_root; |
| 25 |
extern int preserve_acls; |
| 26 |
extern int preserve_xattrs; |
| 27 |
extern int preserve_devices; |
| 28 |
extern int preserve_specials; |
| 29 |
extern int preserve_links; |
| 30 |
extern int safe_symlinks; |
| 31 |
extern int backup_dir_len; |
| 32 |
extern unsigned int backup_dir_remainder; |
| 33 |
extern int operator_path_resolve; |
| 34 |
extern char backup_dir_buf[MAXPATHLEN]; |
| 35 |
extern char *backup_suffix; |
| 36 |
extern char *backup_dir; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
int backup_metadata_hardened(void) |
| 46 |
{ |
| 47 |
return secure_relpath_active() && !symlink_optout_allowed(); |
| 48 |
} |
| 49 |
|
| 50 |
int backup_source_fd(const char *path) |
| 51 |
{ |
| 52 |
#if defined AT_FDCWD && defined O_NOFOLLOW |
| 53 |
if (backup_metadata_hardened() && path && *path) { |
| 54 |
int save = operator_path_resolve, fd; |
| 55 |
operator_path_resolve = 1; |
| 56 |
fd = do_open_at(path, O_RDONLY | O_NONBLOCK | O_NOCTTY | O_CLOEXEC, 0); |
| 57 |
operator_path_resolve = save; |
| 58 |
return fd; |
| 59 |
} |
| 60 |
#endif |
| 61 |
return -1; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
static int validate_backup_dir(void) |
| 66 |
{ |
| 67 |
STRUCT_STAT st; |
| 68 |
|
| 69 |
if (do_lstat_at(backup_dir_buf, &st) < 0) { |
| 70 |
if (errno == ENOENT) |
| 71 |
return 0; |
| 72 |
rsyserr(FERROR, errno, "backup lstat %s failed", backup_dir_buf); |
| 73 |
return -1; |
| 74 |
} |
| 75 |
if (!S_ISDIR(st.st_mode)) { |
| 76 |
int flags = get_del_for_flag(st.st_mode) | DEL_FOR_BACKUP | DEL_RECURSE; |
| 77 |
if (delete_item(backup_dir_buf, st.st_mode, flags) == 0) |
| 78 |
return 0; |
| 79 |
return -1; |
| 80 |
} |
| 81 |
return 1; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
static BOOL copy_valid_path(const char *fname) |
| 89 |
{ |
| 90 |
const char *f; |
| 91 |
int val; |
| 92 |
BOOL ret = True; |
| 93 |
stat_x sx; |
| 94 |
char *b, *rel = backup_dir_buf + backup_dir_len, *name = rel; |
| 95 |
|
| 96 |
for (f = fname, b = rel; *f && *f == *b; f++, b++) { |
| 97 |
if (*b == '/') |
| 98 |
name = b + 1; |
| 99 |
} |
| 100 |
|
| 101 |
if (stringjoin(rel, backup_dir_remainder, fname, backup_suffix, NULL) >= backup_dir_remainder) { |
| 102 |
rprintf(FERROR, "backup filename too long\n"); |
| 103 |
*name = '\0'; |
| 104 |
return False; |
| 105 |
} |
| 106 |
|
| 107 |
for ( ; ; name = b + 1) { |
| 108 |
if ((b = strchr(name, '/')) == NULL) |
| 109 |
return True; |
| 110 |
*b = '\0'; |
| 111 |
|
| 112 |
val = validate_backup_dir(); |
| 113 |
if (val == 0) |
| 114 |
break; |
| 115 |
if (val < 0) { |
| 116 |
*name = '\0'; |
| 117 |
return False; |
| 118 |
} |
| 119 |
|
| 120 |
*b = '/'; |
| 121 |
} |
| 122 |
|
| 123 |
init_stat_x(&sx); |
| 124 |
|
| 125 |
for ( ; b; name = b + 1, b = strchr(name, '/')) { |
| 126 |
*b = '\0'; |
| 127 |
|
| 128 |
while (do_mkdir_at(backup_dir_buf, ACCESSPERMS) < 0) { |
| 129 |
if (errno == EEXIST) { |
| 130 |
val = validate_backup_dir(); |
| 131 |
if (val > 0) |
| 132 |
break; |
| 133 |
if (val == 0) |
| 134 |
continue; |
| 135 |
} else |
| 136 |
rsyserr(FERROR, errno, "backup mkdir %s failed", backup_dir_buf); |
| 137 |
*name = '\0'; |
| 138 |
ret = False; |
| 139 |
goto cleanup; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
if (x_stat(rel, &sx.st, NULL) < 0) |
| 145 |
rsyserr(FERROR, errno, "backup stat %s failed", full_fname(rel)); |
| 146 |
else { |
| 147 |
struct file_struct *file; |
| 148 |
if (!(file = make_file(rel, NULL, NULL, 0, NO_FILTERS))) |
| 149 |
continue; |
| 150 |
#if defined SUPPORT_ACLS || defined SUPPORT_XATTRS |
| 151 |
{ |
| 152 |
int bfd = backup_source_fd(rel); |
| 153 |
if (!backup_metadata_hardened() || bfd >= 0) { |
| 154 |
# ifdef SUPPORT_ACLS |
| 155 |
if (preserve_acls && !S_ISLNK(file->mode)) { |
| 156 |
get_acl_fdat(bfd, -1, NULL, rel, &sx); |
| 157 |
cache_tmp_acl(file, &sx); |
| 158 |
free_acl(&sx); |
| 159 |
} |
| 160 |
# endif |
| 161 |
# ifdef SUPPORT_XATTRS |
| 162 |
if (preserve_xattrs) { |
| 163 |
get_xattr(rel, bfd, &sx); |
| 164 |
cache_tmp_xattr(file, &sx); |
| 165 |
free_xattr(&sx); |
| 166 |
} |
| 167 |
# endif |
| 168 |
} |
| 169 |
if (bfd >= 0) |
| 170 |
close(bfd); |
| 171 |
} |
| 172 |
#endif |
| 173 |
set_file_attrs(backup_dir_buf, file, NULL, NULL, 0); |
| 174 |
unmake_file(file); |
| 175 |
} |
| 176 |
|
| 177 |
*b = '/'; |
| 178 |
} |
| 179 |
|
| 180 |
cleanup: |
| 181 |
|
| 182 |
#ifdef SUPPORT_ACLS |
| 183 |
uncache_tmp_acls(); |
| 184 |
#endif |
| 185 |
#ifdef SUPPORT_XATTRS |
| 186 |
uncache_tmp_xattrs(); |
| 187 |
#endif |
| 188 |
|
| 189 |
return ret; |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
char *get_backup_name(const char *fname) |
| 194 |
{ |
| 195 |
if (backup_dir) { |
| 196 |
static int initialized = 0; |
| 197 |
if (!initialized) { |
| 198 |
char dirbuf[MAXPATHLEN]; |
| 199 |
int ret; |
| 200 |
if (strlcpy(dirbuf, backup_dir_buf, sizeof dirbuf) >= sizeof dirbuf) { |
| 201 |
errno = ENAMETOOLONG; |
| 202 |
return NULL; |
| 203 |
} |
| 204 |
if (backup_dir_len > 1) |
| 205 |
dirbuf[backup_dir_len-1] = '\0'; |
| 206 |
ret = make_path(dirbuf, 0); |
| 207 |
if (ret < 0) |
| 208 |
return NULL; |
| 209 |
initialized = 1; |
| 210 |
} |
| 211 |
|
| 212 |
if (copy_valid_path(fname)) |
| 213 |
return backup_dir_buf; |
| 214 |
|
| 215 |
return NULL; |
| 216 |
} |
| 217 |
|
| 218 |
if (stringjoin(backup_dir_buf, MAXPATHLEN, fname, backup_suffix, NULL) < MAXPATHLEN) |
| 219 |
return backup_dir_buf; |
| 220 |
|
| 221 |
rprintf(FERROR, "backup filename too long\n"); |
| 222 |
return NULL; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
static inline int link_or_rename(const char *from, const char *to, |
| 227 |
BOOL prefer_rename, STRUCT_STAT *stp) |
| 228 |
{ |
| 229 |
#ifdef SUPPORT_HARD_LINKS |
| 230 |
if (!prefer_rename) { |
| 231 |
#ifndef CAN_HARDLINK_SYMLINK |
| 232 |
if (S_ISLNK(stp->st_mode)) |
| 233 |
return 0; |
| 234 |
#endif |
| 235 |
#ifndef CAN_HARDLINK_SPECIAL |
| 236 |
if (IS_SPECIAL(stp->st_mode) || IS_DEVICE(stp->st_mode)) |
| 237 |
return 0; |
| 238 |
#endif |
| 239 |
if (do_link_at(from, to) == 0) { |
| 240 |
if (DEBUG_GTE(BACKUP, 1)) |
| 241 |
rprintf(FINFO, "make_backup: HLINK %s successful.\n", from); |
| 242 |
return 2; |
| 243 |
} |
| 244 |
|
| 245 |
if (!S_ISREG(stp->st_mode) || errno == EEXIST || errno == EISDIR) |
| 246 |
return 0; |
| 247 |
} |
| 248 |
#endif |
| 249 |
if (do_rename_at(from, to) == 0) { |
| 250 |
if (stp->st_nlink > 1 && !S_ISDIR(stp->st_mode)) { |
| 251 |
|
| 252 |
|
| 253 |
robust_unlink(from); |
| 254 |
} |
| 255 |
if (DEBUG_GTE(BACKUP, 1)) |
| 256 |
rprintf(FINFO, "make_backup: RENAME %s successful.\n", from); |
| 257 |
return 1; |
| 258 |
} |
| 259 |
return 0; |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
static int make_backup_inner(const char *fname, BOOL prefer_rename) |
| 266 |
{ |
| 267 |
stat_x sx; |
| 268 |
struct file_struct *file; |
| 269 |
int save_preserve_xattrs; |
| 270 |
char *buf; |
| 271 |
int ret = 0; |
| 272 |
|
| 273 |
init_stat_x(&sx); |
| 274 |
|
| 275 |
if (x_lstat(fname, &sx.st, NULL) < 0) |
| 276 |
return 3; |
| 277 |
|
| 278 |
if (!(buf = get_backup_name(fname))) |
| 279 |
return 0; |
| 280 |
|
| 281 |
#ifdef SUPPORT_LINKS |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
if (preserve_links && S_ISLNK(sx.st.st_mode) && safe_symlinks) { |
| 290 |
char lnkbuf[MAXPATHLEN]; |
| 291 |
int llen = do_readlink(fname, lnkbuf, MAXPATHLEN - 1); |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
if (llen <= 0) { |
| 296 |
if (INFO_GTE(SYMSAFE, 1)) |
| 297 |
rprintf(FINFO, "not backing up symlink with unreadable target \"%s\"\n", fname); |
| 298 |
ret = 2; |
| 299 |
goto success; |
| 300 |
} |
| 301 |
lnkbuf[llen] = '\0'; |
| 302 |
if (unsafe_symlink(lnkbuf, fname)) { |
| 303 |
if (INFO_GTE(SYMSAFE, 1)) { |
| 304 |
rprintf(FINFO, "not backing up unsafe symlink \"%s\" -> \"%s\"\n", |
| 305 |
fname, lnkbuf); |
| 306 |
} |
| 307 |
ret = 2; |
| 308 |
goto success; |
| 309 |
} |
| 310 |
} |
| 311 |
#endif |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
if ((ret = link_or_rename(fname, buf, prefer_rename, &sx.st)) != 0) |
| 317 |
goto success; |
| 318 |
if (errno == EEXIST || errno == EISDIR) { |
| 319 |
STRUCT_STAT bakst; |
| 320 |
if (do_lstat_at(buf, &bakst) == 0) { |
| 321 |
int flags = get_del_for_flag(bakst.st_mode) | DEL_FOR_BACKUP | DEL_RECURSE; |
| 322 |
if (delete_item(buf, bakst.st_mode, flags) != 0) |
| 323 |
return 0; |
| 324 |
} |
| 325 |
if ((ret = link_or_rename(fname, buf, prefer_rename, &sx.st)) != 0) |
| 326 |
goto success; |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
if (!(file = make_file(fname, NULL, &sx.st, 0, NO_FILTERS))) |
| 331 |
return 3; |
| 332 |
|
| 333 |
#if defined SUPPORT_ACLS || defined SUPPORT_XATTRS |
| 334 |
{ |
| 335 |
int bfd = backup_source_fd(fname); |
| 336 |
if (!backup_metadata_hardened() || bfd >= 0) { |
| 337 |
# ifdef SUPPORT_ACLS |
| 338 |
if (preserve_acls && !S_ISLNK(file->mode)) { |
| 339 |
get_acl_fdat(bfd, -1, NULL, fname, &sx); |
| 340 |
cache_tmp_acl(file, &sx); |
| 341 |
free_acl(&sx); |
| 342 |
} |
| 343 |
# endif |
| 344 |
# ifdef SUPPORT_XATTRS |
| 345 |
if (preserve_xattrs) { |
| 346 |
get_xattr(fname, bfd, &sx); |
| 347 |
cache_tmp_xattr(file, &sx); |
| 348 |
free_xattr(&sx); |
| 349 |
} |
| 350 |
# endif |
| 351 |
} |
| 352 |
if (bfd >= 0) |
| 353 |
close(bfd); |
| 354 |
} |
| 355 |
#endif |
| 356 |
|
| 357 |
|
| 358 |
if ((am_root && preserve_devices && IS_DEVICE(file->mode)) |
| 359 |
|| (preserve_specials && IS_SPECIAL(file->mode))) { |
| 360 |
if (do_mknod_at(buf, file->mode, sx.st.st_rdev) < 0) |
| 361 |
rsyserr(FERROR, errno, "mknod %s failed", full_fname(buf)); |
| 362 |
else if (DEBUG_GTE(BACKUP, 1)) |
| 363 |
rprintf(FINFO, "make_backup: DEVICE %s successful.\n", fname); |
| 364 |
ret = 2; |
| 365 |
} |
| 366 |
|
| 367 |
#ifdef SUPPORT_LINKS |
| 368 |
if (!ret && preserve_links && S_ISLNK(file->mode)) { |
| 369 |
const char *sl = F_SYMLINK(file); |
| 370 |
if (safe_symlinks && unsafe_symlink(sl, fname)) { |
| 371 |
if (INFO_GTE(SYMSAFE, 1)) { |
| 372 |
rprintf(FINFO, "not backing up unsafe symlink \"%s\" -> \"%s\"\n", |
| 373 |
fname, sl); |
| 374 |
} |
| 375 |
ret = 2; |
| 376 |
} else { |
| 377 |
if (do_symlink_at(sl, buf) < 0) |
| 378 |
rsyserr(FERROR, errno, "link %s -> \"%s\"", full_fname(buf), sl); |
| 379 |
else if (DEBUG_GTE(BACKUP, 1)) |
| 380 |
rprintf(FINFO, "make_backup: SYMLINK %s successful.\n", fname); |
| 381 |
ret = 2; |
| 382 |
} |
| 383 |
} |
| 384 |
#endif |
| 385 |
|
| 386 |
if (!ret && !S_ISREG(file->mode)) { |
| 387 |
if (INFO_GTE(NONREG, 1)) |
| 388 |
rprintf(FINFO, "make_bak: skipping non-regular file %s\n", fname); |
| 389 |
unmake_file(file); |
| 390 |
#ifdef SUPPORT_ACLS |
| 391 |
uncache_tmp_acls(); |
| 392 |
#endif |
| 393 |
#ifdef SUPPORT_XATTRS |
| 394 |
uncache_tmp_xattrs(); |
| 395 |
#endif |
| 396 |
return 3; |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
if (!ret) { |
| 401 |
if (copy_file(fname, buf, -1, file->mode) < 0) { |
| 402 |
rsyserr(FERROR, errno, "keep_backup failed: %s -> \"%s\"", |
| 403 |
full_fname(fname), buf); |
| 404 |
unmake_file(file); |
| 405 |
#ifdef SUPPORT_ACLS |
| 406 |
uncache_tmp_acls(); |
| 407 |
#endif |
| 408 |
#ifdef SUPPORT_XATTRS |
| 409 |
uncache_tmp_xattrs(); |
| 410 |
#endif |
| 411 |
return 0; |
| 412 |
} |
| 413 |
if (DEBUG_GTE(BACKUP, 1)) |
| 414 |
rprintf(FINFO, "make_backup: COPY %s successful.\n", fname); |
| 415 |
ret = 2; |
| 416 |
} |
| 417 |
|
| 418 |
save_preserve_xattrs = preserve_xattrs; |
| 419 |
preserve_xattrs = 0; |
| 420 |
set_file_attrs(buf, file, NULL, fname, ATTRS_ACCURATE_TIME); |
| 421 |
preserve_xattrs = save_preserve_xattrs; |
| 422 |
|
| 423 |
unmake_file(file); |
| 424 |
#ifdef SUPPORT_ACLS |
| 425 |
uncache_tmp_acls(); |
| 426 |
#endif |
| 427 |
#ifdef SUPPORT_XATTRS |
| 428 |
uncache_tmp_xattrs(); |
| 429 |
#endif |
| 430 |
|
| 431 |
success: |
| 432 |
if (INFO_GTE(BACKUP, 1)) |
| 433 |
rprintf(FINFO, "backed up %s to %s\n", fname, buf); |
| 434 |
return ret; |
| 435 |
} |
| 436 |
|
| 437 |
int make_backup(const char *fname, BOOL prefer_rename) |
| 438 |
{ |
| 439 |
int ret; |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
operator_path_resolve = 1; |
| 446 |
ret = make_backup_inner(fname, prefer_rename); |
| 447 |
operator_path_resolve = 0; |
| 448 |
return ret; |
| 449 |
} |