]>
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 | |
c3b553a9 | 6 | * Copyright (C) 2003-2022 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; |
b774dbc1 WD |
35 | extern int preserve_mtimes; |
36 | extern int omit_dir_times; | |
37 | extern int omit_link_times; | |
7b8356d0 | 38 | extern int am_root; |
794b0a03 | 39 | extern int am_server; |
0d34fbdf | 40 | extern int am_daemon; |
c3e5e585 | 41 | extern int am_sender; |
82b2a31a | 42 | extern int am_receiver; |
c3e5e585 | 43 | extern int am_generator; |
ed9d969c | 44 | extern int am_starting_up; |
e0f4a661 | 45 | extern int allow_8bit_chars; |
b675ba6f | 46 | extern int protocol_version; |
d4070db6 | 47 | extern int got_kill_signal; |
b32aa479 | 48 | extern int called_from_signal_handler; |
3ea6e0e7 | 49 | extern int inc_recurse; |
c786a7ae | 50 | extern int inplace; |
f3d6d480 | 51 | extern int flist_eof; |
ce827c3e | 52 | extern int file_old_total; |
81b07870 | 53 | extern int keep_dirlinks; |
2f03f956 | 54 | extern int make_backups; |
70aeb5fd | 55 | extern int sanitize_paths; |
f3d6d480 | 56 | extern struct file_list *cur_flist, *first_flist, *dir_flist; |
94d3725c | 57 | extern struct chmod_mode_struct *daemon_chmod_modes; |
332cf6df WD |
58 | #ifdef ICONV_OPTION |
59 | extern char *iconv_opt; | |
60 | #endif | |
fe055c71 | 61 | |
b9367410 WD |
62 | #define UPDATED_OWNER (1<<0) |
63 | #define UPDATED_GROUP (1<<1) | |
64 | #define UPDATED_MTIME (1<<2) | |
65 | #define UPDATED_ATIME (1<<3) | |
66 | #define UPDATED_ACLS (1<<4) | |
67 | #define UPDATED_MODE (1<<5) | |
296352ec | 68 | #define UPDATED_CRTIME (1<<6) |
b9367410 | 69 | |
2c681b87 WD |
70 | #ifdef ICONV_CONST |
71 | iconv_t ic_chck = (iconv_t)-1; | |
72 | # ifdef ICONV_OPTION | |
73 | iconv_t ic_send = (iconv_t)-1, ic_recv = (iconv_t)-1; | |
74 | # endif | |
75 | ||
2a62f5ee | 76 | static const char *default_charset(void) |
ceccbacc | 77 | { |
2ac97930 | 78 | # if defined HAVE_LIBCHARSET_H && defined HAVE_LOCALE_CHARSET |
ceccbacc | 79 | return locale_charset(); |
2ac97930 | 80 | # elif defined HAVE_LANGINFO_H && defined HAVE_NL_LANGINFO |
7fc87d2d | 81 | return nl_langinfo(CODESET); |
2ac97930 | 82 | # else |
ceccbacc | 83 | return ""; /* Works with (at the very least) gnu iconv... */ |
2ac97930 | 84 | # endif |
ceccbacc WD |
85 | } |
86 | ||
6191429b | 87 | void setup_iconv(void) |
ceccbacc | 88 | { |
332cf6df WD |
89 | const char *defset = default_charset(); |
90 | # ifdef ICONV_OPTION | |
91 | const char *charset; | |
92 | char *cp; | |
2ac97930 | 93 | # endif |
332cf6df | 94 | |
e0f4a661 | 95 | if (!am_server && !allow_8bit_chars) { |
e0f4a661 WD |
96 | /* It's OK if this fails... */ |
97 | ic_chck = iconv_open(defset, defset); | |
ceccbacc | 98 | |
c9f540f3 | 99 | if (DEBUG_GTE(ICONV, 2)) { |
e0f4a661 WD |
100 | if (ic_chck == (iconv_t)-1) { |
101 | rprintf(FINFO, | |
c9f540f3 WD |
102 | "msg checking via isprint()" |
103 | " (iconv_open(\"%s\", \"%s\") errno: %d)\n", | |
e0f4a661 WD |
104 | defset, defset, errno); |
105 | } else { | |
106 | rprintf(FINFO, | |
c9f540f3 WD |
107 | "msg checking charset: %s\n", |
108 | defset); | |
e0f4a661 | 109 | } |
7fc87d2d | 110 | } |
c9f540f3 WD |
111 | } else |
112 | ic_chck = (iconv_t)-1; | |
332cf6df WD |
113 | |
114 | # ifdef ICONV_OPTION | |
115 | if (!iconv_opt) | |
116 | return; | |
117 | ||
118 | if ((cp = strchr(iconv_opt, ',')) != NULL) { | |
119 | if (am_server) /* A local transfer needs this. */ | |
120 | iconv_opt = cp + 1; | |
121 | else | |
122 | *cp = '\0'; | |
123 | } | |
124 | ||
125 | if (!*iconv_opt || (*iconv_opt == '.' && iconv_opt[1] == '\0')) | |
126 | charset = defset; | |
127 | else | |
128 | charset = iconv_opt; | |
129 | ||
130 | if ((ic_send = iconv_open(UTF8_CHARSET, charset)) == (iconv_t)-1) { | |
131 | rprintf(FERROR, "iconv_open(\"%s\", \"%s\") failed\n", | |
132 | UTF8_CHARSET, charset); | |
133 | exit_cleanup(RERR_UNSUPPORTED); | |
134 | } | |
135 | ||
136 | if ((ic_recv = iconv_open(charset, UTF8_CHARSET)) == (iconv_t)-1) { | |
137 | rprintf(FERROR, "iconv_open(\"%s\", \"%s\") failed\n", | |
138 | charset, UTF8_CHARSET); | |
139 | exit_cleanup(RERR_UNSUPPORTED); | |
140 | } | |
141 | ||
c9f540f3 WD |
142 | if (DEBUG_GTE(ICONV, 1)) { |
143 | rprintf(FINFO, "[%s] charset: %s\n", | |
144 | who_am_i(), *charset ? charset : "[LOCALE]"); | |
332cf6df WD |
145 | } |
146 | # endif | |
ceccbacc | 147 | } |
2ac97930 | 148 | |
907e6a32 WD |
149 | /* This function converts the chars in the "in" xbuf into characters in the |
150 | * "out" xbuf. The ".len" chars of the "in" xbuf is used starting from its | |
151 | * ".pos". The ".size" of the "out" xbuf restricts how many characters can | |
152 | * be stored, starting at its ".pos+.len" position. Note that the last byte | |
153 | * of the "out" xbuf is not used, which reserves space for a trailing '\0' | |
154 | * (though it is up to the caller to store a trailing '\0', as needed). | |
155 | * | |
2ac97930 WD |
156 | * We return a 0 on success or a -1 on error. An error also sets errno to |
157 | * E2BIG, EILSEQ, or EINVAL (see below); otherwise errno will be set to 0. | |
907e6a32 WD |
158 | * The "in" xbuf is altered to update ".pos" and ".len". The "out" xbuf has |
159 | * data appended, and its ".len" incremented (see below for a ".size" note). | |
160 | * | |
161 | * If ICB_CIRCULAR_OUT is set in "flags", the chars going into the "out" xbuf | |
162 | * can wrap around to the start, and the xbuf may have its ".size" reduced | |
163 | * (presumably by 1 byte) if the iconv code doesn't have space to store a | |
164 | * multi-byte character at the physical end of the ".buf" (though no reducing | |
d03c0b1e | 165 | * happens if ".pos" is <= 1, since there is no room to wrap around). |
907e6a32 WD |
166 | * |
167 | * If ICB_EXPAND_OUT is set in "flags", the "out" xbuf will be allocated if | |
168 | * empty, and (as long as ICB_CIRCULAR_OUT is not set) expanded if too small. | |
169 | * This prevents the return of E2BIG (except for a circular xbuf). | |
170 | * | |
171 | * If ICB_INCLUDE_BAD is set in "flags", any badly-encoded chars are included | |
172 | * verbatim in the "out" xbuf, so EILSEQ will not be returned. | |
173 | * | |
174 | * If ICB_INCLUDE_INCOMPLETE is set in "flags", any incomplete multi-byte | |
175 | * chars are included, which ensures that EINVAL is not returned. | |
176 | * | |
177 | * If ICB_INIT is set, the iconv() conversion state is initialized prior to | |
178 | * processing the characters. */ | |
2ac97930 WD |
179 | int iconvbufs(iconv_t ic, xbuf *in, xbuf *out, int flags) |
180 | { | |
181 | ICONV_CONST char *ibuf; | |
20caffd2 | 182 | size_t icnt, ocnt, opos; |
2ac97930 WD |
183 | char *obuf; |
184 | ||
907e6a32 WD |
185 | if (!out->size && flags & ICB_EXPAND_OUT) { |
186 | size_t siz = ROUND_UP_1024(in->len * 2); | |
187 | alloc_xbuf(out, siz); | |
188 | } else if (out->len+1 >= out->size) { | |
189 | /* There is no room to even start storing data. */ | |
190 | if (!(flags & ICB_EXPAND_OUT) || flags & ICB_CIRCULAR_OUT) { | |
191 | errno = E2BIG; | |
192 | return -1; | |
193 | } | |
194 | realloc_xbuf(out, out->size + ROUND_UP_1024(in->len * 2)); | |
195 | } | |
2ac97930 | 196 | |
d8a7290f | 197 | if (flags & ICB_INIT) |
2ac97930 WD |
198 | iconv(ic, NULL, 0, NULL, 0); |
199 | ||
200 | ibuf = in->buf + in->pos; | |
201 | icnt = in->len; | |
202 | ||
20caffd2 WD |
203 | opos = out->pos + out->len; |
204 | if (flags & ICB_CIRCULAR_OUT) { | |
205 | if (opos >= out->size) { | |
206 | opos -= out->size; | |
907e6a32 WD |
207 | /* We know that out->pos is not 0 due to the "no room" check |
208 | * above, so this can't go "negative". */ | |
20caffd2 WD |
209 | ocnt = out->pos - opos - 1; |
210 | } else { | |
907e6a32 WD |
211 | /* Allow the use of all bytes to the physical end of the buffer |
212 | * unless pos is 0, in which case we reserve our trailing '\0'. */ | |
213 | ocnt = out->size - opos - (out->pos ? 0 : 1); | |
20caffd2 WD |
214 | } |
215 | } else | |
216 | ocnt = out->size - opos - 1; | |
217 | obuf = out->buf + opos; | |
2ac97930 WD |
218 | |
219 | while (icnt) { | |
220 | while (iconv(ic, &ibuf, &icnt, &obuf, &ocnt) == (size_t)-1) { | |
221 | if (errno == EINTR) | |
222 | continue; | |
223 | if (errno == EINVAL) { | |
224 | if (!(flags & ICB_INCLUDE_INCOMPLETE)) | |
225 | goto finish; | |
cb784f18 WD |
226 | if (!ocnt) |
227 | goto e2big; | |
2ac97930 WD |
228 | } else if (errno == EILSEQ) { |
229 | if (!(flags & ICB_INCLUDE_BAD)) | |
230 | goto finish; | |
cb784f18 WD |
231 | if (!ocnt) |
232 | goto e2big; | |
907e6a32 WD |
233 | } else if (errno == E2BIG) { |
234 | size_t siz; | |
cb784f18 | 235 | e2big: |
20caffd2 | 236 | opos = obuf - out->buf; |
907e6a32 WD |
237 | if (flags & ICB_CIRCULAR_OUT && out->pos > 1 && opos > out->pos) { |
238 | /* We are in a divided circular buffer at the physical | |
239 | * end with room to wrap to the start. If iconv() refused | |
240 | * to use one or more trailing bytes in the buffer, we | |
241 | * set the size to ignore the unused bytes. */ | |
242 | if (opos < out->size) | |
243 | reduce_iobuf_size(out, opos); | |
244 | obuf = out->buf; | |
245 | ocnt = out->pos - 1; | |
246 | continue; | |
20caffd2 WD |
247 | } |
248 | if (!(flags & ICB_EXPAND_OUT) || flags & ICB_CIRCULAR_OUT) { | |
2ac97930 WD |
249 | errno = E2BIG; |
250 | goto finish; | |
251 | } | |
907e6a32 WD |
252 | siz = ROUND_UP_1024(in->len * 2); |
253 | realloc_xbuf(out, out->size + siz); | |
2ac97930 | 254 | obuf = out->buf + opos; |
907e6a32 | 255 | ocnt += siz; |
2ac97930 | 256 | continue; |
907e6a32 WD |
257 | } else { |
258 | rsyserr(FERROR, errno, "unexpected error from iconv()"); | |
259 | exit_cleanup(RERR_UNSUPPORTED); | |
2ac97930 WD |
260 | } |
261 | *obuf++ = *ibuf++; | |
262 | ocnt--, icnt--; | |
cb784f18 WD |
263 | if (!icnt) |
264 | break; | |
2ac97930 WD |
265 | } |
266 | } | |
267 | ||
268 | errno = 0; | |
269 | ||
270 | finish: | |
20caffd2 | 271 | opos = obuf - out->buf; |
907e6a32 WD |
272 | if (flags & ICB_CIRCULAR_OUT && opos < out->pos) |
273 | opos += out->size; | |
20caffd2 WD |
274 | out->len = opos - out->pos; |
275 | ||
2ac97930 WD |
276 | in->len = icnt; |
277 | in->pos = ibuf - in->buf; | |
2ac97930 WD |
278 | |
279 | return errno ? -1 : 0; | |
280 | } | |
ceccbacc | 281 | #endif |
fe055c71 | 282 | |
53936ef9 WD |
283 | void send_protected_args(int fd, char *args[]) |
284 | { | |
7abcfd85 | 285 | int i; |
53936ef9 | 286 | #ifdef ICONV_OPTION |
7abcfd85 | 287 | int convert = ic_send != (iconv_t)-1; |
53936ef9 WD |
288 | xbuf outbuf, inbuf; |
289 | ||
290 | if (convert) | |
291 | alloc_xbuf(&outbuf, 1024); | |
292 | #endif | |
293 | ||
294 | for (i = 0; args[i]; i++) {} /* find first NULL */ | |
295 | args[i] = "rsync"; /* set a new arg0 */ | |
951e826b | 296 | if (DEBUG_GTE(CMD, 1)) |
53936ef9 WD |
297 | print_child_argv("protected args:", args + i + 1); |
298 | do { | |
08b7c3ed WD |
299 | if (!args[i][0]) |
300 | write_buf(fd, ".", 2); | |
53936ef9 | 301 | #ifdef ICONV_OPTION |
08b7c3ed | 302 | else if (convert) { |
53936ef9 WD |
303 | INIT_XBUF_STRLEN(inbuf, args[i]); |
304 | iconvbufs(ic_send, &inbuf, &outbuf, | |
d8a7290f | 305 | ICB_EXPAND_OUT | ICB_INCLUDE_BAD | ICB_INCLUDE_INCOMPLETE | ICB_INIT); |
53936ef9 WD |
306 | outbuf.buf[outbuf.len] = '\0'; |
307 | write_buf(fd, outbuf.buf, outbuf.len + 1); | |
308 | outbuf.len = 0; | |
08b7c3ed | 309 | } |
53936ef9 | 310 | #endif |
08b7c3ed | 311 | else |
53936ef9 WD |
312 | write_buf(fd, args[i], strlen(args[i]) + 1); |
313 | } while (args[++i]); | |
314 | write_byte(fd, 0); | |
315 | ||
316 | #ifdef ICONV_OPTION | |
317 | if (convert) | |
318 | free(outbuf.buf); | |
319 | #endif | |
320 | } | |
321 | ||
e63ff70e | 322 | int read_ndx_and_attrs(int f_in, int f_out, int *iflag_ptr, uchar *type_ptr, char *buf, int *len_ptr) |
d1c178dd | 323 | { |
f3d6d480 WD |
324 | int len, iflags = 0; |
325 | struct file_list *flist; | |
d1c178dd | 326 | uchar fnamecmp_type = FNAMECMP_FNAME; |
951e826b | 327 | int ndx; |
f3d6d480 WD |
328 | |
329 | read_loop: | |
330 | while (1) { | |
9ae7a2cd | 331 | ndx = read_ndx(f_in); |
f3d6d480 | 332 | |
8b3e6052 | 333 | if (ndx >= 0) |
f3d6d480 WD |
334 | break; |
335 | if (ndx == NDX_DONE) | |
336 | return ndx; | |
20caffd2 WD |
337 | if (ndx == NDX_DEL_STATS) { |
338 | read_del_stats(f_in); | |
339 | if (am_sender && am_server) | |
340 | write_del_stats(f_out); | |
341 | continue; | |
342 | } | |
e982d591 WD |
343 | if (!inc_recurse || am_sender) { |
344 | int last; | |
345 | if (first_flist) | |
346 | last = first_flist->prev->ndx_start + first_flist->prev->used - 1; | |
347 | else | |
348 | last = -1; | |
349 | rprintf(FERROR, | |
350 | "Invalid file index: %d (%d - %d) [%s]\n", | |
351 | ndx, NDX_DONE, last, who_am_i()); | |
352 | exit_cleanup(RERR_PROTOCOL); | |
353 | } | |
f3d6d480 WD |
354 | if (ndx == NDX_FLIST_EOF) { |
355 | flist_eof = 1; | |
20caffd2 WD |
356 | if (DEBUG_GTE(FLIST, 3)) |
357 | rprintf(FINFO, "[%s] flist_eof=1\n", who_am_i()); | |
358 | write_int(f_out, NDX_FLIST_EOF); | |
f3d6d480 WD |
359 | continue; |
360 | } | |
361 | ndx = NDX_FLIST_OFFSET - ndx; | |
9decb4d2 | 362 | if (ndx < 0 || ndx >= dir_flist->used) { |
f3d6d480 WD |
363 | ndx = NDX_FLIST_OFFSET - ndx; |
364 | rprintf(FERROR, | |
e982d591 WD |
365 | "Invalid dir index: %d (%d - %d) [%s]\n", |
366 | ndx, NDX_FLIST_OFFSET, | |
367 | NDX_FLIST_OFFSET - dir_flist->used + 1, | |
368 | who_am_i()); | |
f3d6d480 WD |
369 | exit_cleanup(RERR_PROTOCOL); |
370 | } | |
f3d6d480 | 371 | |
951e826b | 372 | if (DEBUG_GTE(FLIST, 2)) { |
1faa1a6d WD |
373 | rprintf(FINFO, "[%s] receiving flist for dir %d\n", |
374 | who_am_i(), ndx); | |
375 | } | |
20caffd2 WD |
376 | /* Send all the data we read for this flist to the generator. */ |
377 | start_flist_forward(ndx); | |
962f8b90 | 378 | flist = recv_file_list(f_in, ndx); |
f3d6d480 WD |
379 | flist->parent_ndx = ndx; |
380 | stop_flist_forward(); | |
f3d6d480 WD |
381 | } |
382 | ||
383 | iflags = protocol_version >= 29 ? read_shortint(f_in) | |
d1c178dd WD |
384 | : ITEM_TRANSFER | ITEM_MISSING_DATA; |
385 | ||
92d02148 WD |
386 | /* Support the protocol-29 keep-alive style. */ |
387 | if (protocol_version < 30 && ndx == cur_flist->used && iflags == ITEM_IS_NEW) { | |
f3d6d480 | 388 | if (am_sender) |
05c36015 | 389 | maybe_send_keepalive(time(NULL), MSK_ALLOW_FLUSH); |
f3d6d480 WD |
390 | goto read_loop; |
391 | } | |
392 | ||
ce827c3e WD |
393 | flist = flist_for_ndx(ndx, "read_ndx_and_attrs"); |
394 | if (flist != cur_flist) { | |
395 | cur_flist = flist; | |
396 | if (am_sender) { | |
397 | file_old_total = cur_flist->used; | |
398 | for (flist = first_flist; flist != cur_flist; flist = flist->next) | |
399 | file_old_total += flist->used; | |
400 | } | |
401 | } | |
d1c178dd WD |
402 | |
403 | if (iflags & ITEM_BASIS_TYPE_FOLLOWS) | |
404 | fnamecmp_type = read_byte(f_in); | |
405 | *type_ptr = fnamecmp_type; | |
406 | ||
407 | if (iflags & ITEM_XNAME_FOLLOWS) { | |
408 | if ((len = read_vstring(f_in, buf, MAXPATHLEN)) < 0) | |
409 | exit_cleanup(RERR_PROTOCOL); | |
70aeb5fd JO |
410 | |
411 | if (sanitize_paths) { | |
412 | sanitize_path(buf, buf, "", 0, SP_DEFAULT); | |
413 | len = strlen(buf); | |
414 | } | |
d1c178dd WD |
415 | } else { |
416 | *buf = '\0'; | |
417 | len = -1; | |
418 | } | |
419 | *len_ptr = len; | |
420 | ||
421 | if (iflags & ITEM_TRANSFER) { | |
f3d6d480 | 422 | int i = ndx - cur_flist->ndx_start; |
6755a7d7 | 423 | if (i < 0 || !S_ISREG(cur_flist->files[i]->mode)) { |
d1c178dd WD |
424 | rprintf(FERROR, |
425 | "received request to transfer non-regular file: %d [%s]\n", | |
426 | ndx, who_am_i()); | |
427 | exit_cleanup(RERR_PROTOCOL); | |
428 | } | |
d1c178dd WD |
429 | } |
430 | ||
f3d6d480 WD |
431 | *iflag_ptr = iflags; |
432 | return ndx; | |
d1c178dd WD |
433 | } |
434 | ||
c627d613 AT |
435 | /* |
436 | free a sums struct | |
437 | */ | |
2f03f956 | 438 | void free_sums(struct sum_struct *s) |
c627d613 | 439 | { |
298c10d5 AT |
440 | if (s->sums) free(s->sums); |
441 | free(s); | |
c627d613 AT |
442 | } |
443 | ||
81284832 WD |
444 | /* This is only called when we aren't preserving permissions. Figure out what |
445 | * the permissions should be and return them merged back into the mode. */ | |
1c3344a1 WD |
446 | mode_t dest_mode(mode_t flist_mode, mode_t stat_mode, int dflt_perms, |
447 | int exists) | |
81284832 | 448 | { |
67f8a41b | 449 | int new_mode; |
9b499e95 | 450 | /* If the file already exists, we'll return the local permissions, |
81284832 WD |
451 | * possibly tweaked by the --executability option. */ |
452 | if (exists) { | |
67f8a41b | 453 | new_mode = (flist_mode & ~CHMOD_BITS) | (stat_mode & CHMOD_BITS); |
81284832 WD |
454 | if (preserve_executability && S_ISREG(flist_mode)) { |
455 | /* If the source file is executable, grant execute | |
456 | * rights to everyone who can read, but ONLY if the | |
457 | * file isn't already executable. */ | |
458 | if (!(flist_mode & 0111)) | |
67f8a41b WD |
459 | new_mode &= ~0111; |
460 | else if (!(stat_mode & 0111)) | |
461 | new_mode |= (new_mode & 0444) >> 2; | |
81284832 | 462 | } |
67f8a41b | 463 | } else { |
1c3344a1 WD |
464 | /* Apply destination default permissions and turn |
465 | * off special permissions. */ | |
466 | new_mode = flist_mode & (~CHMOD_BITS | dflt_perms); | |
67f8a41b | 467 | } |
67f8a41b | 468 | return new_mode; |
81284832 WD |
469 | } |
470 | ||
d3269612 WD |
471 | static int same_mtime(struct file_struct *file, STRUCT_STAT *st, int extra_accuracy) |
472 | { | |
473 | #ifdef ST_MTIME_NSEC | |
474 | uint32 f1_nsec = F_MOD_NSEC_or_0(file); | |
475 | uint32 f2_nsec = (uint32)st->ST_MTIME_NSEC; | |
476 | #else | |
477 | uint32 f1_nsec = 0, f2_nsec = 0; | |
478 | #endif | |
479 | ||
480 | if (extra_accuracy) /* ignore modify_window when setting the time after a transfer or checksum check */ | |
481 | return file->modtime == st->st_mtime && f1_nsec == f2_nsec; | |
482 | ||
483 | return same_time(file->modtime, f1_nsec, st->st_mtime , f2_nsec); | |
484 | } | |
485 | ||
13710874 | 486 | int set_file_attrs(const char *fname, struct file_struct *file, stat_x *sxp, |
16edf865 | 487 | const char *fnamecmp, int flags) |
c627d613 | 488 | { |
667e72a1 | 489 | int updated = 0; |
13710874 | 490 | stat_x sx2; |
460f6b99 | 491 | int change_uid, change_gid; |
519d55a9 | 492 | mode_t new_mode = file->mode; |
2dc7b91d | 493 | int inherit; |
c627d613 | 494 | |
1c3344a1 | 495 | if (!sxp) { |
f227ffe4 WD |
496 | if (dry_run) |
497 | return 1; | |
1c3344a1 | 498 | if (link_stat(fname, &sx2.st, 0) < 0) { |
3f0211b6 | 499 | rsyserr(FERROR_XFER, errno, "stat %s failed", |
d62bcc17 | 500 | full_fname(fname)); |
667e72a1 AT |
501 | return 0; |
502 | } | |
09ca0d15 | 503 | init_stat_x(&sx2); |
1c3344a1 | 504 | sxp = &sx2; |
2dc7b91d WD |
505 | inherit = !preserve_perms; |
506 | } else | |
507 | inherit = !preserve_perms && file->flags & FLAG_DIR_CREATED; | |
508 | ||
509 | if (inherit && S_ISDIR(new_mode) && sxp->st.st_mode & S_ISGID) { | |
510 | /* We just created this directory and its setgid | |
511 | * bit is on, so make sure it stays on. */ | |
512 | new_mode |= S_ISGID; | |
667e72a1 | 513 | } |
c627d613 | 514 | |
e107b6b1 WD |
515 | if (daemon_chmod_modes && !S_ISLNK(new_mode)) |
516 | new_mode = tweak_mode(new_mode, daemon_chmod_modes); | |
517 | ||
1c3344a1 WD |
518 | #ifdef SUPPORT_ACLS |
519 | if (preserve_acls && !S_ISLNK(file->mode) && !ACL_READY(*sxp)) | |
520 | get_acl(fname, sxp); | |
521 | #endif | |
522 | ||
9b25ef35 WD |
523 | change_uid = am_root && uid_ndx && sxp->st.st_uid != (uid_t)F_OWNER(file); |
524 | change_gid = gid_ndx && !(file->flags & FLAG_SKIP_GROUP) | |
858d45f1 | 525 | && sxp->st.st_gid != (gid_t)F_GROUP(file); |
743348e8 | 526 | #ifndef CAN_CHOWN_SYMLINK |
2e52ba36 | 527 | if (S_ISLNK(sxp->st.st_mode)) { |
a41a1e87 | 528 | ; |
2e52ba36 | 529 | } else |
a41a1e87 | 530 | #endif |
460f6b99 | 531 | if (change_uid || change_gid) { |
951e826b | 532 | if (DEBUG_GTE(OWN, 1)) { |
ce37eb2d WD |
533 | if (change_uid) { |
534 | rprintf(FINFO, | |
4ade505c WD |
535 | "set uid of %s from %u to %u\n", |
536 | fname, (unsigned)sxp->st.st_uid, F_OWNER(file)); | |
ce37eb2d WD |
537 | } |
538 | if (change_gid) { | |
539 | rprintf(FINFO, | |
4ade505c WD |
540 | "set gid of %s from %u to %u\n", |
541 | fname, (unsigned)sxp->st.st_gid, F_GROUP(file)); | |
ce37eb2d WD |
542 | } |
543 | } | |
87629cf2 | 544 | if (am_root >= 0) { |
eee2c77a WD |
545 | uid_t uid = change_uid ? (uid_t)F_OWNER(file) : sxp->st.st_uid; |
546 | gid_t gid = change_gid ? (gid_t)F_GROUP(file) : sxp->st.st_gid; | |
547 | if (do_lchown(fname, uid, gid) != 0) { | |
87629cf2 WD |
548 | /* We shouldn't have attempted to change uid |
549 | * or gid unless have the privilege. */ | |
550 | rsyserr(FERROR_XFER, errno, "%s %s failed", | |
e63ff70e WD |
551 | change_uid ? "chown" : "chgrp", |
552 | full_fname(fname)); | |
87629cf2 WD |
553 | goto cleanup; |
554 | } | |
eee2c77a WD |
555 | if (uid == (uid_t)-1 && sxp->st.st_uid != (uid_t)-1) |
556 | rprintf(FERROR_XFER, "uid 4294967295 (-1) is impossible to set on %s\n", full_fname(fname)); | |
557 | if (gid == (gid_t)-1 && sxp->st.st_gid != (gid_t)-1) | |
558 | rprintf(FERROR_XFER, "gid 4294967295 (-1) is impossible to set on %s\n", full_fname(fname)); | |
87629cf2 WD |
559 | /* A lchown had been done, so we need to re-stat if |
560 | * the destination had the setuid or setgid bits set | |
561 | * (due to the side effect of the chown call). */ | |
562 | if (sxp->st.st_mode & (S_ISUID | S_ISGID)) { | |
563 | link_stat(fname, &sxp->st, | |
564 | keep_dirlinks && S_ISDIR(sxp->st.st_mode)); | |
565 | } | |
a5827a28 | 566 | } |
b9367410 | 567 | if (change_uid) |
e63ff70e | 568 | updated |= UPDATED_OWNER; |
b9367410 | 569 | if (change_gid) |
e63ff70e | 570 | updated |= UPDATED_GROUP; |
667e72a1 | 571 | } |
c627d613 | 572 | |
0e3152fe WD |
573 | #ifdef SUPPORT_XATTRS |
574 | if (am_root < 0) | |
575 | set_stat_xattr(fname, file, new_mode); | |
576 | if (preserve_xattrs && fnamecmp) | |
577 | set_xattr(fname, file, fnamecmp, sxp); | |
578 | #endif | |
579 | ||
b774dbc1 WD |
580 | if ((omit_dir_times && S_ISDIR(sxp->st.st_mode)) |
581 | || (omit_link_times && S_ISLNK(sxp->st.st_mode))) | |
296352ec | 582 | flags |= ATTRS_SKIP_MTIME | ATTRS_SKIP_ATIME | ATTRS_SKIP_CRTIME; |
0f87eafa WD |
583 | else { |
584 | if (!preserve_mtimes) | |
585 | flags |= ATTRS_SKIP_MTIME; | |
586 | if (!atimes_ndx || S_ISDIR(sxp->st.st_mode)) | |
587 | flags |= ATTRS_SKIP_ATIME; | |
588 | /* Don't set the creation date on the root folder of an HFS+ volume. */ | |
589 | if (sxp->st.st_ino == 2 && S_ISDIR(sxp->st.st_mode)) | |
590 | flags |= ATTRS_SKIP_CRTIME; | |
591 | } | |
3af00277 | 592 | if (sxp != &sx2) |
b774dbc1 | 593 | memcpy(&sx2.st, &sxp->st, sizeof sx2.st); |
d3269612 | 594 | if (!(flags & ATTRS_SKIP_MTIME) && !same_mtime(file, &sxp->st, flags & ATTRS_ACCURATE_TIME)) { |
b9367410 WD |
595 | sx2.st.st_mtime = file->modtime; |
596 | #ifdef ST_MTIME_NSEC | |
597 | sx2.st.ST_MTIME_NSEC = F_MOD_NSEC_or_0(file); | |
598 | #endif | |
599 | updated |= UPDATED_MTIME; | |
600 | } | |
601 | if (!(flags & ATTRS_SKIP_ATIME)) { | |
602 | time_t file_atime = F_ATIME(file); | |
d3269612 | 603 | if (flags & ATTRS_ACCURATE_TIME || !same_time(sxp->st.st_atime, 0, file_atime, 0)) { |
b9367410 WD |
604 | sx2.st.st_atime = file_atime; |
605 | #ifdef ST_ATIME_NSEC | |
606 | sx2.st.ST_ATIME_NSEC = 0; | |
607 | #endif | |
608 | updated |= UPDATED_ATIME; | |
609 | } | |
610 | } | |
296352ec WD |
611 | #ifdef SUPPORT_CRTIMES |
612 | if (crtimes_ndx && !(flags & ATTRS_SKIP_CRTIME)) { | |
613 | time_t file_crtime = F_CRTIME(file); | |
614 | if (sxp->crtime == 0) | |
615 | sxp->crtime = get_create_time(fname, &sxp->st); | |
616 | if (!same_time(sxp->crtime, 0L, file_crtime, 0L)) { | |
617 | if ( | |
618 | #ifdef HAVE_GETATTRLIST | |
619 | do_setattrlist_crtime(fname, file_crtime) == 0 | |
620 | #elif defined __CYGWIN__ | |
621 | do_SetFileTime(fname, file_crtime) == 0 | |
622 | #else | |
623 | #error Unknown crtimes implementation | |
624 | #endif | |
625 | ) | |
626 | updated |= UPDATED_CRTIME; | |
627 | } | |
628 | } | |
629 | #endif | |
630 | if (updated & (UPDATED_MTIME|UPDATED_ATIME)) { | |
b9367410 | 631 | int ret = set_times(fname, &sx2.st); |
0e3152fe | 632 | if (ret < 0) { |
296352ec | 633 | rsyserr(FERROR_XFER, errno, "failed to set times on %s", full_fname(fname)); |
0e3152fe WD |
634 | goto cleanup; |
635 | } | |
b9367410 | 636 | if (ret > 0) { /* ret == 1 if symlink could not be set */ |
296352ec | 637 | updated &= ~(UPDATED_MTIME|UPDATED_ATIME); |
0e3152fe | 638 | file->flags |= FLAG_TIME_FAILED; |
b9367410 | 639 | } |
0e3152fe WD |
640 | } |
641 | ||
1c3344a1 WD |
642 | #ifdef SUPPORT_ACLS |
643 | /* It's OK to call set_acl() now, even for a dir, as the generator | |
644 | * will enable owner-writability using chmod, if necessary. | |
9cb7529b | 645 | * |
1c3344a1 WD |
646 | * If set_acl() changes permission bits in the process of setting |
647 | * an access ACL, it changes sxp->st.st_mode so we know whether we | |
648 | * need to chmod(). */ | |
ee1c00fe WD |
649 | if (preserve_acls && !S_ISLNK(new_mode)) { |
650 | if (set_acl(fname, file, sxp, new_mode) > 0) | |
b9367410 | 651 | updated |= UPDATED_ACLS; |
ee1c00fe | 652 | } |
1c3344a1 WD |
653 | #endif |
654 | ||
4f5b0756 | 655 | #ifdef HAVE_CHMOD |
1c3344a1 | 656 | if (!BITS_EQUAL(sxp->st.st_mode, new_mode, CHMOD_BITS)) { |
9439c0cb | 657 | int ret = am_root < 0 ? 0 : do_chmod(fname, new_mode); |
c8d34657 | 658 | if (ret < 0) { |
3f0211b6 | 659 | rsyserr(FERROR_XFER, errno, |
c8d34657 WD |
660 | "failed to set permissions on %s", |
661 | full_fname(fname)); | |
1c3344a1 | 662 | goto cleanup; |
667e72a1 | 663 | } |
c8d34657 | 664 | if (ret == 0) /* ret == 1 if symlink could not be set */ |
b9367410 | 665 | updated |= UPDATED_MODE; |
667e72a1 | 666 | } |
c627d613 | 667 | #endif |
6a7cc46c | 668 | |
951e826b | 669 | if (INFO_GTE(NAME, 2) && flags & ATTRS_REPORT) { |
667e72a1 | 670 | if (updated) |
1925c344 | 671 | rprintf(FCLIENT, "%s\n", fname); |
667e72a1 | 672 | else |
1925c344 | 673 | rprintf(FCLIENT, "%s is uptodate\n", fname); |
667e72a1 | 674 | } |
1c3344a1 | 675 | cleanup: |
d42e7181 WD |
676 | if (sxp == &sx2) |
677 | free_stat_x(&sx2); | |
667e72a1 | 678 | return updated; |
c627d613 AT |
679 | } |
680 | ||
d4070db6 | 681 | /* This is only called for SIGINT, SIGHUP, and SIGTERM. */ |
23afe207 | 682 | void sig_int(int sig_num) |
34ccb63e | 683 | { |
b32aa479 WD |
684 | called_from_signal_handler = 1; |
685 | ||
d5a0b483 WD |
686 | /* KLUGE: if the user hits Ctrl-C while ssh is prompting |
687 | * for a password, then our cleanup's sending of a SIGUSR1 | |
688 | * signal to all our children may kill ssh before it has a | |
689 | * chance to restore the tty settings (i.e. turn echo back | |
690 | * on). By sleeping for a short time, ssh gets a bigger | |
691 | * chance to do the right thing. If child processes are | |
692 | * not ssh waiting for a password, then this tiny delay | |
693 | * shouldn't hurt anything. */ | |
694 | msleep(400); | |
d4070db6 | 695 | |
0d34fbdf WD |
696 | /* If we're an rsync daemon listener (not a daemon server), |
697 | * we'll exit with status 0 if we received SIGTERM. */ | |
698 | if (am_daemon && !am_server && sig_num == SIGTERM) | |
699 | exit_cleanup(0); | |
d4070db6 WD |
700 | |
701 | /* If the signal arrived on the server side (or for the receiver | |
702 | * process on the client), we want to try to do a controlled shutdown | |
703 | * that lets the client side (generator process) know what happened. | |
704 | * To do this, we set a flag and let the normal process handle the | |
705 | * shutdown. We only attempt this if multiplexed IO is in effect and | |
706 | * we didn't already set the flag. */ | |
707 | if (!got_kill_signal && (am_server || am_receiver)) { | |
708 | got_kill_signal = sig_num; | |
b32aa479 | 709 | called_from_signal_handler = 0; |
d4070db6 WD |
710 | return; |
711 | } | |
712 | ||
65417579 | 713 | exit_cleanup(RERR_SIGNAL); |
c627d613 AT |
714 | } |
715 | ||
d8b1c923 | 716 | /* Finish off a file transfer: renaming the file and setting the file's |
83235dbc WD |
717 | * attributes (e.g. permissions, ownership, etc.). If the robust_rename() |
718 | * call is forced to copy the temp file and partialptr is both non-NULL and | |
719 | * not an absolute path, we stage the file into the partial-dir and then | |
d2970213 | 720 | * rename it into place. This returns 1 on success or 0 on failure. */ |
83235dbc WD |
721 | int finish_transfer(const char *fname, const char *fnametmp, |
722 | const char *fnamecmp, const char *partialptr, | |
723 | struct file_struct *file, int ok_to_set_time, | |
724 | int overwriting_basis) | |
c95da96a | 725 | { |
62c9e6b3 | 726 | int ret; |
83235dbc | 727 | const char *temp_copy_name = partialptr && *partialptr != '/' ? partialptr : NULL; |
62c9e6b3 | 728 | |
a3221d2a | 729 | if (inplace) { |
951e826b | 730 | if (DEBUG_GTE(RECV, 1)) |
45c49b52 | 731 | rprintf(FINFO, "finishing %s\n", fname); |
d8b1c923 | 732 | fnametmp = fname; |
36119f6e | 733 | goto do_set_file_attrs; |
a3221d2a WD |
734 | } |
735 | ||
e9489cd6 | 736 | if (make_backups > 0 && overwriting_basis) { |
21cddef2 WD |
737 | int ok = make_backup(fname, False); |
738 | if (!ok) | |
b973bffa | 739 | exit_cleanup(RERR_FILEIO); |
21cddef2 | 740 | if (ok == 1 && fnamecmp == fname) |
4337eeb7 | 741 | fnamecmp = get_backup_name(fname); |
e9489cd6 | 742 | } |
e484f0cc | 743 | |
ebeacb36 | 744 | /* Change permissions before putting the file into place. */ |
16edf865 | 745 | set_file_attrs(fnametmp, file, NULL, fnamecmp, |
974f49e2 | 746 | ok_to_set_time ? ATTRS_ACCURATE_TIME : ATTRS_SKIP_MTIME | ATTRS_SKIP_ATIME | ATTRS_SKIP_CRTIME); |
ebeacb36 | 747 | |
c95da96a | 748 | /* move tmp file over real file */ |
951e826b | 749 | if (DEBUG_GTE(RECV, 1)) |
45c49b52 | 750 | rprintf(FINFO, "renaming %s to %s\n", fnametmp, fname); |
3b22184d | 751 | ret = robust_rename(fnametmp, fname, temp_copy_name, file->mode); |
3d061653 | 752 | if (ret < 0) { |
3f0211b6 | 753 | rsyserr(FERROR_XFER, errno, "%s %s -> \"%s\"", |
45c49b52 WD |
754 | ret == -2 ? "copy" : "rename", |
755 | full_fname(fnametmp), fname); | |
83235dbc | 756 | if (!partialptr || (ret == -2 && temp_copy_name) |
3b22184d | 757 | || robust_rename(fnametmp, partialptr, NULL, file->mode) < 0) |
83235dbc WD |
758 | do_unlink(fnametmp); |
759 | return 0; | |
c95da96a | 760 | } |
ebeacb36 WD |
761 | if (ret == 0) { |
762 | /* The file was moved into place (not copied), so it's done. */ | |
83235dbc | 763 | return 1; |
ebeacb36 | 764 | } |
d8b1c923 WD |
765 | /* The file was copied, so tweak the perms of the copied file. If it |
766 | * was copied to partialptr, move it into its final destination. */ | |
83235dbc | 767 | fnametmp = temp_copy_name ? temp_copy_name : fname; |
d8b1c923 | 768 | |
36119f6e | 769 | do_set_file_attrs: |
16edf865 | 770 | set_file_attrs(fnametmp, file, NULL, fnamecmp, |
974f49e2 | 771 | ok_to_set_time ? ATTRS_ACCURATE_TIME : ATTRS_SKIP_MTIME | ATTRS_SKIP_ATIME | ATTRS_SKIP_CRTIME); |
d8b1c923 | 772 | |
83235dbc | 773 | if (temp_copy_name) { |
d8b1c923 | 774 | if (do_rename(fnametmp, fname) < 0) { |
3f0211b6 | 775 | rsyserr(FERROR_XFER, errno, "rename %s -> \"%s\"", |
d8b1c923 | 776 | full_fname(fnametmp), fname); |
83235dbc WD |
777 | return 0; |
778 | } | |
779 | handle_partial_dir(temp_copy_name, PDIR_DELETE); | |
d8b1c923 | 780 | } |
83235dbc | 781 | return 1; |
c95da96a | 782 | } |
c3e5e585 | 783 | |
e982d591 | 784 | struct file_list *flist_for_ndx(int ndx, const char *fatal_error_loc) |
f3d6d480 WD |
785 | { |
786 | struct file_list *flist = cur_flist; | |
787 | ||
87a34ce5 | 788 | if (!flist && !(flist = first_flist)) |
e982d591 | 789 | goto not_found; |
f3d6d480 | 790 | |
6755a7d7 | 791 | while (ndx < flist->ndx_start-1) { |
f3d6d480 | 792 | if (flist == first_flist) |
e982d591 | 793 | goto not_found; |
f3d6d480 WD |
794 | flist = flist->prev; |
795 | } | |
65b4e4b2 | 796 | while (ndx >= flist->ndx_start + flist->used) { |
f3d6d480 | 797 | if (!(flist = flist->next)) |
e982d591 | 798 | goto not_found; |
f3d6d480 WD |
799 | } |
800 | return flist; | |
e982d591 WD |
801 | |
802 | not_found: | |
803 | if (fatal_error_loc) { | |
804 | int first, last; | |
805 | if (first_flist) { | |
806 | first = first_flist->ndx_start - 1; | |
807 | last = first_flist->prev->ndx_start + first_flist->prev->used - 1; | |
808 | } else { | |
809 | first = 0; | |
810 | last = -1; | |
811 | } | |
812 | rprintf(FERROR, | |
813 | "File-list index %d not in %d - %d (%s) [%s]\n", | |
814 | ndx, first, last, fatal_error_loc, who_am_i()); | |
815 | exit_cleanup(RERR_PROTOCOL); | |
816 | } | |
817 | return NULL; | |
f3d6d480 WD |
818 | } |
819 | ||
c3e5e585 WD |
820 | const char *who_am_i(void) |
821 | { | |
ed9d969c | 822 | if (am_starting_up) |
794b0a03 | 823 | return am_server ? "server" : "client"; |
82b2a31a WD |
824 | return am_sender ? "sender" |
825 | : am_generator ? "generator" | |
826 | : am_receiver ? "receiver" | |
827 | : "Receiver"; /* pre-forked receiver */ | |
c3e5e585 | 828 | } |