Include 2008 in the copyright years.
[rsync/rsync.git] / rsync.c
... / ...
CommitLineData
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-2008 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 3 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, visit the http://fsf.org website.
20 */
21
22#include "rsync.h"
23#include "ifuncs.h"
24#if defined HAVE_LIBCHARSET_H && defined HAVE_LOCALE_CHARSET
25#include <libcharset.h>
26#elif defined HAVE_LANGINFO_H && defined HAVE_NL_LANGINFO
27#include <langinfo.h>
28#endif
29
30extern int verbose;
31extern int dry_run;
32extern int preserve_acls;
33extern int preserve_xattrs;
34extern int preserve_perms;
35extern int preserve_executability;
36extern int preserve_times;
37extern int am_root;
38extern int am_server;
39extern int am_sender;
40extern int am_generator;
41extern int am_starting_up;
42extern int allow_8bit_chars;
43extern int protocol_version;
44extern int receiver_symlink_times;
45extern int uid_ndx;
46extern int gid_ndx;
47extern int inc_recurse;
48extern int inplace;
49extern int flist_eof;
50extern int keep_dirlinks;
51extern int make_backups;
52extern struct file_list *cur_flist, *first_flist, *dir_flist;
53extern struct chmod_mode_struct *daemon_chmod_modes;
54#ifdef ICONV_OPTION
55extern char *iconv_opt;
56#endif
57
58#ifdef ICONV_CONST
59iconv_t ic_chck = (iconv_t)-1;
60# ifdef ICONV_OPTION
61iconv_t ic_send = (iconv_t)-1, ic_recv = (iconv_t)-1;
62# endif
63
64static const char *default_charset(void)
65{
66# if defined HAVE_LIBCHARSET_H && defined HAVE_LOCALE_CHARSET
67 return locale_charset();
68# elif defined HAVE_LANGINFO_H && defined HAVE_NL_LANGINFO
69 return nl_langinfo(CODESET);
70# else
71 return ""; /* Works with (at the very least) gnu iconv... */
72# endif
73}
74
75void setup_iconv(void)
76{
77 const char *defset = default_charset();
78# ifdef ICONV_OPTION
79 const char *charset;
80 char *cp;
81# endif
82
83 if (!am_server && !allow_8bit_chars) {
84
85 /* It's OK if this fails... */
86 ic_chck = iconv_open(defset, defset);
87
88 if (verbose > 3) {
89 if (ic_chck == (iconv_t)-1) {
90 rprintf(FINFO,
91 "note: iconv_open(\"%s\", \"%s\") failed (%d)"
92 " -- using isprint() instead of iconv().\n",
93 defset, defset, errno);
94 } else {
95 rprintf(FINFO,
96 "note: iconv_open(\"%s\", \"%s\") succeeded.\n",
97 defset, defset);
98 }
99 }
100 }
101
102# ifdef ICONV_OPTION
103 if (!iconv_opt)
104 return;
105
106 if ((cp = strchr(iconv_opt, ',')) != NULL) {
107 if (am_server) /* A local transfer needs this. */
108 iconv_opt = cp + 1;
109 else
110 *cp = '\0';
111 }
112
113 if (!*iconv_opt || (*iconv_opt == '.' && iconv_opt[1] == '\0'))
114 charset = defset;
115 else
116 charset = iconv_opt;
117
118 if ((ic_send = iconv_open(UTF8_CHARSET, charset)) == (iconv_t)-1) {
119 rprintf(FERROR, "iconv_open(\"%s\", \"%s\") failed\n",
120 UTF8_CHARSET, charset);
121 exit_cleanup(RERR_UNSUPPORTED);
122 }
123
124 if ((ic_recv = iconv_open(charset, UTF8_CHARSET)) == (iconv_t)-1) {
125 rprintf(FERROR, "iconv_open(\"%s\", \"%s\") failed\n",
126 charset, UTF8_CHARSET);
127 exit_cleanup(RERR_UNSUPPORTED);
128 }
129
130 if (verbose > 1) {
131 rprintf(FINFO, "%s charset: %s\n",
132 am_server ? "server" : "client",
133 *charset ? charset : "[LOCALE]");
134 }
135# endif
136}
137
138/* This function converts the characters in the "in" xbuf into characters
139 * in the "out" xbuf. The "len" of the "in" xbuf is used starting from its
140 * "pos". The "size" of the "out" xbuf restricts how many characters can be
141 * stored, starting at its "pos+len" position. Note that the last byte of
142 * the buffer is never used, which reserves space for a terminating '\0'.
143 * We return a 0 on success or a -1 on error. An error also sets errno to
144 * E2BIG, EILSEQ, or EINVAL (see below); otherwise errno will be set to 0.
145 * The "in" xbuf is altered to update "pos" and "len". The "out" xbuf has
146 * data appended, and its "len" incremented. If ICB_EXPAND_OUT is set in
147 * "flags", the "out" xbuf will also be allocated if empty, and expanded if
148 * too small (so E2BIG will not be returned). If ICB_INCLUDE_BAD is set in
149 * "flags", any badly-encoded chars are included verbatim in the "out" xbuf,
150 * so EILSEQ will not be returned. Likewise for ICB_INCLUDE_INCOMPLETE with
151 * respect to an incomplete multi-byte char at the end, which ensures that
152 * EINVAL is not returned. Anytime "in.pos" is 0 we will reset the iconv()
153 * state prior to processing the characters. */
154int iconvbufs(iconv_t ic, xbuf *in, xbuf *out, int flags)
155{
156 ICONV_CONST char *ibuf;
157 size_t icnt, ocnt;
158 char *obuf;
159
160 if (!out->size && flags & ICB_EXPAND_OUT)
161 alloc_xbuf(out, 1024);
162
163 if (!in->pos)
164 iconv(ic, NULL, 0, NULL, 0);
165
166 ibuf = in->buf + in->pos;
167 icnt = in->len;
168
169 obuf = out->buf + (out->pos + out->len);
170 ocnt = out->size - (out->pos + out->len) - 1;
171
172 while (icnt) {
173 while (iconv(ic, &ibuf, &icnt, &obuf, &ocnt) == (size_t)-1) {
174 if (errno == EINTR)
175 continue;
176 if (errno == EINVAL) {
177 if (!(flags & ICB_INCLUDE_INCOMPLETE))
178 goto finish;
179 } else if (errno == EILSEQ) {
180 if (!(flags & ICB_INCLUDE_BAD))
181 goto finish;
182 } else {
183 size_t opos = obuf - out->buf;
184 if (!(flags & ICB_EXPAND_OUT)) {
185 errno = E2BIG;
186 goto finish;
187 }
188 realloc_xbuf(out, out->size + 1024);
189 obuf = out->buf + opos;
190 ocnt += 1024;
191 continue;
192 }
193 *obuf++ = *ibuf++;
194 ocnt--, icnt--;
195 }
196 }
197
198 errno = 0;
199
200 finish:
201 in->len = icnt;
202 in->pos = ibuf - in->buf;
203 out->len = obuf - out->buf - out->pos;
204
205 return errno ? -1 : 0;
206}
207#endif
208
209int read_ndx_and_attrs(int f_in, int *iflag_ptr, uchar *type_ptr,
210 char *buf, int *len_ptr)
211{
212 int len, iflags = 0;
213 struct file_list *flist;
214 uchar fnamecmp_type = FNAMECMP_FNAME;
215 int ndx, save_verbose = verbose;
216
217 read_loop:
218 while (1) {
219 ndx = read_ndx(f_in);
220
221 if (ndx >= 0)
222 break;
223 if (ndx == NDX_DONE)
224 return ndx;
225 if (!inc_recurse || am_sender)
226 goto invalid_ndx;
227 if (ndx == NDX_FLIST_EOF) {
228 flist_eof = 1;
229 send_msg(MSG_FLIST_EOF, "", 0, 0);
230 continue;
231 }
232 ndx = NDX_FLIST_OFFSET - ndx;
233 if (ndx < 0 || ndx >= dir_flist->used) {
234 ndx = NDX_FLIST_OFFSET - ndx;
235 rprintf(FERROR,
236 "[%s] Invalid dir index: %d (%d - %d)\n",
237 who_am_i(), ndx, NDX_FLIST_OFFSET,
238 NDX_FLIST_OFFSET - dir_flist->used + 1);
239 exit_cleanup(RERR_PROTOCOL);
240 }
241
242 /* Send everything read from f_in to msg_fd_out. */
243 if (verbose > 3) {
244 rprintf(FINFO, "[%s] receiving flist for dir %d\n",
245 who_am_i(), ndx);
246 }
247 verbose = 0;
248 send_msg_int(MSG_FLIST, ndx);
249 start_flist_forward(f_in);
250 flist = recv_file_list(f_in);
251 flist->parent_ndx = ndx;
252 stop_flist_forward();
253 verbose = save_verbose;
254 }
255
256 iflags = protocol_version >= 29 ? read_shortint(f_in)
257 : ITEM_TRANSFER | ITEM_MISSING_DATA;
258
259 /* Honor the old-style keep-alive indicator. */
260 if (protocol_version < 30
261 && ndx == cur_flist->used && iflags == ITEM_IS_NEW) {
262 if (am_sender)
263 maybe_send_keepalive();
264 goto read_loop;
265 }
266
267 if (!(flist = flist_for_ndx(ndx))) {
268 int start, used;
269 invalid_ndx:
270 start = first_flist ? first_flist->ndx_start : 0;
271 used = first_flist ? first_flist->used : 0;
272 rprintf(FERROR,
273 "Invalid file index: %d (%d - %d) with iflags %x [%s]\n",
274 ndx, start - 1, start + used -1, iflags, who_am_i());
275 exit_cleanup(RERR_PROTOCOL);
276 }
277 cur_flist = flist;
278
279 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
280 fnamecmp_type = read_byte(f_in);
281 *type_ptr = fnamecmp_type;
282
283 if (iflags & ITEM_XNAME_FOLLOWS) {
284 if ((len = read_vstring(f_in, buf, MAXPATHLEN)) < 0)
285 exit_cleanup(RERR_PROTOCOL);
286 } else {
287 *buf = '\0';
288 len = -1;
289 }
290 *len_ptr = len;
291
292 if (iflags & ITEM_TRANSFER) {
293 int i = ndx - cur_flist->ndx_start;
294 if (i < 0 || !S_ISREG(cur_flist->files[i]->mode)) {
295 rprintf(FERROR,
296 "received request to transfer non-regular file: %d [%s]\n",
297 ndx, who_am_i());
298 exit_cleanup(RERR_PROTOCOL);
299 }
300 }
301
302 *iflag_ptr = iflags;
303 return ndx;
304}
305
306/*
307 free a sums struct
308 */
309void free_sums(struct sum_struct *s)
310{
311 if (s->sums) free(s->sums);
312 free(s);
313}
314
315/* This is only called when we aren't preserving permissions. Figure out what
316 * the permissions should be and return them merged back into the mode. */
317mode_t dest_mode(mode_t flist_mode, mode_t stat_mode, int dflt_perms,
318 int exists)
319{
320 int new_mode;
321 /* If the file already exists, we'll return the local permissions,
322 * possibly tweaked by the --executability option. */
323 if (exists) {
324 new_mode = (flist_mode & ~CHMOD_BITS) | (stat_mode & CHMOD_BITS);
325 if (preserve_executability && S_ISREG(flist_mode)) {
326 /* If the source file is executable, grant execute
327 * rights to everyone who can read, but ONLY if the
328 * file isn't already executable. */
329 if (!(flist_mode & 0111))
330 new_mode &= ~0111;
331 else if (!(stat_mode & 0111))
332 new_mode |= (new_mode & 0444) >> 2;
333 }
334 } else {
335 /* Apply destination default permissions and turn
336 * off special permissions. */
337 new_mode = flist_mode & (~CHMOD_BITS | dflt_perms);
338 }
339 return new_mode;
340}
341
342int set_file_attrs(const char *fname, struct file_struct *file, stat_x *sxp,
343 const char *fnamecmp, int flags)
344{
345 int updated = 0;
346 stat_x sx2;
347 int change_uid, change_gid;
348 mode_t new_mode = file->mode;
349 int inherit;
350
351 if (!sxp) {
352 if (dry_run)
353 return 1;
354 if (link_stat(fname, &sx2.st, 0) < 0) {
355 rsyserr(FERROR_XFER, errno, "stat %s failed",
356 full_fname(fname));
357 return 0;
358 }
359#ifdef SUPPORT_ACLS
360 sx2.acc_acl = sx2.def_acl = NULL;
361#endif
362#ifdef SUPPORT_XATTRS
363 sx2.xattr = NULL;
364#endif
365 sxp = &sx2;
366 inherit = !preserve_perms;
367 } else
368 inherit = !preserve_perms && file->flags & FLAG_DIR_CREATED;
369
370 if (inherit && S_ISDIR(new_mode) && sxp->st.st_mode & S_ISGID) {
371 /* We just created this directory and its setgid
372 * bit is on, so make sure it stays on. */
373 new_mode |= S_ISGID;
374 }
375
376 if (daemon_chmod_modes && !S_ISLNK(new_mode))
377 new_mode = tweak_mode(new_mode, daemon_chmod_modes);
378
379#ifdef SUPPORT_ACLS
380 if (preserve_acls && !S_ISLNK(file->mode) && !ACL_READY(*sxp))
381 get_acl(fname, sxp);
382#endif
383
384#ifdef SUPPORT_XATTRS
385 if (am_root < 0)
386 set_stat_xattr(fname, file, new_mode);
387 if (preserve_xattrs && fnamecmp)
388 set_xattr(fname, file, fnamecmp, sxp);
389#endif
390
391 if (!preserve_times || (S_ISDIR(sxp->st.st_mode) && preserve_times == 1))
392 flags |= ATTRS_SKIP_MTIME;
393 if (!(flags & ATTRS_SKIP_MTIME)
394 && cmp_time(sxp->st.st_mtime, file->modtime) != 0) {
395 int ret = set_modtime(fname, file->modtime, sxp->st.st_mode);
396 if (ret < 0) {
397 rsyserr(FERROR_XFER, errno, "failed to set times on %s",
398 full_fname(fname));
399 goto cleanup;
400 }
401 if (ret == 0) /* ret == 1 if symlink could not be set */
402 updated = 1;
403 else if (receiver_symlink_times)
404 file->flags |= FLAG_TIME_FAILED;
405 }
406
407 change_uid = am_root && uid_ndx && sxp->st.st_uid != (uid_t)F_OWNER(file);
408 change_gid = gid_ndx && !(file->flags & FLAG_SKIP_GROUP)
409 && sxp->st.st_gid != (gid_t)F_GROUP(file);
410#if !defined HAVE_LCHOWN && !defined CHOWN_MODIFIES_SYMLINK
411 if (S_ISLNK(sxp->st.st_mode)) {
412 ;
413 } else
414#endif
415 if (change_uid || change_gid) {
416 if (verbose > 2) {
417 if (change_uid) {
418 rprintf(FINFO,
419 "set uid of %s from %u to %u\n",
420 fname, (unsigned)sxp->st.st_uid, F_OWNER(file));
421 }
422 if (change_gid) {
423 rprintf(FINFO,
424 "set gid of %s from %u to %u\n",
425 fname, (unsigned)sxp->st.st_gid, F_GROUP(file));
426 }
427 }
428 if (am_root >= 0) {
429 if (do_lchown(fname,
430 change_uid ? (uid_t)F_OWNER(file) : sxp->st.st_uid,
431 change_gid ? (gid_t)F_GROUP(file) : sxp->st.st_gid) != 0) {
432 /* We shouldn't have attempted to change uid
433 * or gid unless have the privilege. */
434 rsyserr(FERROR_XFER, errno, "%s %s failed",
435 change_uid ? "chown" : "chgrp",
436 full_fname(fname));
437 goto cleanup;
438 }
439 /* A lchown had been done, so we need to re-stat if
440 * the destination had the setuid or setgid bits set
441 * (due to the side effect of the chown call). */
442 if (sxp->st.st_mode & (S_ISUID | S_ISGID)) {
443 link_stat(fname, &sxp->st,
444 keep_dirlinks && S_ISDIR(sxp->st.st_mode));
445 }
446 }
447 updated = 1;
448 }
449
450#ifdef SUPPORT_ACLS
451 /* It's OK to call set_acl() now, even for a dir, as the generator
452 * will enable owner-writability using chmod, if necessary.
453 *
454 * If set_acl() changes permission bits in the process of setting
455 * an access ACL, it changes sxp->st.st_mode so we know whether we
456 * need to chmod(). */
457 if (preserve_acls && !S_ISLNK(new_mode) && set_acl(fname, file, sxp) == 0)
458 updated = 1;
459#endif
460
461#ifdef HAVE_CHMOD
462 if (!BITS_EQUAL(sxp->st.st_mode, new_mode, CHMOD_BITS)) {
463 int ret = am_root < 0 ? 0 : do_chmod(fname, new_mode);
464 if (ret < 0) {
465 rsyserr(FERROR_XFER, errno,
466 "failed to set permissions on %s",
467 full_fname(fname));
468 goto cleanup;
469 }
470 if (ret == 0) /* ret == 1 if symlink could not be set */
471 updated = 1;
472 }
473#endif
474
475 if (verbose > 1 && flags & ATTRS_REPORT) {
476 if (updated)
477 rprintf(FCLIENT, "%s\n", fname);
478 else
479 rprintf(FCLIENT, "%s is uptodate\n", fname);
480 }
481 cleanup:
482 if (sxp == &sx2) {
483#ifdef SUPPORT_ACLS
484 if (preserve_acls)
485 free_acl(&sx2);
486#endif
487#ifdef SUPPORT_XATTRS
488 if (preserve_xattrs)
489 free_xattr(&sx2);
490#endif
491 }
492 return updated;
493}
494
495RETSIGTYPE sig_int(UNUSED(int val))
496{
497 /* KLUGE: if the user hits Ctrl-C while ssh is prompting
498 * for a password, then our cleanup's sending of a SIGUSR1
499 * signal to all our children may kill ssh before it has a
500 * chance to restore the tty settings (i.e. turn echo back
501 * on). By sleeping for a short time, ssh gets a bigger
502 * chance to do the right thing. If child processes are
503 * not ssh waiting for a password, then this tiny delay
504 * shouldn't hurt anything. */
505 msleep(400);
506 exit_cleanup(RERR_SIGNAL);
507}
508
509/* Finish off a file transfer: renaming the file and setting the file's
510 * attributes (e.g. permissions, ownership, etc.). If the robust_rename()
511 * call is forced to copy the temp file and partialptr is both non-NULL and
512 * not an absolute path, we stage the file into the partial-dir and then
513 * rename it into place. This returns 1 on succcess or 0 on failure. */
514int finish_transfer(const char *fname, const char *fnametmp,
515 const char *fnamecmp, const char *partialptr,
516 struct file_struct *file, int ok_to_set_time,
517 int overwriting_basis)
518{
519 int ret;
520 const char *temp_copy_name = partialptr && *partialptr != '/' ? partialptr : NULL;
521
522 if (inplace) {
523 if (verbose > 2)
524 rprintf(FINFO, "finishing %s\n", fname);
525 fnametmp = fname;
526 goto do_set_file_attrs;
527 }
528
529 if (make_backups > 0 && overwriting_basis && !make_backup(fname))
530 return 1;
531
532 /* Change permissions before putting the file into place. */
533 set_file_attrs(fnametmp, file, NULL, fnamecmp,
534 ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
535
536 /* move tmp file over real file */
537 if (verbose > 2)
538 rprintf(FINFO, "renaming %s to %s\n", fnametmp, fname);
539 ret = robust_rename(fnametmp, fname, temp_copy_name,
540 file->mode & INITACCESSPERMS);
541 if (ret < 0) {
542 rsyserr(FERROR_XFER, errno, "%s %s -> \"%s\"",
543 ret == -2 ? "copy" : "rename",
544 full_fname(fnametmp), fname);
545 if (!partialptr || (ret == -2 && temp_copy_name)
546 || robust_rename(fnametmp, partialptr, NULL,
547 file->mode & INITACCESSPERMS) < 0)
548 do_unlink(fnametmp);
549 return 0;
550 }
551 if (ret == 0) {
552 /* The file was moved into place (not copied), so it's done. */
553 return 1;
554 }
555 /* The file was copied, so tweak the perms of the copied file. If it
556 * was copied to partialptr, move it into its final destination. */
557 fnametmp = temp_copy_name ? temp_copy_name : fname;
558
559 do_set_file_attrs:
560 set_file_attrs(fnametmp, file, NULL, fnamecmp,
561 ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
562
563 if (temp_copy_name) {
564 if (do_rename(fnametmp, fname) < 0) {
565 rsyserr(FERROR_XFER, errno, "rename %s -> \"%s\"",
566 full_fname(fnametmp), fname);
567 return 0;
568 }
569 handle_partial_dir(temp_copy_name, PDIR_DELETE);
570 }
571 return 1;
572}
573
574struct file_list *flist_for_ndx(int ndx)
575{
576 struct file_list *flist = cur_flist;
577
578 if (!flist && !(flist = first_flist))
579 return NULL;
580
581 while (ndx < flist->ndx_start-1) {
582 if (flist == first_flist)
583 return NULL;
584 flist = flist->prev;
585 }
586 while (ndx >= flist->ndx_start + flist->used) {
587 if (!(flist = flist->next))
588 return NULL;
589 }
590 return flist;
591}
592
593const char *who_am_i(void)
594{
595 if (am_starting_up)
596 return am_server ? "server" : "client";
597 return am_sender ? "sender" : am_generator ? "generator" : "receiver";
598}