2 * Routines common to more than one of the rsync processes.
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2003-2009 Wayne Davison
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 3 of the License, or
11 * (at your option) any later version.
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.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, visit the http://fsf.org website.
24 #if defined HAVE_LIBCHARSET_H && defined HAVE_LOCALE_CHARSET
25 #include <libcharset.h>
26 #elif defined HAVE_LANGINFO_H && defined HAVE_NL_LANGINFO
31 extern int preserve_acls;
32 extern int preserve_xattrs;
33 extern int preserve_perms;
34 extern int preserve_executability;
35 extern int preserve_times;
39 extern int am_generator;
40 extern int am_starting_up;
41 extern int allow_8bit_chars;
42 extern int protocol_version;
45 extern int inc_recurse;
48 extern int file_old_total;
49 extern int keep_dirlinks;
50 extern int make_backups;
51 extern struct file_list *cur_flist, *first_flist, *dir_flist;
52 extern struct chmod_mode_struct *daemon_chmod_modes;
54 extern char *iconv_opt;
58 iconv_t ic_chck = (iconv_t)-1;
60 iconv_t ic_send = (iconv_t)-1, ic_recv = (iconv_t)-1;
63 static const char *default_charset(void)
65 # if defined HAVE_LIBCHARSET_H && defined HAVE_LOCALE_CHARSET
66 return locale_charset();
67 # elif defined HAVE_LANGINFO_H && defined HAVE_NL_LANGINFO
68 return nl_langinfo(CODESET);
70 return ""; /* Works with (at the very least) gnu iconv... */
74 void setup_iconv(void)
76 const char *defset = default_charset();
82 if (!am_server && !allow_8bit_chars) {
83 /* It's OK if this fails... */
84 ic_chck = iconv_open(defset, defset);
86 if (DEBUG_GTE(ICONV, 2)) {
87 if (ic_chck == (iconv_t)-1) {
89 "msg checking via isprint()"
90 " (iconv_open(\"%s\", \"%s\") errno: %d)\n",
91 defset, defset, errno);
94 "msg checking charset: %s\n",
99 ic_chck = (iconv_t)-1;
105 if ((cp = strchr(iconv_opt, ',')) != NULL) {
106 if (am_server) /* A local transfer needs this. */
112 if (!*iconv_opt || (*iconv_opt == '.' && iconv_opt[1] == '\0'))
117 if ((ic_send = iconv_open(UTF8_CHARSET, charset)) == (iconv_t)-1) {
118 rprintf(FERROR, "iconv_open(\"%s\", \"%s\") failed\n",
119 UTF8_CHARSET, charset);
120 exit_cleanup(RERR_UNSUPPORTED);
123 if ((ic_recv = iconv_open(charset, UTF8_CHARSET)) == (iconv_t)-1) {
124 rprintf(FERROR, "iconv_open(\"%s\", \"%s\") failed\n",
125 charset, UTF8_CHARSET);
126 exit_cleanup(RERR_UNSUPPORTED);
129 if (DEBUG_GTE(ICONV, 1)) {
130 rprintf(FINFO, "[%s] charset: %s\n",
131 who_am_i(), *charset ? charset : "[LOCALE]");
136 /* Move any bytes in the overflow space to the start. This avoids any issue
137 * with a multibyte sequence that needs to span the end of the buffer. */
138 static void wrap_overflow(xbuf *out, int siz)
140 if (DEBUG_GTE(IO, 4))
141 rprintf(FINFO, "[%s] wrap-bytes moved: %d (iconvbufs)\n", who_am_i(), siz);
142 memcpy(out->buf, out->buf + out->size, siz);
145 /* This function converts the characters in the "in" xbuf into characters
146 * in the "out" xbuf. The "len" of the "in" xbuf is used starting from its
147 * "pos". The "size" of the "out" xbuf restricts how many characters can be
148 * stored, starting at its "pos+len" position. Note that the last byte of
149 * the buffer is never used, which reserves space for a terminating '\0'.
150 * If ICB_CIRCULAR_OUT is set, the output data can wrap around to the start,
151 * and the buf IS ASSUMED TO HAVE AN EXTRA 4 BYTES OF OVERFLOW SPACE at the
152 * end (the buffer will also not be expanded if it is already allocated).
153 * We return a 0 on success or a -1 on error. An error also sets errno to
154 * E2BIG, EILSEQ, or EINVAL (see below); otherwise errno will be set to 0.
155 * The "in" xbuf is altered to update "pos" and "len". The "out" xbuf has
156 * data appended, and its "len" incremented. If ICB_EXPAND_OUT is set in
157 * "flags", the "out" xbuf will also be allocated if empty, and expanded if
158 * too small (so E2BIG will not be returned). If ICB_INCLUDE_BAD is set in
159 * "flags", any badly-encoded chars are included verbatim in the "out" xbuf,
160 * so EILSEQ will not be returned. Likewise for ICB_INCLUDE_INCOMPLETE with
161 * respect to an incomplete multi-byte char at the end, which ensures that
162 * EINVAL is not returned. If ICB_INIT is set, the iconv() conversion state
163 * is initialized prior to processing the characters. */
164 int iconvbufs(iconv_t ic, xbuf *in, xbuf *out, int flags)
166 ICONV_CONST char *ibuf;
167 size_t icnt, ocnt, opos;
170 if (!out->size && flags & ICB_EXPAND_OUT)
171 alloc_xbuf(out, 1024);
173 if (flags & ICB_INIT)
174 iconv(ic, NULL, 0, NULL, 0);
176 ibuf = in->buf + in->pos;
179 opos = out->pos + out->len;
180 if (flags & ICB_CIRCULAR_OUT) {
181 if (opos >= out->size) {
183 ocnt = out->pos - opos - 1;
185 /* We only make use of the 4 bytes of overflow buffer
186 * if there is room to move the bytes to the start of
187 * the circular buffer. */
188 ocnt = out->size - opos + MIN((ssize_t)out->pos - 1, 4);
191 ocnt = out->size - opos - 1;
192 obuf = out->buf + opos;
195 while (iconv(ic, &ibuf, &icnt, &obuf, &ocnt) == (size_t)-1) {
198 if (errno == EINVAL) {
199 if (!(flags & ICB_INCLUDE_INCOMPLETE))
201 } else if (errno == EILSEQ) {
202 if (!(flags & ICB_INCLUDE_BAD))
205 opos = obuf - out->buf;
206 if (flags & ICB_CIRCULAR_OUT && opos > out->size) {
207 wrap_overflow(out, opos -= out->size);
208 obuf = out->buf + opos;
209 if ((ocnt = out->pos - opos - 1) > 0)
212 if (!(flags & ICB_EXPAND_OUT) || flags & ICB_CIRCULAR_OUT) {
216 realloc_xbuf(out, out->size + 1024);
217 obuf = out->buf + opos;
229 opos = obuf - out->buf;
230 if (flags & ICB_CIRCULAR_OUT) {
231 if (opos > out->size)
232 wrap_overflow(out, opos - out->size);
233 else if (opos < out->pos)
236 out->len = opos - out->pos;
239 in->pos = ibuf - in->buf;
241 return errno ? -1 : 0;
245 void send_protected_args(int fd, char *args[])
249 int convert = ic_send != (iconv_t)-1;
253 alloc_xbuf(&outbuf, 1024);
256 for (i = 0; args[i]; i++) {} /* find first NULL */
257 args[i] = "rsync"; /* set a new arg0 */
258 if (DEBUG_GTE(CMD, 1))
259 print_child_argv("protected args:", args + i + 1);
262 write_buf(fd, ".", 2);
265 INIT_XBUF_STRLEN(inbuf, args[i]);
266 iconvbufs(ic_send, &inbuf, &outbuf,
267 ICB_EXPAND_OUT | ICB_INCLUDE_BAD | ICB_INCLUDE_INCOMPLETE | ICB_INIT);
268 outbuf.buf[outbuf.len] = '\0';
269 write_buf(fd, outbuf.buf, outbuf.len + 1);
274 write_buf(fd, args[i], strlen(args[i]) + 1);
284 int read_ndx_and_attrs(int f_in, int f_out, int *iflag_ptr, uchar *type_ptr,
285 char *buf, int *len_ptr)
288 struct file_list *flist;
289 uchar fnamecmp_type = FNAMECMP_FNAME;
294 ndx = read_ndx(f_in);
300 if (ndx == NDX_DEL_STATS) {
301 read_del_stats(f_in);
302 if (am_sender && am_server)
303 write_del_stats(f_out);
306 if (!inc_recurse || am_sender) {
309 last = first_flist->prev->ndx_start + first_flist->prev->used - 1;
313 "Invalid file index: %d (%d - %d) [%s]\n",
314 ndx, NDX_DONE, last, who_am_i());
315 exit_cleanup(RERR_PROTOCOL);
317 if (ndx == NDX_FLIST_EOF) {
319 if (DEBUG_GTE(FLIST, 3))
320 rprintf(FINFO, "[%s] flist_eof=1\n", who_am_i());
321 write_int(f_out, NDX_FLIST_EOF);
324 ndx = NDX_FLIST_OFFSET - ndx;
325 if (ndx < 0 || ndx >= dir_flist->used) {
326 ndx = NDX_FLIST_OFFSET - ndx;
328 "Invalid dir index: %d (%d - %d) [%s]\n",
329 ndx, NDX_FLIST_OFFSET,
330 NDX_FLIST_OFFSET - dir_flist->used + 1,
332 exit_cleanup(RERR_PROTOCOL);
335 if (DEBUG_GTE(FLIST, 2)) {
336 rprintf(FINFO, "[%s] receiving flist for dir %d\n",
339 /* Send all the data we read for this flist to the generator. */
340 start_flist_forward(ndx);
341 flist = recv_file_list(f_in);
342 flist->parent_ndx = ndx;
343 stop_flist_forward();
346 iflags = protocol_version >= 29 ? read_shortint(f_in)
347 : ITEM_TRANSFER | ITEM_MISSING_DATA;
349 /* Honor the old-style keep-alive indicator. */
350 if (protocol_version < 30
351 && ndx == cur_flist->used && iflags == ITEM_IS_NEW) {
353 maybe_send_keepalive();
357 flist = flist_for_ndx(ndx, "read_ndx_and_attrs");
358 if (flist != cur_flist) {
361 file_old_total = cur_flist->used;
362 for (flist = first_flist; flist != cur_flist; flist = flist->next)
363 file_old_total += flist->used;
367 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
368 fnamecmp_type = read_byte(f_in);
369 *type_ptr = fnamecmp_type;
371 if (iflags & ITEM_XNAME_FOLLOWS) {
372 if ((len = read_vstring(f_in, buf, MAXPATHLEN)) < 0)
373 exit_cleanup(RERR_PROTOCOL);
380 if (iflags & ITEM_TRANSFER) {
381 int i = ndx - cur_flist->ndx_start;
382 if (i < 0 || !S_ISREG(cur_flist->files[i]->mode)) {
384 "received request to transfer non-regular file: %d [%s]\n",
386 exit_cleanup(RERR_PROTOCOL);
397 void free_sums(struct sum_struct *s)
399 if (s->sums) free(s->sums);
403 /* This is only called when we aren't preserving permissions. Figure out what
404 * the permissions should be and return them merged back into the mode. */
405 mode_t dest_mode(mode_t flist_mode, mode_t stat_mode, int dflt_perms,
409 /* If the file already exists, we'll return the local permissions,
410 * possibly tweaked by the --executability option. */
412 new_mode = (flist_mode & ~CHMOD_BITS) | (stat_mode & CHMOD_BITS);
413 if (preserve_executability && S_ISREG(flist_mode)) {
414 /* If the source file is executable, grant execute
415 * rights to everyone who can read, but ONLY if the
416 * file isn't already executable. */
417 if (!(flist_mode & 0111))
419 else if (!(stat_mode & 0111))
420 new_mode |= (new_mode & 0444) >> 2;
423 /* Apply destination default permissions and turn
424 * off special permissions. */
425 new_mode = flist_mode & (~CHMOD_BITS | dflt_perms);
430 int set_file_attrs(const char *fname, struct file_struct *file, stat_x *sxp,
431 const char *fnamecmp, int flags)
435 int change_uid, change_gid;
436 mode_t new_mode = file->mode;
442 if (link_stat(fname, &sx2.st, 0) < 0) {
443 rsyserr(FERROR_XFER, errno, "stat %s failed",
449 inherit = !preserve_perms;
451 inherit = !preserve_perms && file->flags & FLAG_DIR_CREATED;
453 if (inherit && S_ISDIR(new_mode) && sxp->st.st_mode & S_ISGID) {
454 /* We just created this directory and its setgid
455 * bit is on, so make sure it stays on. */
459 if (daemon_chmod_modes && !S_ISLNK(new_mode))
460 new_mode = tweak_mode(new_mode, daemon_chmod_modes);
463 if (preserve_acls && !S_ISLNK(file->mode) && !ACL_READY(*sxp))
467 #ifdef SUPPORT_XATTRS
469 set_stat_xattr(fname, file, new_mode);
470 if (preserve_xattrs && fnamecmp)
471 set_xattr(fname, file, fnamecmp, sxp);
474 if (!preserve_times || (S_ISDIR(sxp->st.st_mode) && preserve_times == 1))
475 flags |= ATTRS_SKIP_MTIME;
476 if (!(flags & ATTRS_SKIP_MTIME)
477 && cmp_time(sxp->st.st_mtime, file->modtime) != 0) {
478 int ret = set_modtime(fname, file->modtime, F_MOD_NSEC(file), sxp->st.st_mode);
480 rsyserr(FERROR_XFER, errno, "failed to set times on %s",
484 if (ret == 0) /* ret == 1 if symlink could not be set */
487 file->flags |= FLAG_TIME_FAILED;
490 change_uid = am_root && uid_ndx && sxp->st.st_uid != (uid_t)F_OWNER(file);
491 change_gid = gid_ndx && !(file->flags & FLAG_SKIP_GROUP)
492 && sxp->st.st_gid != (gid_t)F_GROUP(file);
493 #if !defined HAVE_LCHOWN && !defined CHOWN_MODIFIES_SYMLINK
494 if (S_ISLNK(sxp->st.st_mode)) {
498 if (change_uid || change_gid) {
499 if (DEBUG_GTE(OWN, 1)) {
502 "set uid of %s from %u to %u\n",
503 fname, (unsigned)sxp->st.st_uid, F_OWNER(file));
507 "set gid of %s from %u to %u\n",
508 fname, (unsigned)sxp->st.st_gid, F_GROUP(file));
513 change_uid ? (uid_t)F_OWNER(file) : sxp->st.st_uid,
514 change_gid ? (gid_t)F_GROUP(file) : sxp->st.st_gid) != 0) {
515 /* We shouldn't have attempted to change uid
516 * or gid unless have the privilege. */
517 rsyserr(FERROR_XFER, errno, "%s %s failed",
518 change_uid ? "chown" : "chgrp",
522 /* A lchown had been done, so we need to re-stat if
523 * the destination had the setuid or setgid bits set
524 * (due to the side effect of the chown call). */
525 if (sxp->st.st_mode & (S_ISUID | S_ISGID)) {
526 link_stat(fname, &sxp->st,
527 keep_dirlinks && S_ISDIR(sxp->st.st_mode));
534 /* It's OK to call set_acl() now, even for a dir, as the generator
535 * will enable owner-writability using chmod, if necessary.
537 * If set_acl() changes permission bits in the process of setting
538 * an access ACL, it changes sxp->st.st_mode so we know whether we
539 * need to chmod(). */
540 if (preserve_acls && !S_ISLNK(new_mode)) {
541 if (set_acl(fname, file, sxp, new_mode) > 0)
547 if (!BITS_EQUAL(sxp->st.st_mode, new_mode, CHMOD_BITS)) {
548 int ret = am_root < 0 ? 0 : do_chmod(fname, new_mode);
550 rsyserr(FERROR_XFER, errno,
551 "failed to set permissions on %s",
555 if (ret == 0) /* ret == 1 if symlink could not be set */
560 if (INFO_GTE(NAME, 2) && flags & ATTRS_REPORT) {
562 rprintf(FCLIENT, "%s\n", fname);
564 rprintf(FCLIENT, "%s is uptodate\n", fname);
572 #ifdef SUPPORT_XATTRS
580 RETSIGTYPE sig_int(UNUSED(int val))
582 /* KLUGE: if the user hits Ctrl-C while ssh is prompting
583 * for a password, then our cleanup's sending of a SIGUSR1
584 * signal to all our children may kill ssh before it has a
585 * chance to restore the tty settings (i.e. turn echo back
586 * on). By sleeping for a short time, ssh gets a bigger
587 * chance to do the right thing. If child processes are
588 * not ssh waiting for a password, then this tiny delay
589 * shouldn't hurt anything. */
591 exit_cleanup(RERR_SIGNAL);
594 /* Finish off a file transfer: renaming the file and setting the file's
595 * attributes (e.g. permissions, ownership, etc.). If the robust_rename()
596 * call is forced to copy the temp file and partialptr is both non-NULL and
597 * not an absolute path, we stage the file into the partial-dir and then
598 * rename it into place. This returns 1 on succcess or 0 on failure. */
599 int finish_transfer(const char *fname, const char *fnametmp,
600 const char *fnamecmp, const char *partialptr,
601 struct file_struct *file, int ok_to_set_time,
602 int overwriting_basis)
605 const char *temp_copy_name = partialptr && *partialptr != '/' ? partialptr : NULL;
608 if (DEBUG_GTE(RECV, 1))
609 rprintf(FINFO, "finishing %s\n", fname);
611 goto do_set_file_attrs;
614 if (make_backups > 0 && overwriting_basis) {
615 int ok = make_backup(fname, False);
618 if (ok == 1 && fnamecmp == fname)
619 fnamecmp = get_backup_name(fname);
622 /* Change permissions before putting the file into place. */
623 set_file_attrs(fnametmp, file, NULL, fnamecmp,
624 ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
626 /* move tmp file over real file */
627 if (DEBUG_GTE(RECV, 1))
628 rprintf(FINFO, "renaming %s to %s\n", fnametmp, fname);
629 ret = robust_rename(fnametmp, fname, temp_copy_name,
630 file->mode & INITACCESSPERMS);
632 rsyserr(FERROR_XFER, errno, "%s %s -> \"%s\"",
633 ret == -2 ? "copy" : "rename",
634 full_fname(fnametmp), fname);
635 if (!partialptr || (ret == -2 && temp_copy_name)
636 || robust_rename(fnametmp, partialptr, NULL,
637 file->mode & INITACCESSPERMS) < 0)
642 /* The file was moved into place (not copied), so it's done. */
645 /* The file was copied, so tweak the perms of the copied file. If it
646 * was copied to partialptr, move it into its final destination. */
647 fnametmp = temp_copy_name ? temp_copy_name : fname;
650 set_file_attrs(fnametmp, file, NULL, fnamecmp,
651 ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
653 if (temp_copy_name) {
654 if (do_rename(fnametmp, fname) < 0) {
655 rsyserr(FERROR_XFER, errno, "rename %s -> \"%s\"",
656 full_fname(fnametmp), fname);
659 handle_partial_dir(temp_copy_name, PDIR_DELETE);
664 struct file_list *flist_for_ndx(int ndx, const char *fatal_error_loc)
666 struct file_list *flist = cur_flist;
668 if (!flist && !(flist = first_flist))
671 while (ndx < flist->ndx_start-1) {
672 if (flist == first_flist)
676 while (ndx >= flist->ndx_start + flist->used) {
677 if (!(flist = flist->next))
683 if (fatal_error_loc) {
686 first = first_flist->ndx_start - 1;
687 last = first_flist->prev->ndx_start + first_flist->prev->used - 1;
693 "File-list index %d not in %d - %d (%s) [%s]\n",
694 ndx, first, last, fatal_error_loc, who_am_i());
695 exit_cleanup(RERR_PROTOCOL);
700 const char *who_am_i(void)
703 return am_server ? "server" : "client";
704 return am_sender ? "sender" : am_generator ? "generator" : "receiver";