Debian Codegraph @

batch.c

raw symbols SCIP
1 /*
2 * Support for the batch-file options.
3 *
4 * Copyright (C) 1999 Weiss
5 * Copyright (C) 2004 Chris Shoemaker
6 * Copyright (C) 2004-2022 Wayne Davison
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, visit the http://fsf.org website.
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, /* 0 */
61 &preserve_uid, /* 1 */
62 &preserve_gid, /* 2 */
63 &preserve_links, /* 3 */
64 &preserve_devices, /* 4 */
65 &preserve_hard_links, /* 5 */
66 &always_checksum, /* 6 */
67 &xfer_dirs, /* 7 (protocol 29) */
68 &do_compression, /* 8 (protocol 29) */
69 &tweaked_iconv, /* 9 (protocol 30) */
70 &preserve_acls, /* 10 (protocol 30) */
71 &preserve_xattrs, /* 11 (protocol 30) */
72 &inplace, /* 12 (protocol 30) */
73 &tweaked_append, /* 13 (protocol 30) */
74 &tweaked_append_verify, /* 14 (protocol 30) */
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 /* Start the batch file with a bitmap of data-stream-affecting
108 * flags. */
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 /* Emit a "--opt=" prefix unquoted only when it is a plain option token;
170 * a metacharacter before '=' (an attacker-shaped arg) must be quoted
171 * along with the rest, or it would run raw in the replay script. */
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 /* Single-quote unconditionally so every shell metacharacter (backtick,
186 * newline, redirection, ...) stays literal in the replay script. An
187 * embedded ' is emitted as the '\'' close/escape/reopen sequence. */
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 /* Writes out a space and then an option (or other string) with an optional "=" + arg suffix. */
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 /* A filter pattern is one here-doc line; an embedded newline would let
222 * a crafted pattern (e.g. from a dir-merge/--exclude-from file in an
223 * untrusted tree) forge the "#E#" terminator on its own line and inject
224 * shell commands into the generated replay script. Such a pattern also
225 * can't round-trip the line-delimited here-doc, so refuse it fail-closed
226 * rather than emit an injectable script. */
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 /* This sets batch_fd and (for --write-batch) batch_sh_fd. */
243 void open_batch_files(void)
244 {
245 /* --write-batch/--read-batch are operator-supplied; a planted symlink
246 * could truncate+overwrite an arbitrary file (write side) or stream
247 * attacker bytes into the protocol parser (read side). Refuse symlinks
248 * not owned by uid 0 or our euid anywhere in the path. */
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 /* O_BINARY: the batch stream is binary protocol data; without it
261 * Cygwin et al apply CRLF translation and corrupt it. Unlike
262 * do_open(), open_no_attacker_symlinks passes flags verbatim. */
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 /* --read-batch: the file's bytes drive the protocol parser, so refuse
275 * non-regular files (FIFO, device, socket) at the batch path. */
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 /* This routine tries to write out an equivalent --read-batch command
287 * given the user's --write-batch args. However, it doesn't really
288 * understand most of the options, so it uses some overly simple
289 * heuristics to munge the command line into something that will
290 * (hopefully) work. */
291 void write_batch_shell_file(void)
292 {
293 int i, j, len, err = 0;
294 char *p, *p2;
295
296 /* Write argvs info to BATCH.sh file */
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 /* Elide the filename args from the option list, but scan for them in reverse. */
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 }