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 | |
b3bf9b9d | 6 | * Copyright (C) 2003-2009 Wayne Davison |
0f78b815 WD |
7 | * |
8 | * This program is free software; you can redistribute it and/or modify | |
8e41b68e WD |
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. | |
0f78b815 WD |
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 | 18 | * You should have received a copy of the GNU General Public License along |
4fd842f9 | 19 | * with this program; if not, visit the http://fsf.org website. |
0f78b815 | 20 | */ |
c627d613 | 21 | |
2f03f956 | 22 | #include "rsync.h" |
1b42f628 | 23 | #include "ifuncs.h" |
ceccbacc WD |
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 | |
43a481dc | 29 | |
c627d613 | 30 | extern int dry_run; |
1c3344a1 | 31 | extern int preserve_acls; |
16edf865 | 32 | extern int preserve_xattrs; |
9b499e95 | 33 | extern int preserve_perms; |
81284832 | 34 | extern int preserve_executability; |
2f03f956 | 35 | extern int preserve_times; |
7b8356d0 | 36 | extern int am_root; |
794b0a03 | 37 | extern int am_server; |
c3e5e585 WD |
38 | extern int am_sender; |
39 | extern int am_generator; | |
ed9d969c | 40 | extern int am_starting_up; |
e0f4a661 | 41 | extern int allow_8bit_chars; |
b675ba6f | 42 | extern int protocol_version; |
9b25ef35 WD |
43 | extern int uid_ndx; |
44 | extern int gid_ndx; | |
3ea6e0e7 | 45 | extern int inc_recurse; |
c786a7ae | 46 | extern int inplace; |
f3d6d480 | 47 | extern int flist_eof; |
ce827c3e | 48 | extern int file_old_total; |
eabc85ef | 49 | extern int msgs2stderr; |
81b07870 | 50 | extern int keep_dirlinks; |
2f03f956 | 51 | extern int make_backups; |
f3d6d480 | 52 | extern struct file_list *cur_flist, *first_flist, *dir_flist; |
94d3725c | 53 | extern struct chmod_mode_struct *daemon_chmod_modes; |
332cf6df WD |
54 | #ifdef ICONV_OPTION |
55 | extern char *iconv_opt; | |
56 | #endif | |
fe055c71 | 57 | |
332cf6df | 58 | #ifdef ICONV_CONST |
ceccbacc | 59 | iconv_t ic_chck = (iconv_t)-1; |
2ac97930 | 60 | # ifdef ICONV_OPTION |
332cf6df | 61 | iconv_t ic_send = (iconv_t)-1, ic_recv = (iconv_t)-1; |
2ac97930 | 62 | # endif |
ceccbacc | 63 | |
2a62f5ee | 64 | static const char *default_charset(void) |
ceccbacc | 65 | { |
2ac97930 | 66 | # if defined HAVE_LIBCHARSET_H && defined HAVE_LOCALE_CHARSET |
ceccbacc | 67 | return locale_charset(); |
2ac97930 | 68 | # elif defined HAVE_LANGINFO_H && defined HAVE_NL_LANGINFO |
7fc87d2d | 69 | return nl_langinfo(CODESET); |
2ac97930 | 70 | # else |
ceccbacc | 71 | return ""; /* Works with (at the very least) gnu iconv... */ |
2ac97930 | 72 | # endif |
ceccbacc WD |
73 | } |
74 | ||
6191429b | 75 | void setup_iconv(void) |
ceccbacc | 76 | { |
332cf6df WD |
77 | const char *defset = default_charset(); |
78 | # ifdef ICONV_OPTION | |
79 | const char *charset; | |
80 | char *cp; | |
2ac97930 | 81 | # endif |
332cf6df | 82 | |
e0f4a661 | 83 | if (!am_server && !allow_8bit_chars) { |
e0f4a661 WD |
84 | /* It's OK if this fails... */ |
85 | ic_chck = iconv_open(defset, defset); | |
ceccbacc | 86 | |
c9f540f3 | 87 | if (DEBUG_GTE(ICONV, 2)) { |
e0f4a661 WD |
88 | if (ic_chck == (iconv_t)-1) { |
89 | rprintf(FINFO, | |
c9f540f3 WD |
90 | "msg checking via isprint()" |
91 | " (iconv_open(\"%s\", \"%s\") errno: %d)\n", | |
e0f4a661 WD |
92 | defset, defset, errno); |
93 | } else { | |
94 | rprintf(FINFO, | |
c9f540f3 WD |
95 | "msg checking charset: %s\n", |
96 | defset); | |
e0f4a661 | 97 | } |
7fc87d2d | 98 | } |
c9f540f3 WD |
99 | } else |
100 | ic_chck = (iconv_t)-1; | |
332cf6df WD |
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 | ||
c9f540f3 WD |
130 | if (DEBUG_GTE(ICONV, 1)) { |
131 | rprintf(FINFO, "[%s] charset: %s\n", | |
132 | who_am_i(), *charset ? charset : "[LOCALE]"); | |
332cf6df WD |
133 | } |
134 | # endif | |
ceccbacc | 135 | } |
2ac97930 | 136 | |
2ac97930 WD |
137 | /* This function converts the characters in the "in" xbuf into characters |
138 | * in the "out" xbuf. The "len" of the "in" xbuf is used starting from its | |
139 | * "pos". The "size" of the "out" xbuf restricts how many characters can be | |
140 | * stored, starting at its "pos+len" position. Note that the last byte of | |
141 | * the buffer is never used, which reserves space for a terminating '\0'. | |
142 | * We return a 0 on success or a -1 on error. An error also sets errno to | |
143 | * E2BIG, EILSEQ, or EINVAL (see below); otherwise errno will be set to 0. | |
144 | * The "in" xbuf is altered to update "pos" and "len". The "out" xbuf has | |
145 | * data appended, and its "len" incremented. If ICB_EXPAND_OUT is set in | |
146 | * "flags", the "out" xbuf will also be allocated if empty, and expanded if | |
147 | * too small (so E2BIG will not be returned). If ICB_INCLUDE_BAD is set in | |
148 | * "flags", any badly-encoded chars are included verbatim in the "out" xbuf, | |
149 | * so EILSEQ will not be returned. Likewise for ICB_INCLUDE_INCOMPLETE with | |
150 | * respect to an incomplete multi-byte char at the end, which ensures that | |
151 | * EINVAL is not returned. Anytime "in.pos" is 0 we will reset the iconv() | |
152 | * state prior to processing the characters. */ | |
153 | int iconvbufs(iconv_t ic, xbuf *in, xbuf *out, int flags) | |
154 | { | |
155 | ICONV_CONST char *ibuf; | |
156 | size_t icnt, ocnt; | |
157 | char *obuf; | |
158 | ||
159 | if (!out->size && flags & ICB_EXPAND_OUT) | |
160 | alloc_xbuf(out, 1024); | |
161 | ||
162 | if (!in->pos) | |
163 | iconv(ic, NULL, 0, NULL, 0); | |
164 | ||
165 | ibuf = in->buf + in->pos; | |
166 | icnt = in->len; | |
167 | ||
168 | obuf = out->buf + (out->pos + out->len); | |
169 | ocnt = out->size - (out->pos + out->len) - 1; | |
170 | ||
171 | while (icnt) { | |
172 | while (iconv(ic, &ibuf, &icnt, &obuf, &ocnt) == (size_t)-1) { | |
173 | if (errno == EINTR) | |
174 | continue; | |
175 | if (errno == EINVAL) { | |
176 | if (!(flags & ICB_INCLUDE_INCOMPLETE)) | |
177 | goto finish; | |
178 | } else if (errno == EILSEQ) { | |
179 | if (!(flags & ICB_INCLUDE_BAD)) | |
180 | goto finish; | |
181 | } else { | |
182 | size_t opos = obuf - out->buf; | |
183 | if (!(flags & ICB_EXPAND_OUT)) { | |
184 | errno = E2BIG; | |
185 | goto finish; | |
186 | } | |
187 | realloc_xbuf(out, out->size + 1024); | |
188 | obuf = out->buf + opos; | |
189 | ocnt += 1024; | |
190 | continue; | |
191 | } | |
192 | *obuf++ = *ibuf++; | |
193 | ocnt--, icnt--; | |
194 | } | |
195 | } | |
196 | ||
197 | errno = 0; | |
198 | ||
199 | finish: | |
200 | in->len = icnt; | |
201 | in->pos = ibuf - in->buf; | |
202 | out->len = obuf - out->buf - out->pos; | |
203 | ||
204 | return errno ? -1 : 0; | |
205 | } | |
ceccbacc | 206 | #endif |
fe055c71 | 207 | |
53936ef9 WD |
208 | void send_protected_args(int fd, char *args[]) |
209 | { | |
7abcfd85 | 210 | int i; |
53936ef9 | 211 | #ifdef ICONV_OPTION |
7abcfd85 | 212 | int convert = ic_send != (iconv_t)-1; |
53936ef9 WD |
213 | xbuf outbuf, inbuf; |
214 | ||
215 | if (convert) | |
216 | alloc_xbuf(&outbuf, 1024); | |
217 | #endif | |
218 | ||
219 | for (i = 0; args[i]; i++) {} /* find first NULL */ | |
220 | args[i] = "rsync"; /* set a new arg0 */ | |
951e826b | 221 | if (DEBUG_GTE(CMD, 1)) |
53936ef9 WD |
222 | print_child_argv("protected args:", args + i + 1); |
223 | do { | |
08b7c3ed WD |
224 | if (!args[i][0]) |
225 | write_buf(fd, ".", 2); | |
53936ef9 | 226 | #ifdef ICONV_OPTION |
08b7c3ed | 227 | else if (convert) { |
53936ef9 WD |
228 | INIT_XBUF_STRLEN(inbuf, args[i]); |
229 | iconvbufs(ic_send, &inbuf, &outbuf, | |
230 | ICB_EXPAND_OUT | ICB_INCLUDE_BAD | ICB_INCLUDE_INCOMPLETE); | |
231 | outbuf.buf[outbuf.len] = '\0'; | |
232 | write_buf(fd, outbuf.buf, outbuf.len + 1); | |
233 | outbuf.len = 0; | |
08b7c3ed | 234 | } |
53936ef9 | 235 | #endif |
08b7c3ed | 236 | else |
53936ef9 WD |
237 | write_buf(fd, args[i], strlen(args[i]) + 1); |
238 | } while (args[++i]); | |
239 | write_byte(fd, 0); | |
240 | ||
241 | #ifdef ICONV_OPTION | |
242 | if (convert) | |
243 | free(outbuf.buf); | |
244 | #endif | |
245 | } | |
246 | ||
16edf865 WD |
247 | int read_ndx_and_attrs(int f_in, int *iflag_ptr, uchar *type_ptr, |
248 | char *buf, int *len_ptr) | |
d1c178dd | 249 | { |
f3d6d480 WD |
250 | int len, iflags = 0; |
251 | struct file_list *flist; | |
d1c178dd | 252 | uchar fnamecmp_type = FNAMECMP_FNAME; |
951e826b | 253 | int ndx; |
f3d6d480 WD |
254 | |
255 | read_loop: | |
256 | while (1) { | |
9ae7a2cd | 257 | ndx = read_ndx(f_in); |
f3d6d480 | 258 | |
8b3e6052 | 259 | if (ndx >= 0) |
f3d6d480 WD |
260 | break; |
261 | if (ndx == NDX_DONE) | |
262 | return ndx; | |
e982d591 WD |
263 | if (!inc_recurse || am_sender) { |
264 | int last; | |
265 | if (first_flist) | |
266 | last = first_flist->prev->ndx_start + first_flist->prev->used - 1; | |
267 | else | |
268 | last = -1; | |
269 | rprintf(FERROR, | |
270 | "Invalid file index: %d (%d - %d) [%s]\n", | |
271 | ndx, NDX_DONE, last, who_am_i()); | |
272 | exit_cleanup(RERR_PROTOCOL); | |
273 | } | |
f3d6d480 WD |
274 | if (ndx == NDX_FLIST_EOF) { |
275 | flist_eof = 1; | |
332cf6df | 276 | send_msg(MSG_FLIST_EOF, "", 0, 0); |
f3d6d480 WD |
277 | continue; |
278 | } | |
279 | ndx = NDX_FLIST_OFFSET - ndx; | |
9decb4d2 | 280 | if (ndx < 0 || ndx >= dir_flist->used) { |
f3d6d480 WD |
281 | ndx = NDX_FLIST_OFFSET - ndx; |
282 | rprintf(FERROR, | |
e982d591 WD |
283 | "Invalid dir index: %d (%d - %d) [%s]\n", |
284 | ndx, NDX_FLIST_OFFSET, | |
285 | NDX_FLIST_OFFSET - dir_flist->used + 1, | |
286 | who_am_i()); | |
f3d6d480 WD |
287 | exit_cleanup(RERR_PROTOCOL); |
288 | } | |
f3d6d480 WD |
289 | |
290 | /* Send everything read from f_in to msg_fd_out. */ | |
951e826b | 291 | if (DEBUG_GTE(FLIST, 2)) { |
1faa1a6d WD |
292 | rprintf(FINFO, "[%s] receiving flist for dir %d\n", |
293 | who_am_i(), ndx); | |
294 | } | |
eabc85ef WD |
295 | if (!msgs2stderr) |
296 | negate_output_levels(); /* turn off all info/debug output */ | |
b7386d23 WD |
297 | send_msg_int(MSG_FLIST, ndx); |
298 | start_flist_forward(f_in); | |
f3d6d480 WD |
299 | flist = recv_file_list(f_in); |
300 | flist->parent_ndx = ndx; | |
301 | stop_flist_forward(); | |
eabc85ef WD |
302 | if (!msgs2stderr) |
303 | negate_output_levels(); /* restore info/debug output */ | |
f3d6d480 WD |
304 | } |
305 | ||
306 | iflags = protocol_version >= 29 ? read_shortint(f_in) | |
d1c178dd WD |
307 | : ITEM_TRANSFER | ITEM_MISSING_DATA; |
308 | ||
f3d6d480 WD |
309 | /* Honor the old-style keep-alive indicator. */ |
310 | if (protocol_version < 30 | |
9decb4d2 | 311 | && ndx == cur_flist->used && iflags == ITEM_IS_NEW) { |
f3d6d480 WD |
312 | if (am_sender) |
313 | maybe_send_keepalive(); | |
314 | goto read_loop; | |
315 | } | |
316 | ||
ce827c3e WD |
317 | flist = flist_for_ndx(ndx, "read_ndx_and_attrs"); |
318 | if (flist != cur_flist) { | |
319 | cur_flist = flist; | |
320 | if (am_sender) { | |
321 | file_old_total = cur_flist->used; | |
322 | for (flist = first_flist; flist != cur_flist; flist = flist->next) | |
323 | file_old_total += flist->used; | |
324 | } | |
325 | } | |
d1c178dd WD |
326 | |
327 | if (iflags & ITEM_BASIS_TYPE_FOLLOWS) | |
328 | fnamecmp_type = read_byte(f_in); | |
329 | *type_ptr = fnamecmp_type; | |
330 | ||
331 | if (iflags & ITEM_XNAME_FOLLOWS) { | |
332 | if ((len = read_vstring(f_in, buf, MAXPATHLEN)) < 0) | |
333 | exit_cleanup(RERR_PROTOCOL); | |
334 | } else { | |
335 | *buf = '\0'; | |
336 | len = -1; | |
337 | } | |
338 | *len_ptr = len; | |
339 | ||
340 | if (iflags & ITEM_TRANSFER) { | |
f3d6d480 | 341 | int i = ndx - cur_flist->ndx_start; |
6755a7d7 | 342 | if (i < 0 || !S_ISREG(cur_flist->files[i]->mode)) { |
d1c178dd WD |
343 | rprintf(FERROR, |
344 | "received request to transfer non-regular file: %d [%s]\n", | |
345 | ndx, who_am_i()); | |
346 | exit_cleanup(RERR_PROTOCOL); | |
347 | } | |
d1c178dd WD |
348 | } |
349 | ||
f3d6d480 WD |
350 | *iflag_ptr = iflags; |
351 | return ndx; | |
d1c178dd WD |
352 | } |
353 | ||
c627d613 AT |
354 | /* |
355 | free a sums struct | |
356 | */ | |
2f03f956 | 357 | void free_sums(struct sum_struct *s) |
c627d613 | 358 | { |
298c10d5 AT |
359 | if (s->sums) free(s->sums); |
360 | free(s); | |
c627d613 AT |
361 | } |
362 | ||
81284832 WD |
363 | /* This is only called when we aren't preserving permissions. Figure out what |
364 | * the permissions should be and return them merged back into the mode. */ | |
1c3344a1 WD |
365 | mode_t dest_mode(mode_t flist_mode, mode_t stat_mode, int dflt_perms, |
366 | int exists) | |
81284832 | 367 | { |
67f8a41b | 368 | int new_mode; |
9b499e95 | 369 | /* If the file already exists, we'll return the local permissions, |
81284832 WD |
370 | * possibly tweaked by the --executability option. */ |
371 | if (exists) { | |
67f8a41b | 372 | new_mode = (flist_mode & ~CHMOD_BITS) | (stat_mode & CHMOD_BITS); |
81284832 WD |
373 | if (preserve_executability && S_ISREG(flist_mode)) { |
374 | /* If the source file is executable, grant execute | |
375 | * rights to everyone who can read, but ONLY if the | |
376 | * file isn't already executable. */ | |
377 | if (!(flist_mode & 0111)) | |
67f8a41b WD |
378 | new_mode &= ~0111; |
379 | else if (!(stat_mode & 0111)) | |
380 | new_mode |= (new_mode & 0444) >> 2; | |
81284832 | 381 | } |
67f8a41b | 382 | } else { |
1c3344a1 WD |
383 | /* Apply destination default permissions and turn |
384 | * off special permissions. */ | |
385 | new_mode = flist_mode & (~CHMOD_BITS | dflt_perms); | |
67f8a41b | 386 | } |
67f8a41b | 387 | return new_mode; |
81284832 WD |
388 | } |
389 | ||
13710874 | 390 | int set_file_attrs(const char *fname, struct file_struct *file, stat_x *sxp, |
16edf865 | 391 | const char *fnamecmp, int flags) |
c627d613 | 392 | { |
667e72a1 | 393 | int updated = 0; |
13710874 | 394 | stat_x sx2; |
460f6b99 | 395 | int change_uid, change_gid; |
519d55a9 | 396 | mode_t new_mode = file->mode; |
2dc7b91d | 397 | int inherit; |
c627d613 | 398 | |
1c3344a1 | 399 | if (!sxp) { |
f227ffe4 WD |
400 | if (dry_run) |
401 | return 1; | |
1c3344a1 | 402 | if (link_stat(fname, &sx2.st, 0) < 0) { |
3f0211b6 | 403 | rsyserr(FERROR_XFER, errno, "stat %s failed", |
d62bcc17 | 404 | full_fname(fname)); |
667e72a1 AT |
405 | return 0; |
406 | } | |
09ca0d15 | 407 | init_stat_x(&sx2); |
1c3344a1 | 408 | sxp = &sx2; |
2dc7b91d WD |
409 | inherit = !preserve_perms; |
410 | } else | |
411 | inherit = !preserve_perms && file->flags & FLAG_DIR_CREATED; | |
412 | ||
413 | if (inherit && S_ISDIR(new_mode) && sxp->st.st_mode & S_ISGID) { | |
414 | /* We just created this directory and its setgid | |
415 | * bit is on, so make sure it stays on. */ | |
416 | new_mode |= S_ISGID; | |
667e72a1 | 417 | } |
c627d613 | 418 | |
e107b6b1 WD |
419 | if (daemon_chmod_modes && !S_ISLNK(new_mode)) |
420 | new_mode = tweak_mode(new_mode, daemon_chmod_modes); | |
421 | ||
1c3344a1 WD |
422 | #ifdef SUPPORT_ACLS |
423 | if (preserve_acls && !S_ISLNK(file->mode) && !ACL_READY(*sxp)) | |
424 | get_acl(fname, sxp); | |
425 | #endif | |
426 | ||
16edf865 | 427 | #ifdef SUPPORT_XATTRS |
9439c0cb | 428 | if (am_root < 0) |
e107b6b1 | 429 | set_stat_xattr(fname, file, new_mode); |
4d7c8e6b WD |
430 | if (preserve_xattrs && fnamecmp) |
431 | set_xattr(fname, file, fnamecmp, sxp); | |
16edf865 WD |
432 | #endif |
433 | ||
f9998046 | 434 | if (!preserve_times || (S_ISDIR(sxp->st.st_mode) && preserve_times == 1)) |
36119f6e WD |
435 | flags |= ATTRS_SKIP_MTIME; |
436 | if (!(flags & ATTRS_SKIP_MTIME) | |
1c3344a1 | 437 | && cmp_time(sxp->st.st_mtime, file->modtime) != 0) { |
1a2e41af | 438 | int ret = set_modtime(fname, file->modtime, F_MOD_NSEC(file), sxp->st.st_mode); |
c8d34657 | 439 | if (ret < 0) { |
3f0211b6 | 440 | rsyserr(FERROR_XFER, errno, "failed to set times on %s", |
d62bcc17 | 441 | full_fname(fname)); |
1c3344a1 | 442 | goto cleanup; |
667e72a1 | 443 | } |
c8d34657 WD |
444 | if (ret == 0) /* ret == 1 if symlink could not be set */ |
445 | updated = 1; | |
ee39281d | 446 | else |
1ed56a05 | 447 | file->flags |= FLAG_TIME_FAILED; |
667e72a1 AT |
448 | } |
449 | ||
9b25ef35 WD |
450 | change_uid = am_root && uid_ndx && sxp->st.st_uid != (uid_t)F_OWNER(file); |
451 | change_gid = gid_ndx && !(file->flags & FLAG_SKIP_GROUP) | |
858d45f1 | 452 | && sxp->st.st_gid != (gid_t)F_GROUP(file); |
4f5b0756 | 453 | #if !defined HAVE_LCHOWN && !defined CHOWN_MODIFIES_SYMLINK |
2e52ba36 | 454 | if (S_ISLNK(sxp->st.st_mode)) { |
a41a1e87 | 455 | ; |
2e52ba36 | 456 | } else |
a41a1e87 | 457 | #endif |
460f6b99 | 458 | if (change_uid || change_gid) { |
951e826b | 459 | if (DEBUG_GTE(OWN, 1)) { |
ce37eb2d WD |
460 | if (change_uid) { |
461 | rprintf(FINFO, | |
4ade505c WD |
462 | "set uid of %s from %u to %u\n", |
463 | fname, (unsigned)sxp->st.st_uid, F_OWNER(file)); | |
ce37eb2d WD |
464 | } |
465 | if (change_gid) { | |
466 | rprintf(FINFO, | |
4ade505c WD |
467 | "set gid of %s from %u to %u\n", |
468 | fname, (unsigned)sxp->st.st_gid, F_GROUP(file)); | |
ce37eb2d WD |
469 | } |
470 | } | |
87629cf2 WD |
471 | if (am_root >= 0) { |
472 | if (do_lchown(fname, | |
473 | change_uid ? (uid_t)F_OWNER(file) : sxp->st.st_uid, | |
474 | change_gid ? (gid_t)F_GROUP(file) : sxp->st.st_gid) != 0) { | |
475 | /* We shouldn't have attempted to change uid | |
476 | * or gid unless have the privilege. */ | |
477 | rsyserr(FERROR_XFER, errno, "%s %s failed", | |
478 | change_uid ? "chown" : "chgrp", | |
479 | full_fname(fname)); | |
480 | goto cleanup; | |
481 | } | |
482 | /* A lchown had been done, so we need to re-stat if | |
483 | * the destination had the setuid or setgid bits set | |
484 | * (due to the side effect of the chown call). */ | |
485 | if (sxp->st.st_mode & (S_ISUID | S_ISGID)) { | |
486 | link_stat(fname, &sxp->st, | |
487 | keep_dirlinks && S_ISDIR(sxp->st.st_mode)); | |
488 | } | |
a5827a28 | 489 | } |
460f6b99 | 490 | updated = 1; |
667e72a1 | 491 | } |
c627d613 | 492 | |
1c3344a1 WD |
493 | #ifdef SUPPORT_ACLS |
494 | /* It's OK to call set_acl() now, even for a dir, as the generator | |
495 | * will enable owner-writability using chmod, if necessary. | |
496 | * | |
497 | * If set_acl() changes permission bits in the process of setting | |
498 | * an access ACL, it changes sxp->st.st_mode so we know whether we | |
499 | * need to chmod(). */ | |
ee1c00fe WD |
500 | if (preserve_acls && !S_ISLNK(new_mode)) { |
501 | if (set_acl(fname, file, sxp, new_mode) > 0) | |
502 | updated = 1; | |
503 | } | |
1c3344a1 WD |
504 | #endif |
505 | ||
4f5b0756 | 506 | #ifdef HAVE_CHMOD |
1c3344a1 | 507 | if (!BITS_EQUAL(sxp->st.st_mode, new_mode, CHMOD_BITS)) { |
9439c0cb | 508 | int ret = am_root < 0 ? 0 : do_chmod(fname, new_mode); |
c8d34657 | 509 | if (ret < 0) { |
3f0211b6 | 510 | rsyserr(FERROR_XFER, errno, |
c8d34657 WD |
511 | "failed to set permissions on %s", |
512 | full_fname(fname)); | |
1c3344a1 | 513 | goto cleanup; |
667e72a1 | 514 | } |
c8d34657 WD |
515 | if (ret == 0) /* ret == 1 if symlink could not be set */ |
516 | updated = 1; | |
667e72a1 | 517 | } |
c627d613 | 518 | #endif |
6a7cc46c | 519 | |
951e826b | 520 | if (INFO_GTE(NAME, 2) && flags & ATTRS_REPORT) { |
667e72a1 | 521 | if (updated) |
1925c344 | 522 | rprintf(FCLIENT, "%s\n", fname); |
667e72a1 | 523 | else |
1925c344 | 524 | rprintf(FCLIENT, "%s is uptodate\n", fname); |
667e72a1 | 525 | } |
1c3344a1 | 526 | cleanup: |
16edf865 | 527 | if (sxp == &sx2) { |
1c3344a1 | 528 | #ifdef SUPPORT_ACLS |
16edf865 WD |
529 | if (preserve_acls) |
530 | free_acl(&sx2); | |
1c3344a1 | 531 | #endif |
16edf865 WD |
532 | #ifdef SUPPORT_XATTRS |
533 | if (preserve_xattrs) | |
534 | free_xattr(&sx2); | |
535 | #endif | |
536 | } | |
667e72a1 | 537 | return updated; |
c627d613 AT |
538 | } |
539 | ||
b8e9c234 | 540 | RETSIGTYPE sig_int(UNUSED(int val)) |
34ccb63e | 541 | { |
d5a0b483 WD |
542 | /* KLUGE: if the user hits Ctrl-C while ssh is prompting |
543 | * for a password, then our cleanup's sending of a SIGUSR1 | |
544 | * signal to all our children may kill ssh before it has a | |
545 | * chance to restore the tty settings (i.e. turn echo back | |
546 | * on). By sleeping for a short time, ssh gets a bigger | |
547 | * chance to do the right thing. If child processes are | |
548 | * not ssh waiting for a password, then this tiny delay | |
549 | * shouldn't hurt anything. */ | |
550 | msleep(400); | |
65417579 | 551 | exit_cleanup(RERR_SIGNAL); |
c627d613 AT |
552 | } |
553 | ||
d8b1c923 | 554 | /* Finish off a file transfer: renaming the file and setting the file's |
83235dbc WD |
555 | * attributes (e.g. permissions, ownership, etc.). If the robust_rename() |
556 | * call is forced to copy the temp file and partialptr is both non-NULL and | |
557 | * not an absolute path, we stage the file into the partial-dir and then | |
558 | * rename it into place. This returns 1 on succcess or 0 on failure. */ | |
559 | int finish_transfer(const char *fname, const char *fnametmp, | |
560 | const char *fnamecmp, const char *partialptr, | |
561 | struct file_struct *file, int ok_to_set_time, | |
562 | int overwriting_basis) | |
c95da96a | 563 | { |
62c9e6b3 | 564 | int ret; |
83235dbc | 565 | const char *temp_copy_name = partialptr && *partialptr != '/' ? partialptr : NULL; |
62c9e6b3 | 566 | |
a3221d2a | 567 | if (inplace) { |
951e826b | 568 | if (DEBUG_GTE(RECV, 1)) |
45c49b52 | 569 | rprintf(FINFO, "finishing %s\n", fname); |
d8b1c923 | 570 | fnametmp = fname; |
36119f6e | 571 | goto do_set_file_attrs; |
a3221d2a WD |
572 | } |
573 | ||
e9489cd6 | 574 | if (make_backups > 0 && overwriting_basis) { |
21cddef2 WD |
575 | int ok = make_backup(fname, False); |
576 | if (!ok) | |
e9489cd6 | 577 | return 1; |
21cddef2 | 578 | if (ok == 1 && fnamecmp == fname) |
4337eeb7 | 579 | fnamecmp = get_backup_name(fname); |
e9489cd6 | 580 | } |
e484f0cc | 581 | |
ebeacb36 | 582 | /* Change permissions before putting the file into place. */ |
16edf865 | 583 | set_file_attrs(fnametmp, file, NULL, fnamecmp, |
36119f6e | 584 | ok_to_set_time ? 0 : ATTRS_SKIP_MTIME); |
ebeacb36 | 585 | |
c95da96a | 586 | /* move tmp file over real file */ |
951e826b | 587 | if (DEBUG_GTE(RECV, 1)) |
45c49b52 | 588 | rprintf(FINFO, "renaming %s to %s\n", fnametmp, fname); |
83235dbc | 589 | ret = robust_rename(fnametmp, fname, temp_copy_name, |
d8b1c923 | 590 | file->mode & INITACCESSPERMS); |
3d061653 | 591 | if (ret < 0) { |
3f0211b6 | 592 | rsyserr(FERROR_XFER, errno, "%s %s -> \"%s\"", |
45c49b52 WD |
593 | ret == -2 ? "copy" : "rename", |
594 | full_fname(fnametmp), fname); | |
83235dbc WD |
595 | if (!partialptr || (ret == -2 && temp_copy_name) |
596 | || robust_rename(fnametmp, partialptr, NULL, | |
597 | file->mode & INITACCESSPERMS) < 0) | |
598 | do_unlink(fnametmp); | |
599 | return 0; | |
c95da96a | 600 | } |
ebeacb36 WD |
601 | if (ret == 0) { |
602 | /* The file was moved into place (not copied), so it's done. */ | |
83235dbc | 603 | return 1; |
ebeacb36 | 604 | } |
d8b1c923 WD |
605 | /* The file was copied, so tweak the perms of the copied file. If it |
606 | * was copied to partialptr, move it into its final destination. */ | |
83235dbc | 607 | fnametmp = temp_copy_name ? temp_copy_name : fname; |
d8b1c923 | 608 | |
36119f6e | 609 | do_set_file_attrs: |
16edf865 | 610 | set_file_attrs(fnametmp, file, NULL, fnamecmp, |
36119f6e | 611 | ok_to_set_time ? 0 : ATTRS_SKIP_MTIME); |
d8b1c923 | 612 | |
83235dbc | 613 | if (temp_copy_name) { |
d8b1c923 | 614 | if (do_rename(fnametmp, fname) < 0) { |
3f0211b6 | 615 | rsyserr(FERROR_XFER, errno, "rename %s -> \"%s\"", |
d8b1c923 | 616 | full_fname(fnametmp), fname); |
83235dbc WD |
617 | return 0; |
618 | } | |
619 | handle_partial_dir(temp_copy_name, PDIR_DELETE); | |
d8b1c923 | 620 | } |
83235dbc | 621 | return 1; |
c95da96a | 622 | } |
c3e5e585 | 623 | |
e982d591 | 624 | struct file_list *flist_for_ndx(int ndx, const char *fatal_error_loc) |
f3d6d480 WD |
625 | { |
626 | struct file_list *flist = cur_flist; | |
627 | ||
87a34ce5 | 628 | if (!flist && !(flist = first_flist)) |
e982d591 | 629 | goto not_found; |
f3d6d480 | 630 | |
6755a7d7 | 631 | while (ndx < flist->ndx_start-1) { |
f3d6d480 | 632 | if (flist == first_flist) |
e982d591 | 633 | goto not_found; |
f3d6d480 WD |
634 | flist = flist->prev; |
635 | } | |
65b4e4b2 | 636 | while (ndx >= flist->ndx_start + flist->used) { |
f3d6d480 | 637 | if (!(flist = flist->next)) |
e982d591 | 638 | goto not_found; |
f3d6d480 WD |
639 | } |
640 | return flist; | |
e982d591 WD |
641 | |
642 | not_found: | |
643 | if (fatal_error_loc) { | |
644 | int first, last; | |
645 | if (first_flist) { | |
646 | first = first_flist->ndx_start - 1; | |
647 | last = first_flist->prev->ndx_start + first_flist->prev->used - 1; | |
648 | } else { | |
649 | first = 0; | |
650 | last = -1; | |
651 | } | |
652 | rprintf(FERROR, | |
653 | "File-list index %d not in %d - %d (%s) [%s]\n", | |
654 | ndx, first, last, fatal_error_loc, who_am_i()); | |
655 | exit_cleanup(RERR_PROTOCOL); | |
656 | } | |
657 | return NULL; | |
f3d6d480 WD |
658 | } |
659 | ||
c3e5e585 WD |
660 | const char *who_am_i(void) |
661 | { | |
ed9d969c | 662 | if (am_starting_up) |
794b0a03 | 663 | return am_server ? "server" : "client"; |
ebeacb36 | 664 | return am_sender ? "sender" : am_generator ? "generator" : "receiver"; |
c3e5e585 | 665 | } |