Made an error of readlink_stat() use the right function name.
[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 dry_run;
31extern int preserve_acls;
32extern int preserve_xattrs;
33extern int preserve_perms;
34extern int preserve_executability;
35extern int preserve_times;
36extern int am_root;
37extern int am_server;
38extern int am_sender;
39extern int am_generator;
40extern int am_starting_up;
41extern int allow_8bit_chars;
42extern int protocol_version;
43extern int uid_ndx;
44extern int gid_ndx;
45extern int inc_recurse;
46extern int inplace;
47extern int flist_eof;
48extern int msgs2stderr;
49extern int keep_dirlinks;
50extern int make_backups;
51extern int delete_during;
52extern int check_for_io_err;
53extern struct file_list *cur_flist, *first_flist, *dir_flist;
54extern struct chmod_mode_struct *daemon_chmod_modes;
55#ifdef ICONV_OPTION
56extern char *iconv_opt;
57#endif
58
59#ifdef ICONV_CONST
60iconv_t ic_chck = (iconv_t)-1;
61# ifdef ICONV_OPTION
62iconv_t ic_send = (iconv_t)-1, ic_recv = (iconv_t)-1;
63# endif
64
65static const char *default_charset(void)
66{
67# if defined HAVE_LIBCHARSET_H && defined HAVE_LOCALE_CHARSET
68 return locale_charset();
69# elif defined HAVE_LANGINFO_H && defined HAVE_NL_LANGINFO
70 return nl_langinfo(CODESET);
71# else
72 return ""; /* Works with (at the very least) gnu iconv... */
73# endif
74}
75
76void setup_iconv(void)
77{
78 const char *defset = default_charset();
79# ifdef ICONV_OPTION
80 const char *charset;
81 char *cp;
82# endif
83
84 if (!am_server && !allow_8bit_chars) {
85 /* It's OK if this fails... */
86 ic_chck = iconv_open(defset, defset);
87
88 if (DEBUG_GTE(ICONV, 2)) {
89 if (ic_chck == (iconv_t)-1) {
90 rprintf(FINFO,
91 "msg checking via isprint()"
92 " (iconv_open(\"%s\", \"%s\") errno: %d)\n",
93 defset, defset, errno);
94 } else {
95 rprintf(FINFO,
96 "msg checking charset: %s\n",
97 defset);
98 }
99 }
100 } else
101 ic_chck = (iconv_t)-1;
102
103# ifdef ICONV_OPTION
104 if (!iconv_opt)
105 return;
106
107 if ((cp = strchr(iconv_opt, ',')) != NULL) {
108 if (am_server) /* A local transfer needs this. */
109 iconv_opt = cp + 1;
110 else
111 *cp = '\0';
112 }
113
114 if (!*iconv_opt || (*iconv_opt == '.' && iconv_opt[1] == '\0'))
115 charset = defset;
116 else
117 charset = iconv_opt;
118
119 if ((ic_send = iconv_open(UTF8_CHARSET, charset)) == (iconv_t)-1) {
120 rprintf(FERROR, "iconv_open(\"%s\", \"%s\") failed\n",
121 UTF8_CHARSET, charset);
122 exit_cleanup(RERR_UNSUPPORTED);
123 }
124
125 if ((ic_recv = iconv_open(charset, UTF8_CHARSET)) == (iconv_t)-1) {
126 rprintf(FERROR, "iconv_open(\"%s\", \"%s\") failed\n",
127 charset, UTF8_CHARSET);
128 exit_cleanup(RERR_UNSUPPORTED);
129 }
130
131 if (DEBUG_GTE(ICONV, 1)) {
132 rprintf(FINFO, "[%s] charset: %s\n",
133 who_am_i(), *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
209void send_protected_args(int fd, char *args[])
210{
211 int i;
212#ifdef ICONV_OPTION
213 int convert = ic_send != (iconv_t)-1;
214 xbuf outbuf, inbuf;
215
216 if (convert)
217 alloc_xbuf(&outbuf, 1024);
218#endif
219
220 for (i = 0; args[i]; i++) {} /* find first NULL */
221 args[i] = "rsync"; /* set a new arg0 */
222 if (DEBUG_GTE(CMD, 1))
223 print_child_argv("protected args:", args + i + 1);
224 do {
225#ifdef ICONV_OPTION
226 if (convert) {
227 INIT_XBUF_STRLEN(inbuf, args[i]);
228 iconvbufs(ic_send, &inbuf, &outbuf,
229 ICB_EXPAND_OUT | ICB_INCLUDE_BAD | ICB_INCLUDE_INCOMPLETE);
230 outbuf.buf[outbuf.len] = '\0';
231 write_buf(fd, outbuf.buf, outbuf.len + 1);
232 outbuf.len = 0;
233 } else
234#endif
235 write_buf(fd, args[i], strlen(args[i]) + 1);
236 } while (args[++i]);
237 write_byte(fd, 0);
238
239#ifdef ICONV_OPTION
240 if (convert)
241 free(outbuf.buf);
242#endif
243}
244
245int read_ndx_and_attrs(int f_in, int *iflag_ptr, uchar *type_ptr,
246 char *buf, int *len_ptr)
247{
248 int len, iflags = 0;
249 struct file_list *flist;
250 uchar fnamecmp_type = FNAMECMP_FNAME;
251 int ndx;
252
253 read_loop:
254 while (1) {
255 ndx = read_ndx(f_in);
256
257 if (ndx >= 0) {
258 if (check_for_io_err) {
259 /* Let generator know there was no I/O error. */
260 send_msg_int(MSG_IO_ERROR, 0);
261 check_for_io_err = 0;
262 }
263 break;
264 }
265 check_for_io_err = 0;
266 if (ndx == NDX_DONE)
267 return ndx;
268 if (!inc_recurse || am_sender)
269 goto invalid_ndx;
270 if (ndx == NDX_FLIST_EOF) {
271 flist_eof = 1;
272 send_msg(MSG_FLIST_EOF, "", 0, 0);
273 continue;
274 }
275 ndx = NDX_FLIST_OFFSET - ndx;
276 if (ndx < 0 || ndx >= dir_flist->used) {
277 ndx = NDX_FLIST_OFFSET - ndx;
278 rprintf(FERROR,
279 "[%s] Invalid dir index: %d (%d - %d)\n",
280 who_am_i(), ndx, NDX_FLIST_OFFSET,
281 NDX_FLIST_OFFSET - dir_flist->used + 1);
282 exit_cleanup(RERR_PROTOCOL);
283 }
284
285 /* Send everything read from f_in to msg_fd_out. */
286 if (DEBUG_GTE(FLIST, 2)) {
287 rprintf(FINFO, "[%s] receiving flist for dir %d\n",
288 who_am_i(), ndx);
289 }
290 if (!msgs2stderr)
291 negate_output_levels(); /* turn off all info/debug output */
292 send_msg_int(MSG_FLIST, ndx);
293 start_flist_forward(f_in);
294 flist = recv_file_list(f_in);
295 flist->parent_ndx = ndx;
296 stop_flist_forward();
297 if (!msgs2stderr)
298 negate_output_levels(); /* restore info/debug output */
299 /* If the sender is going to send us an MSG_IO_ERROR value, it
300 * will always be the very next message following a file list. */
301 if (delete_during)
302 check_for_io_err = 1;
303 }
304
305 iflags = protocol_version >= 29 ? read_shortint(f_in)
306 : ITEM_TRANSFER | ITEM_MISSING_DATA;
307
308 /* Honor the old-style keep-alive indicator. */
309 if (protocol_version < 30
310 && ndx == cur_flist->used && iflags == ITEM_IS_NEW) {
311 if (am_sender)
312 maybe_send_keepalive();
313 goto read_loop;
314 }
315
316 if (!(flist = flist_for_ndx(ndx))) {
317 int start, used;
318 invalid_ndx:
319 start = first_flist ? first_flist->ndx_start : 0;
320 used = first_flist ? first_flist->used : 0;
321 rprintf(FERROR,
322 "Invalid file index: %d (%d - %d) with iflags %x [%s]\n",
323 ndx, start - 1, start + used -1, iflags, who_am_i());
324 exit_cleanup(RERR_PROTOCOL);
325 }
326 cur_flist = flist;
327
328 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
329 fnamecmp_type = read_byte(f_in);
330 *type_ptr = fnamecmp_type;
331
332 if (iflags & ITEM_XNAME_FOLLOWS) {
333 if ((len = read_vstring(f_in, buf, MAXPATHLEN)) < 0)
334 exit_cleanup(RERR_PROTOCOL);
335 } else {
336 *buf = '\0';
337 len = -1;
338 }
339 *len_ptr = len;
340
341 if (iflags & ITEM_TRANSFER) {
342 int i = ndx - cur_flist->ndx_start;
343 if (i < 0 || !S_ISREG(cur_flist->files[i]->mode)) {
344 rprintf(FERROR,
345 "received request to transfer non-regular file: %d [%s]\n",
346 ndx, who_am_i());
347 exit_cleanup(RERR_PROTOCOL);
348 }
349 }
350
351 *iflag_ptr = iflags;
352 return ndx;
353}
354
355/*
356 free a sums struct
357 */
358void free_sums(struct sum_struct *s)
359{
360 if (s->sums) free(s->sums);
361 free(s);
362}
363
364/* This is only called when we aren't preserving permissions. Figure out what
365 * the permissions should be and return them merged back into the mode. */
366mode_t dest_mode(mode_t flist_mode, mode_t stat_mode, int dflt_perms,
367 int exists)
368{
369 int new_mode;
370 /* If the file already exists, we'll return the local permissions,
371 * possibly tweaked by the --executability option. */
372 if (exists) {
373 new_mode = (flist_mode & ~CHMOD_BITS) | (stat_mode & CHMOD_BITS);
374 if (preserve_executability && S_ISREG(flist_mode)) {
375 /* If the source file is executable, grant execute
376 * rights to everyone who can read, but ONLY if the
377 * file isn't already executable. */
378 if (!(flist_mode & 0111))
379 new_mode &= ~0111;
380 else if (!(stat_mode & 0111))
381 new_mode |= (new_mode & 0444) >> 2;
382 }
383 } else {
384 /* Apply destination default permissions and turn
385 * off special permissions. */
386 new_mode = flist_mode & (~CHMOD_BITS | dflt_perms);
387 }
388 return new_mode;
389}
390
391int set_file_attrs(const char *fname, struct file_struct *file, stat_x *sxp,
392 const char *fnamecmp, int flags)
393{
394 int updated = 0;
395 stat_x sx2;
396 int change_uid, change_gid;
397 mode_t new_mode = file->mode;
398 int inherit;
399
400 if (!sxp) {
401 if (dry_run)
402 return 1;
403 if (link_stat(fname, &sx2.st, 0) < 0) {
404 rsyserr(FERROR_XFER, errno, "stat %s failed",
405 full_fname(fname));
406 return 0;
407 }
408#ifdef SUPPORT_ACLS
409 sx2.acc_acl = sx2.def_acl = NULL;
410#endif
411#ifdef SUPPORT_XATTRS
412 sx2.xattr = NULL;
413#endif
414 sxp = &sx2;
415 inherit = !preserve_perms;
416 } else
417 inherit = !preserve_perms && file->flags & FLAG_DIR_CREATED;
418
419 if (inherit && S_ISDIR(new_mode) && sxp->st.st_mode & S_ISGID) {
420 /* We just created this directory and its setgid
421 * bit is on, so make sure it stays on. */
422 new_mode |= S_ISGID;
423 }
424
425 if (daemon_chmod_modes && !S_ISLNK(new_mode))
426 new_mode = tweak_mode(new_mode, daemon_chmod_modes);
427
428#ifdef SUPPORT_ACLS
429 if (preserve_acls && !S_ISLNK(file->mode) && !ACL_READY(*sxp))
430 get_acl(fname, sxp);
431#endif
432
433#ifdef SUPPORT_XATTRS
434 if (am_root < 0)
435 set_stat_xattr(fname, file, new_mode);
436 if (preserve_xattrs && fnamecmp)
437 set_xattr(fname, file, fnamecmp, sxp);
438#endif
439
440 if (!preserve_times || (S_ISDIR(sxp->st.st_mode) && preserve_times == 1))
441 flags |= ATTRS_SKIP_MTIME;
442 if (!(flags & ATTRS_SKIP_MTIME)
443 && cmp_time(sxp->st.st_mtime, file->modtime) != 0) {
444 int ret = set_modtime(fname, file->modtime, sxp->st.st_mode);
445 if (ret < 0) {
446 rsyserr(FERROR_XFER, errno, "failed to set times on %s",
447 full_fname(fname));
448 goto cleanup;
449 }
450 if (ret == 0) /* ret == 1 if symlink could not be set */
451 updated = 1;
452 else
453 file->flags |= FLAG_TIME_FAILED;
454 }
455
456 change_uid = am_root && uid_ndx && sxp->st.st_uid != (uid_t)F_OWNER(file);
457 change_gid = gid_ndx && !(file->flags & FLAG_SKIP_GROUP)
458 && sxp->st.st_gid != (gid_t)F_GROUP(file);
459#if !defined HAVE_LCHOWN && !defined CHOWN_MODIFIES_SYMLINK
460 if (S_ISLNK(sxp->st.st_mode)) {
461 ;
462 } else
463#endif
464 if (change_uid || change_gid) {
465 if (DEBUG_GTE(OWN, 1)) {
466 if (change_uid) {
467 rprintf(FINFO,
468 "set uid of %s from %u to %u\n",
469 fname, (unsigned)sxp->st.st_uid, F_OWNER(file));
470 }
471 if (change_gid) {
472 rprintf(FINFO,
473 "set gid of %s from %u to %u\n",
474 fname, (unsigned)sxp->st.st_gid, F_GROUP(file));
475 }
476 }
477 if (am_root >= 0) {
478 if (do_lchown(fname,
479 change_uid ? (uid_t)F_OWNER(file) : sxp->st.st_uid,
480 change_gid ? (gid_t)F_GROUP(file) : sxp->st.st_gid) != 0) {
481 /* We shouldn't have attempted to change uid
482 * or gid unless have the privilege. */
483 rsyserr(FERROR_XFER, errno, "%s %s failed",
484 change_uid ? "chown" : "chgrp",
485 full_fname(fname));
486 goto cleanup;
487 }
488 /* A lchown had been done, so we need to re-stat if
489 * the destination had the setuid or setgid bits set
490 * (due to the side effect of the chown call). */
491 if (sxp->st.st_mode & (S_ISUID | S_ISGID)) {
492 link_stat(fname, &sxp->st,
493 keep_dirlinks && S_ISDIR(sxp->st.st_mode));
494 }
495 }
496 updated = 1;
497 }
498
499#ifdef SUPPORT_ACLS
500 /* It's OK to call set_acl() now, even for a dir, as the generator
501 * will enable owner-writability using chmod, if necessary.
502 *
503 * If set_acl() changes permission bits in the process of setting
504 * an access ACL, it changes sxp->st.st_mode so we know whether we
505 * need to chmod(). */
506 if (preserve_acls && !S_ISLNK(new_mode) && set_acl(fname, file, sxp) == 0)
507 updated = 1;
508#endif
509
510#ifdef HAVE_CHMOD
511 if (!BITS_EQUAL(sxp->st.st_mode, new_mode, CHMOD_BITS)) {
512 int ret = am_root < 0 ? 0 : do_chmod(fname, new_mode);
513 if (ret < 0) {
514 rsyserr(FERROR_XFER, errno,
515 "failed to set permissions on %s",
516 full_fname(fname));
517 goto cleanup;
518 }
519 if (ret == 0) /* ret == 1 if symlink could not be set */
520 updated = 1;
521 }
522#endif
523
524 if (INFO_GTE(NAME, 2) && flags & ATTRS_REPORT) {
525 if (updated)
526 rprintf(FCLIENT, "%s\n", fname);
527 else
528 rprintf(FCLIENT, "%s is uptodate\n", fname);
529 }
530 cleanup:
531 if (sxp == &sx2) {
532#ifdef SUPPORT_ACLS
533 if (preserve_acls)
534 free_acl(&sx2);
535#endif
536#ifdef SUPPORT_XATTRS
537 if (preserve_xattrs)
538 free_xattr(&sx2);
539#endif
540 }
541 return updated;
542}
543
544RETSIGTYPE sig_int(UNUSED(int val))
545{
546 /* KLUGE: if the user hits Ctrl-C while ssh is prompting
547 * for a password, then our cleanup's sending of a SIGUSR1
548 * signal to all our children may kill ssh before it has a
549 * chance to restore the tty settings (i.e. turn echo back
550 * on). By sleeping for a short time, ssh gets a bigger
551 * chance to do the right thing. If child processes are
552 * not ssh waiting for a password, then this tiny delay
553 * shouldn't hurt anything. */
554 msleep(400);
555 exit_cleanup(RERR_SIGNAL);
556}
557
558/* Finish off a file transfer: renaming the file and setting the file's
559 * attributes (e.g. permissions, ownership, etc.). If the robust_rename()
560 * call is forced to copy the temp file and partialptr is both non-NULL and
561 * not an absolute path, we stage the file into the partial-dir and then
562 * rename it into place. This returns 1 on succcess or 0 on failure. */
563int finish_transfer(const char *fname, const char *fnametmp,
564 const char *fnamecmp, const char *partialptr,
565 struct file_struct *file, int ok_to_set_time,
566 int overwriting_basis)
567{
568 int ret;
569 const char *temp_copy_name = partialptr && *partialptr != '/' ? partialptr : NULL;
570
571 if (inplace) {
572 if (DEBUG_GTE(RECV, 1))
573 rprintf(FINFO, "finishing %s\n", fname);
574 fnametmp = fname;
575 goto do_set_file_attrs;
576 }
577
578 if (make_backups > 0 && overwriting_basis) {
579 if (!make_backup(fname))
580 return 1;
581 if (fnamecmp == fname)
582 fnamecmp = get_backup_name(fname);
583 }
584
585 /* Change permissions before putting the file into place. */
586 set_file_attrs(fnametmp, file, NULL, fnamecmp,
587 ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
588
589 /* move tmp file over real file */
590 if (DEBUG_GTE(RECV, 1))
591 rprintf(FINFO, "renaming %s to %s\n", fnametmp, fname);
592 ret = robust_rename(fnametmp, fname, temp_copy_name,
593 file->mode & INITACCESSPERMS);
594 if (ret < 0) {
595 rsyserr(FERROR_XFER, errno, "%s %s -> \"%s\"",
596 ret == -2 ? "copy" : "rename",
597 full_fname(fnametmp), fname);
598 if (!partialptr || (ret == -2 && temp_copy_name)
599 || robust_rename(fnametmp, partialptr, NULL,
600 file->mode & INITACCESSPERMS) < 0)
601 do_unlink(fnametmp);
602 return 0;
603 }
604 if (ret == 0) {
605 /* The file was moved into place (not copied), so it's done. */
606 return 1;
607 }
608 /* The file was copied, so tweak the perms of the copied file. If it
609 * was copied to partialptr, move it into its final destination. */
610 fnametmp = temp_copy_name ? temp_copy_name : fname;
611
612 do_set_file_attrs:
613 set_file_attrs(fnametmp, file, NULL, fnamecmp,
614 ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
615
616 if (temp_copy_name) {
617 if (do_rename(fnametmp, fname) < 0) {
618 rsyserr(FERROR_XFER, errno, "rename %s -> \"%s\"",
619 full_fname(fnametmp), fname);
620 return 0;
621 }
622 handle_partial_dir(temp_copy_name, PDIR_DELETE);
623 }
624 return 1;
625}
626
627struct file_list *flist_for_ndx(int ndx)
628{
629 struct file_list *flist = cur_flist;
630
631 if (!flist && !(flist = first_flist))
632 return NULL;
633
634 while (ndx < flist->ndx_start-1) {
635 if (flist == first_flist)
636 return NULL;
637 flist = flist->prev;
638 }
639 while (ndx >= flist->ndx_start + flist->used) {
640 if (!(flist = flist->next))
641 return NULL;
642 }
643 return flist;
644}
645
646const char *who_am_i(void)
647{
648 if (am_starting_up)
649 return am_server ? "server" : "client";
650 return am_sender ? "sender" : am_generator ? "generator" : "receiver";
651}