| 1 | /* |
| 2 | * Routines common to more than one of the rsync processes. |
| 3 | * |
| 4 | * Copyright (C) 1996 Andrew Tridgell |
| 5 | * Copyright (C) 1996 Paul Mackerras |
| 6 | * Copyright (C) 2003, 2004, 2005, 2006 Wayne Davison |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. |
| 21 | */ |
| 22 | |
| 23 | #include "rsync.h" |
| 24 | #if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H |
| 25 | #include <iconv.h> |
| 26 | #endif |
| 27 | #if defined HAVE_LIBCHARSET_H && defined HAVE_LOCALE_CHARSET |
| 28 | #include <libcharset.h> |
| 29 | #elif defined HAVE_LANGINFO_H && defined HAVE_NL_LANGINFO |
| 30 | #include <langinfo.h> |
| 31 | #endif |
| 32 | |
| 33 | extern int verbose; |
| 34 | extern int dry_run; |
| 35 | extern int preserve_perms; |
| 36 | extern int preserve_executability; |
| 37 | extern int preserve_times; |
| 38 | extern int omit_dir_times; |
| 39 | extern int am_root; |
| 40 | extern int am_server; |
| 41 | extern int am_sender; |
| 42 | extern int am_generator; |
| 43 | extern int am_starting_up; |
| 44 | extern int allow_8bit_chars; |
| 45 | extern int protocol_version; |
| 46 | extern int preserve_uid; |
| 47 | extern int preserve_gid; |
| 48 | extern int incremental; |
| 49 | extern int inplace; |
| 50 | extern int flist_eof; |
| 51 | extern int keep_dirlinks; |
| 52 | extern int make_backups; |
| 53 | extern mode_t orig_umask; |
| 54 | extern struct file_list *cur_flist, *first_flist, *dir_flist; |
| 55 | extern struct chmod_mode_struct *daemon_chmod_modes; |
| 56 | |
| 57 | #if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H |
| 58 | iconv_t ic_chck = (iconv_t)-1; |
| 59 | |
| 60 | static const char *default_charset(void) |
| 61 | { |
| 62 | #if defined HAVE_LIBCHARSET_H && defined HAVE_LOCALE_CHARSET |
| 63 | return locale_charset(); |
| 64 | #elif defined HAVE_LANGINFO_H && defined HAVE_NL_LANGINFO |
| 65 | return nl_langinfo(CODESET); |
| 66 | #else |
| 67 | return ""; /* Works with (at the very least) gnu iconv... */ |
| 68 | #endif |
| 69 | } |
| 70 | |
| 71 | void setup_iconv() |
| 72 | { |
| 73 | if (!am_server && !allow_8bit_chars) { |
| 74 | const char *defset = default_charset(); |
| 75 | |
| 76 | /* It's OK if this fails... */ |
| 77 | ic_chck = iconv_open(defset, defset); |
| 78 | |
| 79 | if (verbose > 3) { |
| 80 | if (ic_chck == (iconv_t)-1) { |
| 81 | rprintf(FINFO, |
| 82 | "note: iconv_open(\"%s\", \"%s\") failed (%d)" |
| 83 | " -- using isprint() instead of iconv().\n", |
| 84 | defset, defset, errno); |
| 85 | } else { |
| 86 | rprintf(FINFO, |
| 87 | "note: iconv_open(\"%s\", \"%s\") succeeded.\n", |
| 88 | defset, defset); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | #endif |
| 94 | |
| 95 | /* This is used by sender.c with a valid f_out, and by receive.c with |
| 96 | * f_out = -1. */ |
| 97 | int read_ndx_and_attrs(int f_in, int f_out, int *iflag_ptr, |
| 98 | uchar *type_ptr, char *buf, int *len_ptr) |
| 99 | { |
| 100 | int len, iflags = 0; |
| 101 | struct file_list *flist; |
| 102 | uchar fnamecmp_type = FNAMECMP_FNAME; |
| 103 | int verbose_save, ndx; |
| 104 | |
| 105 | read_loop: |
| 106 | while (1) { |
| 107 | ndx = read_ndx(f_in); |
| 108 | |
| 109 | if (ndx >= 0) |
| 110 | break; |
| 111 | if (ndx == NDX_DONE) |
| 112 | return ndx; |
| 113 | if (!incremental || am_sender) |
| 114 | goto invalid_ndx; |
| 115 | if (ndx == NDX_FLIST_EOF) { |
| 116 | flist_eof = 1; |
| 117 | send_msg(MSG_FLIST_EOF, "", 0); |
| 118 | continue; |
| 119 | } |
| 120 | ndx = NDX_FLIST_OFFSET - ndx; |
| 121 | if (ndx < 0 || ndx >= dir_flist->count) { |
| 122 | ndx = NDX_FLIST_OFFSET - ndx; |
| 123 | rprintf(FERROR, |
| 124 | "Invalid dir index: %d (%d - %d)\n", |
| 125 | ndx, NDX_FLIST_OFFSET, |
| 126 | NDX_FLIST_OFFSET - dir_flist->count); |
| 127 | exit_cleanup(RERR_PROTOCOL); |
| 128 | } |
| 129 | verbose_save = verbose; |
| 130 | verbose = 0; /* TODO allow verbose messages? */ |
| 131 | |
| 132 | /* Send everything read from f_in to msg_fd_out. */ |
| 133 | send_msg_int(MSG_FLIST, ndx); |
| 134 | start_flist_forward(f_in); |
| 135 | flist = recv_file_list(f_in); |
| 136 | flist->parent_ndx = ndx; |
| 137 | stop_flist_forward(); |
| 138 | |
| 139 | verbose = verbose_save; |
| 140 | } |
| 141 | |
| 142 | iflags = protocol_version >= 29 ? read_shortint(f_in) |
| 143 | : ITEM_TRANSFER | ITEM_MISSING_DATA; |
| 144 | |
| 145 | /* Honor the old-style keep-alive indicator. */ |
| 146 | if (protocol_version < 30 |
| 147 | && ndx == cur_flist->count && iflags == ITEM_IS_NEW) { |
| 148 | if (am_sender) |
| 149 | maybe_send_keepalive(); |
| 150 | goto read_loop; |
| 151 | } |
| 152 | |
| 153 | if (!(flist = flist_for_ndx(ndx))) { |
| 154 | invalid_ndx: |
| 155 | rprintf(FERROR, |
| 156 | "Invalid file index: %d (%d - %d) with iflags %x [%s]\n", |
| 157 | ndx, first_flist->ndx_start + first_flist->ndx_start, |
| 158 | first_flist->prev->ndx_start + first_flist->ndx_start |
| 159 | + first_flist->prev->count - 1, iflags, who_am_i()); |
| 160 | exit_cleanup(RERR_PROTOCOL); |
| 161 | } |
| 162 | cur_flist = flist; |
| 163 | |
| 164 | if (iflags & ITEM_BASIS_TYPE_FOLLOWS) |
| 165 | fnamecmp_type = read_byte(f_in); |
| 166 | *type_ptr = fnamecmp_type; |
| 167 | |
| 168 | if (iflags & ITEM_XNAME_FOLLOWS) { |
| 169 | if ((len = read_vstring(f_in, buf, MAXPATHLEN)) < 0) |
| 170 | exit_cleanup(RERR_PROTOCOL); |
| 171 | } else { |
| 172 | *buf = '\0'; |
| 173 | len = -1; |
| 174 | } |
| 175 | *len_ptr = len; |
| 176 | |
| 177 | if (iflags & ITEM_TRANSFER) { |
| 178 | int i = ndx - cur_flist->ndx_start; |
| 179 | if (!S_ISREG(cur_flist->files[i]->mode)) { |
| 180 | rprintf(FERROR, |
| 181 | "received request to transfer non-regular file: %d [%s]\n", |
| 182 | ndx, who_am_i()); |
| 183 | exit_cleanup(RERR_PROTOCOL); |
| 184 | } |
| 185 | } else if (f_out >= 0) { |
| 186 | write_ndx_and_attrs(f_out, ndx, iflags, |
| 187 | fnamecmp_type, buf, len); |
| 188 | } |
| 189 | |
| 190 | *iflag_ptr = iflags; |
| 191 | return ndx; |
| 192 | } |
| 193 | |
| 194 | /* |
| 195 | free a sums struct |
| 196 | */ |
| 197 | void free_sums(struct sum_struct *s) |
| 198 | { |
| 199 | if (s->sums) free(s->sums); |
| 200 | free(s); |
| 201 | } |
| 202 | |
| 203 | /* This is only called when we aren't preserving permissions. Figure out what |
| 204 | * the permissions should be and return them merged back into the mode. */ |
| 205 | mode_t dest_mode(mode_t flist_mode, mode_t stat_mode, int exists) |
| 206 | { |
| 207 | int new_mode; |
| 208 | /* If the file already exists, we'll return the local permissions, |
| 209 | * possibly tweaked by the --executability option. */ |
| 210 | if (exists) { |
| 211 | new_mode = (flist_mode & ~CHMOD_BITS) | (stat_mode & CHMOD_BITS); |
| 212 | if (preserve_executability && S_ISREG(flist_mode)) { |
| 213 | /* If the source file is executable, grant execute |
| 214 | * rights to everyone who can read, but ONLY if the |
| 215 | * file isn't already executable. */ |
| 216 | if (!(flist_mode & 0111)) |
| 217 | new_mode &= ~0111; |
| 218 | else if (!(stat_mode & 0111)) |
| 219 | new_mode |= (new_mode & 0444) >> 2; |
| 220 | } |
| 221 | } else { |
| 222 | /* Apply the umask and turn off special permissions. */ |
| 223 | new_mode = flist_mode & (~CHMOD_BITS | (ACCESSPERMS & ~orig_umask)); |
| 224 | } |
| 225 | return new_mode; |
| 226 | } |
| 227 | |
| 228 | int set_file_attrs(char *fname, struct file_struct *file, STRUCT_STAT *st, |
| 229 | int flags) |
| 230 | { |
| 231 | int updated = 0; |
| 232 | STRUCT_STAT st2; |
| 233 | int change_uid, change_gid; |
| 234 | mode_t new_mode = file->mode; |
| 235 | |
| 236 | if (!st) { |
| 237 | if (dry_run) |
| 238 | return 1; |
| 239 | if (link_stat(fname, &st2, 0) < 0) { |
| 240 | rsyserr(FERROR, errno, "stat %s failed", |
| 241 | full_fname(fname)); |
| 242 | return 0; |
| 243 | } |
| 244 | st = &st2; |
| 245 | if (!preserve_perms && S_ISDIR(new_mode) |
| 246 | && st->st_mode & S_ISGID) { |
| 247 | /* We just created this directory and its setgid |
| 248 | * bit is on, so make sure it stays on. */ |
| 249 | new_mode |= S_ISGID; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | if (!preserve_times || (S_ISDIR(st->st_mode) && omit_dir_times)) |
| 254 | flags |= ATTRS_SKIP_MTIME; |
| 255 | if (!(flags & ATTRS_SKIP_MTIME) |
| 256 | && cmp_time(st->st_mtime, file->modtime) != 0) { |
| 257 | int ret = set_modtime(fname, file->modtime, st->st_mode); |
| 258 | if (ret < 0) { |
| 259 | rsyserr(FERROR, errno, "failed to set times on %s", |
| 260 | full_fname(fname)); |
| 261 | return 0; |
| 262 | } |
| 263 | if (ret == 0) /* ret == 1 if symlink could not be set */ |
| 264 | updated = 1; |
| 265 | } |
| 266 | |
| 267 | change_uid = am_root && preserve_uid && st->st_uid != F_UID(file); |
| 268 | change_gid = preserve_gid && F_GID(file) != GID_NONE |
| 269 | && st->st_gid != F_GID(file); |
| 270 | #if !defined HAVE_LCHOWN && !defined CHOWN_MODIFIES_SYMLINK |
| 271 | if (S_ISLNK(st->st_mode)) |
| 272 | ; |
| 273 | else |
| 274 | #endif |
| 275 | if (change_uid || change_gid) { |
| 276 | if (verbose > 2) { |
| 277 | if (change_uid) { |
| 278 | rprintf(FINFO, |
| 279 | "set uid of %s from %ld to %ld\n", |
| 280 | fname, |
| 281 | (long)st->st_uid, (long)F_UID(file)); |
| 282 | } |
| 283 | if (change_gid) { |
| 284 | rprintf(FINFO, |
| 285 | "set gid of %s from %ld to %ld\n", |
| 286 | fname, |
| 287 | (long)st->st_gid, (long)F_GID(file)); |
| 288 | } |
| 289 | } |
| 290 | if (do_lchown(fname, |
| 291 | change_uid ? F_UID(file) : st->st_uid, |
| 292 | change_gid ? F_GID(file) : st->st_gid) != 0) { |
| 293 | /* shouldn't have attempted to change uid or gid |
| 294 | * unless have the privilege */ |
| 295 | rsyserr(FERROR, errno, "%s %s failed", |
| 296 | change_uid ? "chown" : "chgrp", |
| 297 | full_fname(fname)); |
| 298 | return 0; |
| 299 | } |
| 300 | /* a lchown had been done - we have to re-stat if the |
| 301 | * destination had the setuid or setgid bits set due |
| 302 | * to the side effect of the chown call */ |
| 303 | if (st->st_mode & (S_ISUID | S_ISGID)) { |
| 304 | link_stat(fname, st, |
| 305 | keep_dirlinks && S_ISDIR(st->st_mode)); |
| 306 | } |
| 307 | updated = 1; |
| 308 | } |
| 309 | |
| 310 | if (daemon_chmod_modes && !S_ISLNK(new_mode)) |
| 311 | new_mode = tweak_mode(new_mode, daemon_chmod_modes); |
| 312 | #ifdef HAVE_CHMOD |
| 313 | if (!BITS_EQUAL(st->st_mode, new_mode, CHMOD_BITS)) { |
| 314 | int ret = do_chmod(fname, new_mode); |
| 315 | if (ret < 0) { |
| 316 | rsyserr(FERROR, errno, |
| 317 | "failed to set permissions on %s", |
| 318 | full_fname(fname)); |
| 319 | return 0; |
| 320 | } |
| 321 | if (ret == 0) /* ret == 1 if symlink could not be set */ |
| 322 | updated = 1; |
| 323 | } |
| 324 | #endif |
| 325 | |
| 326 | if (verbose > 1 && flags & ATTRS_REPORT) { |
| 327 | if (updated) |
| 328 | rprintf(FCLIENT, "%s\n", fname); |
| 329 | else |
| 330 | rprintf(FCLIENT, "%s is uptodate\n", fname); |
| 331 | } |
| 332 | return updated; |
| 333 | } |
| 334 | |
| 335 | RETSIGTYPE sig_int(UNUSED(int val)) |
| 336 | { |
| 337 | /* KLUGE: if the user hits Ctrl-C while ssh is prompting |
| 338 | * for a password, then our cleanup's sending of a SIGUSR1 |
| 339 | * signal to all our children may kill ssh before it has a |
| 340 | * chance to restore the tty settings (i.e. turn echo back |
| 341 | * on). By sleeping for a short time, ssh gets a bigger |
| 342 | * chance to do the right thing. If child processes are |
| 343 | * not ssh waiting for a password, then this tiny delay |
| 344 | * shouldn't hurt anything. */ |
| 345 | msleep(400); |
| 346 | exit_cleanup(RERR_SIGNAL); |
| 347 | } |
| 348 | |
| 349 | /* Finish off a file transfer: renaming the file and setting the file's |
| 350 | * attributes (e.g. permissions, ownership, etc.). If partialptr is not |
| 351 | * NULL and the robust_rename() call is forced to copy the temp file, we |
| 352 | * stage the file into the partial-dir and then rename it into place. */ |
| 353 | void finish_transfer(char *fname, char *fnametmp, char *partialptr, |
| 354 | struct file_struct *file, int ok_to_set_time, |
| 355 | int overwriting_basis) |
| 356 | { |
| 357 | int ret; |
| 358 | |
| 359 | if (inplace) { |
| 360 | if (verbose > 2) |
| 361 | rprintf(FINFO, "finishing %s\n", fname); |
| 362 | fnametmp = fname; |
| 363 | goto do_set_file_attrs; |
| 364 | } |
| 365 | |
| 366 | if (make_backups > 0 && overwriting_basis && !make_backup(fname)) |
| 367 | return; |
| 368 | |
| 369 | /* Change permissions before putting the file into place. */ |
| 370 | set_file_attrs(fnametmp, file, NULL, |
| 371 | ok_to_set_time ? 0 : ATTRS_SKIP_MTIME); |
| 372 | |
| 373 | /* move tmp file over real file */ |
| 374 | if (verbose > 2) |
| 375 | rprintf(FINFO, "renaming %s to %s\n", fnametmp, fname); |
| 376 | ret = robust_rename(fnametmp, fname, partialptr, |
| 377 | file->mode & INITACCESSPERMS); |
| 378 | if (ret < 0) { |
| 379 | rsyserr(FERROR, errno, "%s %s -> \"%s\"", |
| 380 | ret == -2 ? "copy" : "rename", |
| 381 | full_fname(fnametmp), fname); |
| 382 | do_unlink(fnametmp); |
| 383 | return; |
| 384 | } |
| 385 | if (ret == 0) { |
| 386 | /* The file was moved into place (not copied), so it's done. */ |
| 387 | return; |
| 388 | } |
| 389 | /* The file was copied, so tweak the perms of the copied file. If it |
| 390 | * was copied to partialptr, move it into its final destination. */ |
| 391 | fnametmp = partialptr ? partialptr : fname; |
| 392 | |
| 393 | do_set_file_attrs: |
| 394 | set_file_attrs(fnametmp, file, NULL, |
| 395 | ok_to_set_time ? 0 : ATTRS_SKIP_MTIME); |
| 396 | |
| 397 | if (partialptr) { |
| 398 | if (do_rename(fnametmp, fname) < 0) { |
| 399 | rsyserr(FERROR, errno, "rename %s -> \"%s\"", |
| 400 | full_fname(fnametmp), fname); |
| 401 | } else |
| 402 | handle_partial_dir(partialptr, PDIR_DELETE); |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | struct file_list *flist_for_ndx(int ndx) |
| 407 | { |
| 408 | struct file_list *flist = cur_flist; |
| 409 | |
| 410 | if (!flist) |
| 411 | return NULL; |
| 412 | |
| 413 | while (ndx < flist->ndx_start) { |
| 414 | if (flist == first_flist) |
| 415 | return NULL; |
| 416 | flist = flist->prev; |
| 417 | } |
| 418 | while (ndx >= flist->ndx_start + flist->count) { |
| 419 | if (!(flist = flist->next)) |
| 420 | return NULL; |
| 421 | } |
| 422 | return flist; |
| 423 | } |
| 424 | |
| 425 | const char *who_am_i(void) |
| 426 | { |
| 427 | if (am_starting_up) |
| 428 | return am_server ? "server" : "client"; |
| 429 | return am_sender ? "sender" : am_generator ? "generator" : "receiver"; |
| 430 | } |