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 | |
d3d07a5e | 6 | * Copyright (C) 2003-2008 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; |
eabc85ef | 48 | extern int msgs2stderr; |
81b07870 | 49 | extern int keep_dirlinks; |
2f03f956 | 50 | extern int make_backups; |
f303b749 WD |
51 | extern int delete_during; |
52 | extern int check_for_io_err; | |
f3d6d480 | 53 | extern struct file_list *cur_flist, *first_flist, *dir_flist; |
94d3725c | 54 | extern struct chmod_mode_struct *daemon_chmod_modes; |
332cf6df WD |
55 | #ifdef ICONV_OPTION |
56 | extern char *iconv_opt; | |
57 | #endif | |
fe055c71 | 58 | |
332cf6df | 59 | #ifdef ICONV_CONST |
ceccbacc | 60 | iconv_t ic_chck = (iconv_t)-1; |
2ac97930 | 61 | # ifdef ICONV_OPTION |
332cf6df | 62 | iconv_t ic_send = (iconv_t)-1, ic_recv = (iconv_t)-1; |
2ac97930 | 63 | # endif |
ceccbacc | 64 | |
2a62f5ee | 65 | static const char *default_charset(void) |
ceccbacc | 66 | { |
2ac97930 | 67 | # if defined HAVE_LIBCHARSET_H && defined HAVE_LOCALE_CHARSET |
ceccbacc | 68 | return locale_charset(); |
2ac97930 | 69 | # elif defined HAVE_LANGINFO_H && defined HAVE_NL_LANGINFO |
7fc87d2d | 70 | return nl_langinfo(CODESET); |
2ac97930 | 71 | # else |
ceccbacc | 72 | return ""; /* Works with (at the very least) gnu iconv... */ |
2ac97930 | 73 | # endif |
ceccbacc WD |
74 | } |
75 | ||
6191429b | 76 | void setup_iconv(void) |
ceccbacc | 77 | { |
332cf6df WD |
78 | const char *defset = default_charset(); |
79 | # ifdef ICONV_OPTION | |
80 | const char *charset; | |
81 | char *cp; | |
2ac97930 | 82 | # endif |
332cf6df | 83 | |
e0f4a661 | 84 | if (!am_server && !allow_8bit_chars) { |
e0f4a661 WD |
85 | /* It's OK if this fails... */ |
86 | ic_chck = iconv_open(defset, defset); | |
ceccbacc | 87 | |
c9f540f3 | 88 | if (DEBUG_GTE(ICONV, 2)) { |
e0f4a661 WD |
89 | if (ic_chck == (iconv_t)-1) { |
90 | rprintf(FINFO, | |
c9f540f3 WD |
91 | "msg checking via isprint()" |
92 | " (iconv_open(\"%s\", \"%s\") errno: %d)\n", | |
e0f4a661 WD |
93 | defset, defset, errno); |
94 | } else { | |
95 | rprintf(FINFO, | |
c9f540f3 WD |
96 | "msg checking charset: %s\n", |
97 | defset); | |
e0f4a661 | 98 | } |
7fc87d2d | 99 | } |
c9f540f3 WD |
100 | } else |
101 | ic_chck = (iconv_t)-1; | |
332cf6df WD |
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 | ||
c9f540f3 WD |
131 | if (DEBUG_GTE(ICONV, 1)) { |
132 | rprintf(FINFO, "[%s] charset: %s\n", | |
133 | who_am_i(), *charset ? charset : "[LOCALE]"); | |
332cf6df WD |
134 | } |
135 | # endif | |
ceccbacc | 136 | } |
2ac97930 | 137 | |
2ac97930 WD |
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. */ | |
154 | int 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 | } | |
ceccbacc | 207 | #endif |
fe055c71 | 208 | |
53936ef9 WD |
209 | void send_protected_args(int fd, char *args[]) |
210 | { | |
7abcfd85 | 211 | int i; |
53936ef9 | 212 | #ifdef ICONV_OPTION |
7abcfd85 | 213 | int convert = ic_send != (iconv_t)-1; |
53936ef9 WD |
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 */ | |
951e826b | 222 | if (DEBUG_GTE(CMD, 1)) |
53936ef9 WD |
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 | ||
16edf865 WD |
245 | int read_ndx_and_attrs(int f_in, int *iflag_ptr, uchar *type_ptr, |
246 | char *buf, int *len_ptr) | |
d1c178dd | 247 | { |
f3d6d480 WD |
248 | int len, iflags = 0; |
249 | struct file_list *flist; | |
d1c178dd | 250 | uchar fnamecmp_type = FNAMECMP_FNAME; |
951e826b | 251 | int ndx; |
f3d6d480 WD |
252 | |
253 | read_loop: | |
254 | while (1) { | |
9ae7a2cd | 255 | ndx = read_ndx(f_in); |
f3d6d480 | 256 | |
f303b749 WD |
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 | } | |
f3d6d480 | 263 | break; |
f303b749 WD |
264 | } |
265 | check_for_io_err = 0; | |
f3d6d480 WD |
266 | if (ndx == NDX_DONE) |
267 | return ndx; | |
3ea6e0e7 | 268 | if (!inc_recurse || am_sender) |
f3d6d480 WD |
269 | goto invalid_ndx; |
270 | if (ndx == NDX_FLIST_EOF) { | |
271 | flist_eof = 1; | |
332cf6df | 272 | send_msg(MSG_FLIST_EOF, "", 0, 0); |
f3d6d480 WD |
273 | continue; |
274 | } | |
275 | ndx = NDX_FLIST_OFFSET - ndx; | |
9decb4d2 | 276 | if (ndx < 0 || ndx >= dir_flist->used) { |
f3d6d480 WD |
277 | ndx = NDX_FLIST_OFFSET - ndx; |
278 | rprintf(FERROR, | |
59fd2a5e WD |
279 | "[%s] Invalid dir index: %d (%d - %d)\n", |
280 | who_am_i(), ndx, NDX_FLIST_OFFSET, | |
9decb4d2 | 281 | NDX_FLIST_OFFSET - dir_flist->used + 1); |
f3d6d480 WD |
282 | exit_cleanup(RERR_PROTOCOL); |
283 | } | |
f3d6d480 WD |
284 | |
285 | /* Send everything read from f_in to msg_fd_out. */ | |
951e826b | 286 | if (DEBUG_GTE(FLIST, 2)) { |
1faa1a6d WD |
287 | rprintf(FINFO, "[%s] receiving flist for dir %d\n", |
288 | who_am_i(), ndx); | |
289 | } | |
eabc85ef WD |
290 | if (!msgs2stderr) |
291 | negate_output_levels(); /* turn off all info/debug output */ | |
b7386d23 WD |
292 | send_msg_int(MSG_FLIST, ndx); |
293 | start_flist_forward(f_in); | |
f3d6d480 WD |
294 | flist = recv_file_list(f_in); |
295 | flist->parent_ndx = ndx; | |
296 | stop_flist_forward(); | |
eabc85ef WD |
297 | if (!msgs2stderr) |
298 | negate_output_levels(); /* restore info/debug output */ | |
f303b749 WD |
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; | |
f3d6d480 WD |
303 | } |
304 | ||
305 | iflags = protocol_version >= 29 ? read_shortint(f_in) | |
d1c178dd WD |
306 | : ITEM_TRANSFER | ITEM_MISSING_DATA; |
307 | ||
f3d6d480 WD |
308 | /* Honor the old-style keep-alive indicator. */ |
309 | if (protocol_version < 30 | |
9decb4d2 | 310 | && ndx == cur_flist->used && iflags == ITEM_IS_NEW) { |
f3d6d480 WD |
311 | if (am_sender) |
312 | maybe_send_keepalive(); | |
313 | goto read_loop; | |
314 | } | |
315 | ||
316 | if (!(flist = flist_for_ndx(ndx))) { | |
65b4e4b2 | 317 | int start, used; |
f3d6d480 | 318 | invalid_ndx: |
65b4e4b2 WD |
319 | start = first_flist ? first_flist->ndx_start : 0; |
320 | used = first_flist ? first_flist->used : 0; | |
f3d6d480 WD |
321 | rprintf(FERROR, |
322 | "Invalid file index: %d (%d - %d) with iflags %x [%s]\n", | |
65b4e4b2 | 323 | ndx, start - 1, start + used -1, iflags, who_am_i()); |
d1c178dd WD |
324 | exit_cleanup(RERR_PROTOCOL); |
325 | } | |
f3d6d480 | 326 | cur_flist = flist; |
d1c178dd WD |
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) { | |
f3d6d480 | 342 | int i = ndx - cur_flist->ndx_start; |
6755a7d7 | 343 | if (i < 0 || !S_ISREG(cur_flist->files[i]->mode)) { |
d1c178dd WD |
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 | } | |
d1c178dd WD |
349 | } |
350 | ||
f3d6d480 WD |
351 | *iflag_ptr = iflags; |
352 | return ndx; | |
d1c178dd WD |
353 | } |
354 | ||
c627d613 AT |
355 | /* |
356 | free a sums struct | |
357 | */ | |
2f03f956 | 358 | void free_sums(struct sum_struct *s) |
c627d613 | 359 | { |
298c10d5 AT |
360 | if (s->sums) free(s->sums); |
361 | free(s); | |
c627d613 AT |
362 | } |
363 | ||
81284832 WD |
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. */ | |
1c3344a1 WD |
366 | mode_t dest_mode(mode_t flist_mode, mode_t stat_mode, int dflt_perms, |
367 | int exists) | |
81284832 | 368 | { |
67f8a41b | 369 | int new_mode; |
9b499e95 | 370 | /* If the file already exists, we'll return the local permissions, |
81284832 WD |
371 | * possibly tweaked by the --executability option. */ |
372 | if (exists) { | |
67f8a41b | 373 | new_mode = (flist_mode & ~CHMOD_BITS) | (stat_mode & CHMOD_BITS); |
81284832 WD |
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)) | |
67f8a41b WD |
379 | new_mode &= ~0111; |
380 | else if (!(stat_mode & 0111)) | |
381 | new_mode |= (new_mode & 0444) >> 2; | |
81284832 | 382 | } |
67f8a41b | 383 | } else { |
1c3344a1 WD |
384 | /* Apply destination default permissions and turn |
385 | * off special permissions. */ | |
386 | new_mode = flist_mode & (~CHMOD_BITS | dflt_perms); | |
67f8a41b | 387 | } |
67f8a41b | 388 | return new_mode; |
81284832 WD |
389 | } |
390 | ||
13710874 | 391 | int set_file_attrs(const char *fname, struct file_struct *file, stat_x *sxp, |
16edf865 | 392 | const char *fnamecmp, int flags) |
c627d613 | 393 | { |
667e72a1 | 394 | int updated = 0; |
13710874 | 395 | stat_x sx2; |
460f6b99 | 396 | int change_uid, change_gid; |
519d55a9 | 397 | mode_t new_mode = file->mode; |
2dc7b91d | 398 | int inherit; |
c627d613 | 399 | |
1c3344a1 | 400 | if (!sxp) { |
f227ffe4 WD |
401 | if (dry_run) |
402 | return 1; | |
1c3344a1 | 403 | if (link_stat(fname, &sx2.st, 0) < 0) { |
3f0211b6 | 404 | rsyserr(FERROR_XFER, errno, "stat %s failed", |
d62bcc17 | 405 | full_fname(fname)); |
667e72a1 AT |
406 | return 0; |
407 | } | |
1c3344a1 WD |
408 | #ifdef SUPPORT_ACLS |
409 | sx2.acc_acl = sx2.def_acl = NULL; | |
16edf865 WD |
410 | #endif |
411 | #ifdef SUPPORT_XATTRS | |
412 | sx2.xattr = NULL; | |
1c3344a1 | 413 | #endif |
1c3344a1 | 414 | sxp = &sx2; |
2dc7b91d WD |
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; | |
667e72a1 | 423 | } |
c627d613 | 424 | |
e107b6b1 WD |
425 | if (daemon_chmod_modes && !S_ISLNK(new_mode)) |
426 | new_mode = tweak_mode(new_mode, daemon_chmod_modes); | |
427 | ||
1c3344a1 WD |
428 | #ifdef SUPPORT_ACLS |
429 | if (preserve_acls && !S_ISLNK(file->mode) && !ACL_READY(*sxp)) | |
430 | get_acl(fname, sxp); | |
431 | #endif | |
432 | ||
16edf865 | 433 | #ifdef SUPPORT_XATTRS |
9439c0cb | 434 | if (am_root < 0) |
e107b6b1 | 435 | set_stat_xattr(fname, file, new_mode); |
4d7c8e6b WD |
436 | if (preserve_xattrs && fnamecmp) |
437 | set_xattr(fname, file, fnamecmp, sxp); | |
16edf865 WD |
438 | #endif |
439 | ||
f9998046 | 440 | if (!preserve_times || (S_ISDIR(sxp->st.st_mode) && preserve_times == 1)) |
36119f6e WD |
441 | flags |= ATTRS_SKIP_MTIME; |
442 | if (!(flags & ATTRS_SKIP_MTIME) | |
1c3344a1 WD |
443 | && cmp_time(sxp->st.st_mtime, file->modtime) != 0) { |
444 | int ret = set_modtime(fname, file->modtime, sxp->st.st_mode); | |
c8d34657 | 445 | if (ret < 0) { |
3f0211b6 | 446 | rsyserr(FERROR_XFER, errno, "failed to set times on %s", |
d62bcc17 | 447 | full_fname(fname)); |
1c3344a1 | 448 | goto cleanup; |
667e72a1 | 449 | } |
c8d34657 WD |
450 | if (ret == 0) /* ret == 1 if symlink could not be set */ |
451 | updated = 1; | |
ee39281d | 452 | else |
1ed56a05 | 453 | file->flags |= FLAG_TIME_FAILED; |
667e72a1 AT |
454 | } |
455 | ||
9b25ef35 WD |
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) | |
858d45f1 | 458 | && sxp->st.st_gid != (gid_t)F_GROUP(file); |
4f5b0756 | 459 | #if !defined HAVE_LCHOWN && !defined CHOWN_MODIFIES_SYMLINK |
2e52ba36 | 460 | if (S_ISLNK(sxp->st.st_mode)) { |
a41a1e87 | 461 | ; |
2e52ba36 | 462 | } else |
a41a1e87 | 463 | #endif |
460f6b99 | 464 | if (change_uid || change_gid) { |
951e826b | 465 | if (DEBUG_GTE(OWN, 1)) { |
ce37eb2d WD |
466 | if (change_uid) { |
467 | rprintf(FINFO, | |
4ade505c WD |
468 | "set uid of %s from %u to %u\n", |
469 | fname, (unsigned)sxp->st.st_uid, F_OWNER(file)); | |
ce37eb2d WD |
470 | } |
471 | if (change_gid) { | |
472 | rprintf(FINFO, | |
4ade505c WD |
473 | "set gid of %s from %u to %u\n", |
474 | fname, (unsigned)sxp->st.st_gid, F_GROUP(file)); | |
ce37eb2d WD |
475 | } |
476 | } | |
87629cf2 WD |
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 | } | |
a5827a28 | 495 | } |
460f6b99 | 496 | updated = 1; |
667e72a1 | 497 | } |
c627d613 | 498 | |
1c3344a1 WD |
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 | ||
4f5b0756 | 510 | #ifdef HAVE_CHMOD |
1c3344a1 | 511 | if (!BITS_EQUAL(sxp->st.st_mode, new_mode, CHMOD_BITS)) { |
9439c0cb | 512 | int ret = am_root < 0 ? 0 : do_chmod(fname, new_mode); |
c8d34657 | 513 | if (ret < 0) { |
3f0211b6 | 514 | rsyserr(FERROR_XFER, errno, |
c8d34657 WD |
515 | "failed to set permissions on %s", |
516 | full_fname(fname)); | |
1c3344a1 | 517 | goto cleanup; |
667e72a1 | 518 | } |
c8d34657 WD |
519 | if (ret == 0) /* ret == 1 if symlink could not be set */ |
520 | updated = 1; | |
667e72a1 | 521 | } |
c627d613 | 522 | #endif |
6a7cc46c | 523 | |
951e826b | 524 | if (INFO_GTE(NAME, 2) && flags & ATTRS_REPORT) { |
667e72a1 | 525 | if (updated) |
1925c344 | 526 | rprintf(FCLIENT, "%s\n", fname); |
667e72a1 | 527 | else |
1925c344 | 528 | rprintf(FCLIENT, "%s is uptodate\n", fname); |
667e72a1 | 529 | } |
1c3344a1 | 530 | cleanup: |
16edf865 | 531 | if (sxp == &sx2) { |
1c3344a1 | 532 | #ifdef SUPPORT_ACLS |
16edf865 WD |
533 | if (preserve_acls) |
534 | free_acl(&sx2); | |
1c3344a1 | 535 | #endif |
16edf865 WD |
536 | #ifdef SUPPORT_XATTRS |
537 | if (preserve_xattrs) | |
538 | free_xattr(&sx2); | |
539 | #endif | |
540 | } | |
667e72a1 | 541 | return updated; |
c627d613 AT |
542 | } |
543 | ||
b8e9c234 | 544 | RETSIGTYPE sig_int(UNUSED(int val)) |
34ccb63e | 545 | { |
d5a0b483 WD |
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); | |
65417579 | 555 | exit_cleanup(RERR_SIGNAL); |
c627d613 AT |
556 | } |
557 | ||
d8b1c923 | 558 | /* Finish off a file transfer: renaming the file and setting the file's |
83235dbc WD |
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. */ | |
563 | int 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) | |
c95da96a | 567 | { |
62c9e6b3 | 568 | int ret; |
83235dbc | 569 | const char *temp_copy_name = partialptr && *partialptr != '/' ? partialptr : NULL; |
62c9e6b3 | 570 | |
a3221d2a | 571 | if (inplace) { |
951e826b | 572 | if (DEBUG_GTE(RECV, 1)) |
45c49b52 | 573 | rprintf(FINFO, "finishing %s\n", fname); |
d8b1c923 | 574 | fnametmp = fname; |
36119f6e | 575 | goto do_set_file_attrs; |
a3221d2a WD |
576 | } |
577 | ||
e9489cd6 WD |
578 | if (make_backups > 0 && overwriting_basis) { |
579 | if (!make_backup(fname)) | |
580 | return 1; | |
4337eeb7 WD |
581 | if (fnamecmp == fname) |
582 | fnamecmp = get_backup_name(fname); | |
e9489cd6 | 583 | } |
e484f0cc | 584 | |
ebeacb36 | 585 | /* Change permissions before putting the file into place. */ |
16edf865 | 586 | set_file_attrs(fnametmp, file, NULL, fnamecmp, |
36119f6e | 587 | ok_to_set_time ? 0 : ATTRS_SKIP_MTIME); |
ebeacb36 | 588 | |
c95da96a | 589 | /* move tmp file over real file */ |
951e826b | 590 | if (DEBUG_GTE(RECV, 1)) |
45c49b52 | 591 | rprintf(FINFO, "renaming %s to %s\n", fnametmp, fname); |
83235dbc | 592 | ret = robust_rename(fnametmp, fname, temp_copy_name, |
d8b1c923 | 593 | file->mode & INITACCESSPERMS); |
3d061653 | 594 | if (ret < 0) { |
3f0211b6 | 595 | rsyserr(FERROR_XFER, errno, "%s %s -> \"%s\"", |
45c49b52 WD |
596 | ret == -2 ? "copy" : "rename", |
597 | full_fname(fnametmp), fname); | |
83235dbc WD |
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; | |
c95da96a | 603 | } |
ebeacb36 WD |
604 | if (ret == 0) { |
605 | /* The file was moved into place (not copied), so it's done. */ | |
83235dbc | 606 | return 1; |
ebeacb36 | 607 | } |
d8b1c923 WD |
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. */ | |
83235dbc | 610 | fnametmp = temp_copy_name ? temp_copy_name : fname; |
d8b1c923 | 611 | |
36119f6e | 612 | do_set_file_attrs: |
16edf865 | 613 | set_file_attrs(fnametmp, file, NULL, fnamecmp, |
36119f6e | 614 | ok_to_set_time ? 0 : ATTRS_SKIP_MTIME); |
d8b1c923 | 615 | |
83235dbc | 616 | if (temp_copy_name) { |
d8b1c923 | 617 | if (do_rename(fnametmp, fname) < 0) { |
3f0211b6 | 618 | rsyserr(FERROR_XFER, errno, "rename %s -> \"%s\"", |
d8b1c923 | 619 | full_fname(fnametmp), fname); |
83235dbc WD |
620 | return 0; |
621 | } | |
622 | handle_partial_dir(temp_copy_name, PDIR_DELETE); | |
d8b1c923 | 623 | } |
83235dbc | 624 | return 1; |
c95da96a | 625 | } |
c3e5e585 | 626 | |
f3d6d480 WD |
627 | struct file_list *flist_for_ndx(int ndx) |
628 | { | |
629 | struct file_list *flist = cur_flist; | |
630 | ||
87a34ce5 | 631 | if (!flist && !(flist = first_flist)) |
f3d6d480 WD |
632 | return NULL; |
633 | ||
6755a7d7 | 634 | while (ndx < flist->ndx_start-1) { |
f3d6d480 WD |
635 | if (flist == first_flist) |
636 | return NULL; | |
637 | flist = flist->prev; | |
638 | } | |
65b4e4b2 | 639 | while (ndx >= flist->ndx_start + flist->used) { |
f3d6d480 WD |
640 | if (!(flist = flist->next)) |
641 | return NULL; | |
642 | } | |
643 | return flist; | |
644 | } | |
645 | ||
c3e5e585 WD |
646 | const char *who_am_i(void) |
647 | { | |
ed9d969c | 648 | if (am_starting_up) |
794b0a03 | 649 | return am_server ? "server" : "client"; |
ebeacb36 | 650 | return am_sender ? "sender" : am_generator ? "generator" : "receiver"; |
c3e5e585 | 651 | } |