Commit | Line | Data |
---|---|---|
7a2fd68b | 1 | /* |
0f78b815 WD |
2 | * Routines common to more than one of the rsync processes. |
3 | * | |
4 | * Copyright (C) 1996 Andrew Tridgell | |
5 | * Copyright (C) 1996 Paul Mackerras | |
100018b7 | 6 | * Copyright (C) 2003, 2004, 2005, 2006, 2007 Wayne Davison |
0f78b815 WD |
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 | * | |
e7c67065 WD |
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. | |
0f78b815 | 21 | */ |
c627d613 | 22 | |
2f03f956 | 23 | #include "rsync.h" |
c8563142 | 24 | #if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H |
ceccbacc WD |
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 | |
43a481dc | 32 | |
c627d613 | 33 | extern int verbose; |
c627d613 | 34 | extern int dry_run; |
9b499e95 | 35 | extern int preserve_perms; |
81284832 | 36 | extern int preserve_executability; |
2f03f956 | 37 | extern int preserve_times; |
82471e68 | 38 | extern int omit_dir_times; |
7b8356d0 | 39 | extern int am_root; |
794b0a03 | 40 | extern int am_server; |
c3e5e585 WD |
41 | extern int am_sender; |
42 | extern int am_generator; | |
ed9d969c | 43 | extern int am_starting_up; |
e0f4a661 | 44 | extern int allow_8bit_chars; |
b675ba6f | 45 | extern int protocol_version; |
2f03f956 AT |
46 | extern int preserve_uid; |
47 | extern int preserve_gid; | |
3ea6e0e7 | 48 | extern int inc_recurse; |
c786a7ae | 49 | extern int inplace; |
f3d6d480 | 50 | extern int flist_eof; |
81b07870 | 51 | extern int keep_dirlinks; |
2f03f956 | 52 | extern int make_backups; |
c5b7aa15 | 53 | extern mode_t orig_umask; |
f3d6d480 | 54 | extern struct file_list *cur_flist, *first_flist, *dir_flist; |
94d3725c | 55 | extern struct chmod_mode_struct *daemon_chmod_modes; |
fe055c71 | 56 | |
595251de | 57 | #if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H |
ceccbacc WD |
58 | iconv_t ic_chck = (iconv_t)-1; |
59 | ||
2a62f5ee | 60 | static const char *default_charset(void) |
ceccbacc WD |
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 | |
7fc87d2d | 65 | return nl_langinfo(CODESET); |
ceccbacc WD |
66 | #else |
67 | return ""; /* Works with (at the very least) gnu iconv... */ | |
68 | #endif | |
69 | } | |
70 | ||
71 | void setup_iconv() | |
72 | { | |
e0f4a661 WD |
73 | if (!am_server && !allow_8bit_chars) { |
74 | const char *defset = default_charset(); | |
2d2f71fb | 75 | |
e0f4a661 WD |
76 | /* It's OK if this fails... */ |
77 | ic_chck = iconv_open(defset, defset); | |
ceccbacc | 78 | |
e0f4a661 WD |
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 | } | |
7fc87d2d | 90 | } |
ceccbacc WD |
91 | } |
92 | } | |
93 | #endif | |
fe055c71 | 94 | |
d1c178dd WD |
95 | /* This is used by sender.c with a valid f_out, and by receive.c with |
96 | * f_out = -1. */ | |
f3d6d480 WD |
97 | int read_ndx_and_attrs(int f_in, int f_out, int *iflag_ptr, |
98 | uchar *type_ptr, char *buf, int *len_ptr) | |
d1c178dd | 99 | { |
f3d6d480 WD |
100 | int len, iflags = 0; |
101 | struct file_list *flist; | |
d1c178dd | 102 | uchar fnamecmp_type = FNAMECMP_FNAME; |
f3d6d480 WD |
103 | int verbose_save, ndx; |
104 | ||
105 | read_loop: | |
106 | while (1) { | |
9ae7a2cd | 107 | ndx = read_ndx(f_in); |
f3d6d480 WD |
108 | |
109 | if (ndx >= 0) | |
110 | break; | |
111 | if (ndx == NDX_DONE) | |
112 | return ndx; | |
3ea6e0e7 | 113 | if (!inc_recurse || am_sender) |
f3d6d480 WD |
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) | |
d1c178dd WD |
143 | : ITEM_TRANSFER | ITEM_MISSING_DATA; |
144 | ||
f3d6d480 WD |
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()); | |
d1c178dd WD |
160 | exit_cleanup(RERR_PROTOCOL); |
161 | } | |
f3d6d480 | 162 | cur_flist = flist; |
d1c178dd WD |
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) { | |
f3d6d480 WD |
178 | int i = ndx - cur_flist->ndx_start; |
179 | if (!S_ISREG(cur_flist->files[i]->mode)) { | |
d1c178dd WD |
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 | ||
f3d6d480 WD |
190 | *iflag_ptr = iflags; |
191 | return ndx; | |
d1c178dd WD |
192 | } |
193 | ||
c627d613 AT |
194 | /* |
195 | free a sums struct | |
196 | */ | |
2f03f956 | 197 | void free_sums(struct sum_struct *s) |
c627d613 | 198 | { |
298c10d5 AT |
199 | if (s->sums) free(s->sums); |
200 | free(s); | |
c627d613 AT |
201 | } |
202 | ||
81284832 WD |
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. */ | |
67f8a41b | 205 | mode_t dest_mode(mode_t flist_mode, mode_t stat_mode, int exists) |
81284832 | 206 | { |
67f8a41b | 207 | int new_mode; |
9b499e95 | 208 | /* If the file already exists, we'll return the local permissions, |
81284832 WD |
209 | * possibly tweaked by the --executability option. */ |
210 | if (exists) { | |
67f8a41b | 211 | new_mode = (flist_mode & ~CHMOD_BITS) | (stat_mode & CHMOD_BITS); |
81284832 WD |
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)) | |
67f8a41b WD |
217 | new_mode &= ~0111; |
218 | else if (!(stat_mode & 0111)) | |
219 | new_mode |= (new_mode & 0444) >> 2; | |
81284832 | 220 | } |
67f8a41b WD |
221 | } else { |
222 | /* Apply the umask and turn off special permissions. */ | |
223 | new_mode = flist_mode & (~CHMOD_BITS | (ACCESSPERMS & ~orig_umask)); | |
224 | } | |
67f8a41b | 225 | return new_mode; |
81284832 WD |
226 | } |
227 | ||
36119f6e WD |
228 | int set_file_attrs(char *fname, struct file_struct *file, STRUCT_STAT *st, |
229 | int flags) | |
c627d613 | 230 | { |
667e72a1 AT |
231 | int updated = 0; |
232 | STRUCT_STAT st2; | |
460f6b99 | 233 | int change_uid, change_gid; |
519d55a9 | 234 | mode_t new_mode = file->mode; |
c627d613 | 235 | |
667e72a1 | 236 | if (!st) { |
f227ffe4 WD |
237 | if (dry_run) |
238 | return 1; | |
373ef160 | 239 | if (link_stat(fname, &st2, 0) < 0) { |
d62bcc17 WD |
240 | rsyserr(FERROR, errno, "stat %s failed", |
241 | full_fname(fname)); | |
667e72a1 AT |
242 | return 0; |
243 | } | |
244 | st = &st2; | |
519d55a9 | 245 | if (!preserve_perms && S_ISDIR(new_mode) |
9b499e95 WD |
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. */ | |
519d55a9 | 249 | new_mode |= S_ISGID; |
9b499e95 | 250 | } |
667e72a1 | 251 | } |
c627d613 | 252 | |
c8d34657 | 253 | if (!preserve_times || (S_ISDIR(st->st_mode) && omit_dir_times)) |
36119f6e WD |
254 | flags |= ATTRS_SKIP_MTIME; |
255 | if (!(flags & ATTRS_SKIP_MTIME) | |
0f86c74e | 256 | && cmp_time(st->st_mtime, file->modtime) != 0) { |
c8d34657 WD |
257 | int ret = set_modtime(fname, file->modtime, st->st_mode); |
258 | if (ret < 0) { | |
d62bcc17 WD |
259 | rsyserr(FERROR, errno, "failed to set times on %s", |
260 | full_fname(fname)); | |
667e72a1 AT |
261 | return 0; |
262 | } | |
c8d34657 WD |
263 | if (ret == 0) /* ret == 1 if symlink could not be set */ |
264 | updated = 1; | |
667e72a1 AT |
265 | } |
266 | ||
82ad07c4 WD |
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); | |
4f5b0756 | 270 | #if !defined HAVE_LCHOWN && !defined CHOWN_MODIFIES_SYMLINK |
a41a1e87 WD |
271 | if (S_ISLNK(st->st_mode)) |
272 | ; | |
273 | else | |
274 | #endif | |
460f6b99 | 275 | if (change_uid || change_gid) { |
62c9e6b3 | 276 | if (verbose > 2) { |
ce37eb2d WD |
277 | if (change_uid) { |
278 | rprintf(FINFO, | |
4875d6b6 | 279 | "set uid of %s from %ld to %ld\n", |
45c49b52 | 280 | fname, |
82ad07c4 | 281 | (long)st->st_uid, (long)F_UID(file)); |
ce37eb2d WD |
282 | } |
283 | if (change_gid) { | |
284 | rprintf(FINFO, | |
4875d6b6 | 285 | "set gid of %s from %ld to %ld\n", |
45c49b52 | 286 | fname, |
82ad07c4 | 287 | (long)st->st_gid, (long)F_GID(file)); |
ce37eb2d WD |
288 | } |
289 | } | |
667e72a1 | 290 | if (do_lchown(fname, |
82ad07c4 WD |
291 | change_uid ? F_UID(file) : st->st_uid, |
292 | change_gid ? F_GID(file) : st->st_gid) != 0) { | |
460f6b99 | 293 | /* shouldn't have attempted to change uid or gid |
a174e1ed | 294 | * unless have the privilege */ |
d62bcc17 | 295 | rsyserr(FERROR, errno, "%s %s failed", |
a174e1ed | 296 | change_uid ? "chown" : "chgrp", |
d62bcc17 | 297 | full_fname(fname)); |
460f6b99 | 298 | return 0; |
667e72a1 | 299 | } |
a5827a28 | 300 | /* a lchown had been done - we have to re-stat if the |
dd096ae0 WD |
301 | * destination had the setuid or setgid bits set due |
302 | * to the side effect of the chown call */ | |
a5827a28 | 303 | if (st->st_mode & (S_ISUID | S_ISGID)) { |
81b07870 WD |
304 | link_stat(fname, st, |
305 | keep_dirlinks && S_ISDIR(st->st_mode)); | |
a5827a28 | 306 | } |
460f6b99 | 307 | updated = 1; |
667e72a1 | 308 | } |
c627d613 | 309 | |
519d55a9 WD |
310 | if (daemon_chmod_modes && !S_ISLNK(new_mode)) |
311 | new_mode = tweak_mode(new_mode, daemon_chmod_modes); | |
4f5b0756 | 312 | #ifdef HAVE_CHMOD |
225aeca3 | 313 | if (!BITS_EQUAL(st->st_mode, new_mode, CHMOD_BITS)) { |
519d55a9 | 314 | int ret = do_chmod(fname, new_mode); |
c8d34657 WD |
315 | if (ret < 0) { |
316 | rsyserr(FERROR, errno, | |
317 | "failed to set permissions on %s", | |
318 | full_fname(fname)); | |
319 | return 0; | |
667e72a1 | 320 | } |
c8d34657 WD |
321 | if (ret == 0) /* ret == 1 if symlink could not be set */ |
322 | updated = 1; | |
667e72a1 | 323 | } |
c627d613 | 324 | #endif |
6a7cc46c | 325 | |
36119f6e | 326 | if (verbose > 1 && flags & ATTRS_REPORT) { |
667e72a1 | 327 | if (updated) |
1925c344 | 328 | rprintf(FCLIENT, "%s\n", fname); |
667e72a1 | 329 | else |
1925c344 | 330 | rprintf(FCLIENT, "%s is uptodate\n", fname); |
667e72a1 AT |
331 | } |
332 | return updated; | |
c627d613 AT |
333 | } |
334 | ||
b8e9c234 | 335 | RETSIGTYPE sig_int(UNUSED(int val)) |
34ccb63e | 336 | { |
d5a0b483 WD |
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); | |
65417579 | 346 | exit_cleanup(RERR_SIGNAL); |
c627d613 AT |
347 | } |
348 | ||
d8b1c923 WD |
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) | |
c95da96a | 356 | { |
62c9e6b3 WD |
357 | int ret; |
358 | ||
a3221d2a WD |
359 | if (inplace) { |
360 | if (verbose > 2) | |
45c49b52 | 361 | rprintf(FINFO, "finishing %s\n", fname); |
d8b1c923 | 362 | fnametmp = fname; |
36119f6e | 363 | goto do_set_file_attrs; |
a3221d2a WD |
364 | } |
365 | ||
f3d6d480 | 366 | if (make_backups > 0 && overwriting_basis && !make_backup(fname)) |
e484f0cc WD |
367 | return; |
368 | ||
ebeacb36 | 369 | /* Change permissions before putting the file into place. */ |
36119f6e WD |
370 | set_file_attrs(fnametmp, file, NULL, |
371 | ok_to_set_time ? 0 : ATTRS_SKIP_MTIME); | |
ebeacb36 | 372 | |
c95da96a | 373 | /* move tmp file over real file */ |
45c49b52 WD |
374 | if (verbose > 2) |
375 | rprintf(FINFO, "renaming %s to %s\n", fnametmp, fname); | |
d8b1c923 WD |
376 | ret = robust_rename(fnametmp, fname, partialptr, |
377 | file->mode & INITACCESSPERMS); | |
3d061653 | 378 | if (ret < 0) { |
d62bcc17 | 379 | rsyserr(FERROR, errno, "%s %s -> \"%s\"", |
45c49b52 WD |
380 | ret == -2 ? "copy" : "rename", |
381 | full_fname(fnametmp), fname); | |
c7c11a0d | 382 | do_unlink(fnametmp); |
077e59b7 | 383 | return; |
c95da96a | 384 | } |
ebeacb36 WD |
385 | if (ret == 0) { |
386 | /* The file was moved into place (not copied), so it's done. */ | |
387 | return; | |
388 | } | |
d8b1c923 WD |
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 | ||
36119f6e | 393 | do_set_file_attrs: |
d8b1c923 | 394 | set_file_attrs(fnametmp, file, NULL, |
36119f6e | 395 | ok_to_set_time ? 0 : ATTRS_SKIP_MTIME); |
d8b1c923 WD |
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 | } | |
c95da96a | 404 | } |
c3e5e585 | 405 | |
f3d6d480 WD |
406 | struct file_list *flist_for_ndx(int ndx) |
407 | { | |
408 | struct file_list *flist = cur_flist; | |
409 | ||
87a34ce5 | 410 | if (!flist && !(flist = first_flist)) |
f3d6d480 WD |
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 | ||
c3e5e585 WD |
425 | const char *who_am_i(void) |
426 | { | |
ed9d969c | 427 | if (am_starting_up) |
794b0a03 | 428 | return am_server ? "server" : "client"; |
ebeacb36 | 429 | return am_sender ? "sender" : am_generator ? "generator" : "receiver"; |
c3e5e585 | 430 | } |