| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#include "rsync.h" |
| 23 |
#include <zlib.h> |
| 24 |
#include <time.h> |
| 25 |
|
| 26 |
extern int eol_nulls; |
| 27 |
extern int recurse; |
| 28 |
extern int xfer_dirs; |
| 29 |
extern int preserve_links; |
| 30 |
extern int preserve_hard_links; |
| 31 |
extern int preserve_devices; |
| 32 |
extern int preserve_uid; |
| 33 |
extern int preserve_gid; |
| 34 |
extern int preserve_acls; |
| 35 |
extern int preserve_xattrs; |
| 36 |
extern int always_checksum; |
| 37 |
extern int do_compression; |
| 38 |
extern int inplace; |
| 39 |
extern int append_mode; |
| 40 |
extern int write_batch; |
| 41 |
extern int protocol_version; |
| 42 |
extern int raw_argc, cooked_argc; |
| 43 |
extern char **raw_argv, **cooked_argv; |
| 44 |
extern char *batch_name; |
| 45 |
#ifdef ICONV_OPTION |
| 46 |
extern char *iconv_opt; |
| 47 |
#endif |
| 48 |
|
| 49 |
extern filter_rule_list filter_list; |
| 50 |
|
| 51 |
int batch_fd = -1; |
| 52 |
int batch_sh_fd = -1; |
| 53 |
int batch_stream_flags; |
| 54 |
|
| 55 |
static int tweaked_append; |
| 56 |
static int tweaked_append_verify; |
| 57 |
static int tweaked_iconv; |
| 58 |
|
| 59 |
static int *flag_ptr[] = { |
| 60 |
&recurse, |
| 61 |
&preserve_uid, |
| 62 |
&preserve_gid, |
| 63 |
&preserve_links, |
| 64 |
&preserve_devices, |
| 65 |
&preserve_hard_links, |
| 66 |
&always_checksum, |
| 67 |
&xfer_dirs, |
| 68 |
&do_compression, |
| 69 |
&tweaked_iconv, |
| 70 |
&preserve_acls, |
| 71 |
&preserve_xattrs, |
| 72 |
&inplace, |
| 73 |
&tweaked_append, |
| 74 |
&tweaked_append_verify, |
| 75 |
NULL |
| 76 |
}; |
| 77 |
|
| 78 |
static const char *const flag_name[] = { |
| 79 |
"--recurse (-r)", |
| 80 |
"--owner (-o)", |
| 81 |
"--group (-g)", |
| 82 |
"--links (-l)", |
| 83 |
"--devices (-D)", |
| 84 |
"--hard-links (-H)", |
| 85 |
"--checksum (-c)", |
| 86 |
"--dirs (-d)", |
| 87 |
"--compress (-z)", |
| 88 |
"--iconv", |
| 89 |
"--acls (-A)", |
| 90 |
"--xattrs (-X)", |
| 91 |
"--inplace", |
| 92 |
"--append", |
| 93 |
"--append-verify", |
| 94 |
NULL |
| 95 |
}; |
| 96 |
|
| 97 |
void write_stream_flags(int fd) |
| 98 |
{ |
| 99 |
int i, flags; |
| 100 |
|
| 101 |
tweaked_append = append_mode == 1; |
| 102 |
tweaked_append_verify = append_mode == 2; |
| 103 |
#ifdef ICONV_OPTION |
| 104 |
tweaked_iconv = iconv_opt != NULL; |
| 105 |
#endif |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
for (i = 0, flags = 0; flag_ptr[i]; i++) { |
| 110 |
if (*flag_ptr[i]) |
| 111 |
flags |= 1 << i; |
| 112 |
} |
| 113 |
write_int(fd, flags); |
| 114 |
} |
| 115 |
|
| 116 |
void read_stream_flags(int fd) |
| 117 |
{ |
| 118 |
batch_stream_flags = read_int(fd); |
| 119 |
} |
| 120 |
|
| 121 |
void check_batch_flags(void) |
| 122 |
{ |
| 123 |
int i; |
| 124 |
|
| 125 |
if (protocol_version < 29) |
| 126 |
flag_ptr[7] = NULL; |
| 127 |
else if (protocol_version < 30) |
| 128 |
flag_ptr[9] = NULL; |
| 129 |
tweaked_append = append_mode == 1; |
| 130 |
tweaked_append_verify = append_mode == 2; |
| 131 |
#ifdef ICONV_OPTION |
| 132 |
tweaked_iconv = iconv_opt != NULL; |
| 133 |
#endif |
| 134 |
for (i = 0; flag_ptr[i]; i++) { |
| 135 |
int set = batch_stream_flags & (1 << i) ? 1 : 0; |
| 136 |
if (*flag_ptr[i] != set) { |
| 137 |
if (i == 9) { |
| 138 |
rprintf(FERROR, |
| 139 |
"%s specify the --iconv option to use this batch file.\n", |
| 140 |
set ? "Please" : "Do not"); |
| 141 |
exit_cleanup(RERR_SYNTAX); |
| 142 |
} |
| 143 |
if (INFO_GTE(MISC, 1)) { |
| 144 |
rprintf(FINFO, |
| 145 |
"%sing the %s option to match the batchfile.\n", |
| 146 |
set ? "Sett" : "Clear", flag_name[i]); |
| 147 |
} |
| 148 |
*flag_ptr[i] = set; |
| 149 |
} |
| 150 |
} |
| 151 |
if (protocol_version < 29) { |
| 152 |
if (recurse) |
| 153 |
xfer_dirs |= 1; |
| 154 |
else if (xfer_dirs < 2) |
| 155 |
xfer_dirs = 0; |
| 156 |
} |
| 157 |
|
| 158 |
if (tweaked_append) |
| 159 |
append_mode = 1; |
| 160 |
else if (tweaked_append_verify) |
| 161 |
append_mode = 2; |
| 162 |
} |
| 163 |
|
| 164 |
static int write_arg(const char *arg) |
| 165 |
{ |
| 166 |
const char *x, *s; |
| 167 |
int len, err = 0; |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
if (*arg == '-' && (x = strchr(arg, '=')) != NULL) { |
| 173 |
const char *p = arg; |
| 174 |
while (p < x && (*p == '-' || *p == '_' |
| 175 |
|| (*p >= '0' && *p <= '9') |
| 176 |
|| (*p >= 'A' && *p <= 'Z') |
| 177 |
|| (*p >= 'a' && *p <= 'z'))) |
| 178 |
p++; |
| 179 |
if (p == x) { |
| 180 |
err |= write(batch_sh_fd, arg, x - arg + 1) != x - arg + 1; |
| 181 |
arg += x - arg + 1; |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
err |= write(batch_sh_fd, "'", 1) != 1; |
| 189 |
for (s = arg; (x = strchr(s, '\'')) != NULL; s = x + 1) { |
| 190 |
err |= write(batch_sh_fd, s, x - s) != x - s; |
| 191 |
err |= write(batch_sh_fd, "'\\''", 4) != 4; |
| 192 |
} |
| 193 |
len = strlen(s); |
| 194 |
err |= write(batch_sh_fd, s, len) != len; |
| 195 |
err |= write(batch_sh_fd, "'", 1) != 1; |
| 196 |
|
| 197 |
return err; |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
static int write_opt(const char *opt, const char *arg) |
| 202 |
{ |
| 203 |
int len = strlen(opt); |
| 204 |
int err = write(batch_sh_fd, " ", 1) != 1; |
| 205 |
err |= write(batch_sh_fd, opt, len) != len; |
| 206 |
if (arg) { |
| 207 |
err |= write(batch_sh_fd, "=", 1) != 1; |
| 208 |
err |= write_arg(arg); |
| 209 |
} |
| 210 |
return err; |
| 211 |
} |
| 212 |
|
| 213 |
static void write_filter_rules(int fd) |
| 214 |
{ |
| 215 |
filter_rule *ent; |
| 216 |
|
| 217 |
write_sbuf(fd, " <<'#E#'\n"); |
| 218 |
for (ent = filter_list.head; ent; ent = ent->next) { |
| 219 |
unsigned int plen; |
| 220 |
char *p = get_rule_prefix(ent, "- ", 0, &plen); |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
if (ent->pattern && strchr(ent->pattern, '\n')) { |
| 228 |
rprintf(FERROR, "cannot write a filter rule containing a newline to the batch replay script\n"); |
| 229 |
exit_cleanup(RERR_SYNTAX); |
| 230 |
} |
| 231 |
write_buf(fd, p, plen); |
| 232 |
write_sbuf(fd, ent->pattern); |
| 233 |
if (ent->rflags & FILTRULE_DIRECTORY) |
| 234 |
write_byte(fd, '/'); |
| 235 |
write_byte(fd, eol_nulls ? 0 : '\n'); |
| 236 |
} |
| 237 |
if (eol_nulls) |
| 238 |
write_sbuf(fd, ";\n"); |
| 239 |
write_sbuf(fd, "#E#"); |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
void open_batch_files(void) |
| 244 |
{ |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
if (write_batch) { |
| 250 |
char filename[MAXPATHLEN]; |
| 251 |
|
| 252 |
stringjoin(filename, sizeof filename, batch_name, ".sh", NULL); |
| 253 |
|
| 254 |
batch_sh_fd = open_no_attacker_symlinks(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR | S_IWUSR | S_IXUSR); |
| 255 |
if (batch_sh_fd < 0) { |
| 256 |
rsyserr(FERROR, errno, "Batch file %s open error", full_fname(filename)); |
| 257 |
exit_cleanup(RERR_FILESELECT); |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
batch_fd = open_no_attacker_symlinks(batch_name, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR | S_IWUSR); |
| 264 |
} else if (strcmp(batch_name, "-") == 0) |
| 265 |
batch_fd = STDIN_FILENO; |
| 266 |
else |
| 267 |
batch_fd = open_no_attacker_symlinks(batch_name, O_RDONLY | O_BINARY, S_IRUSR | S_IWUSR); |
| 268 |
|
| 269 |
if (batch_fd < 0) { |
| 270 |
rsyserr(FERROR, errno, "Batch file %s open error", full_fname(batch_name)); |
| 271 |
exit_cleanup(RERR_FILEIO); |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
if (!write_batch && batch_fd != STDIN_FILENO) { |
| 277 |
STRUCT_STAT st; |
| 278 |
if (do_fstat(batch_fd, &st) == 0 && !S_ISREG(st.st_mode)) { |
| 279 |
rprintf(FERROR, "Batch file %s is not a regular file\n", |
| 280 |
full_fname(batch_name)); |
| 281 |
exit_cleanup(RERR_FILEIO); |
| 282 |
} |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
void write_batch_shell_file(void) |
| 292 |
{ |
| 293 |
int i, j, len, err = 0; |
| 294 |
char *p, *p2; |
| 295 |
|
| 296 |
|
| 297 |
err |= write_arg(raw_argv[0]); |
| 298 |
if (filter_list.head) { |
| 299 |
if (protocol_version >= 29) |
| 300 |
err |= write_opt("--filter", "._-"); |
| 301 |
else |
| 302 |
err |= write_opt("--exclude-from", "-"); |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
for (i = raw_argc-1, j = cooked_argc-1; i > 0 && j >= 0; i--) { |
| 307 |
if (strcmp(raw_argv[i], cooked_argv[j]) == 0) { |
| 308 |
raw_argv[i] = NULL; |
| 309 |
j--; |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
for (i = 1; i < raw_argc; i++) { |
| 314 |
if (!(p = raw_argv[i])) |
| 315 |
continue; |
| 316 |
if (strncmp(p, "--files-from", 12) == 0 |
| 317 |
|| strncmp(p, "--filter", 8) == 0 |
| 318 |
|| strncmp(p, "--include", 9) == 0 |
| 319 |
|| strncmp(p, "--exclude", 9) == 0) { |
| 320 |
if (strchr(p, '=') == NULL) |
| 321 |
i++; |
| 322 |
continue; |
| 323 |
} |
| 324 |
if (strcmp(p, "-f") == 0) { |
| 325 |
i++; |
| 326 |
continue; |
| 327 |
} |
| 328 |
if (strncmp(p, "--write-batch", len = 13) == 0 |
| 329 |
|| strncmp(p, "--only-write-batch", len = 18) == 0) |
| 330 |
err |= write_opt("--read-batch", p[len] == '=' ? p + len + 1 : NULL); |
| 331 |
else { |
| 332 |
err |= write(batch_sh_fd, " ", 1) != 1; |
| 333 |
err |= write_arg(p); |
| 334 |
} |
| 335 |
} |
| 336 |
if (!(p = check_for_hostspec(cooked_argv[cooked_argc - 1], &p2, &i))) |
| 337 |
p = cooked_argv[cooked_argc - 1]; |
| 338 |
err |= write_opt("${1:-", NULL); |
| 339 |
err |= write_arg(p); |
| 340 |
err |= write(batch_sh_fd, "}", 1) != 1; |
| 341 |
if (filter_list.head) |
| 342 |
write_filter_rules(batch_sh_fd); |
| 343 |
if (write(batch_sh_fd, "\n", 1) != 1 || close(batch_sh_fd) < 0 || err) { |
| 344 |
rsyserr(FERROR, errno, "Batch file %s.sh write error", batch_name); |
| 345 |
exit_cleanup(RERR_FILEIO); |
| 346 |
} |
| 347 |
batch_sh_fd = -1; |
| 348 |
} |