update rsync systemd unit, add more security features
[rsync.git] / batch.c
1 /*
2  * Support for the batch-file options.
3  *
4  * Copyright (C) 1999 Weiss
5  * Copyright (C) 2004 Chris Shoemaker
6  * Copyright (C) 2004-2020 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 xfersum_type;
42 extern int protocol_version;
43 extern int raw_argc, cooked_argc;
44 extern char **raw_argv, **cooked_argv;
45 extern char *batch_name;
46 extern const char *checksum_choice;
47 extern const char *compress_choice;
48 #ifdef ICONV_OPTION
49 extern char *iconv_opt;
50 #endif
51
52 extern filter_rule_list filter_list;
53
54 int batch_fd = -1;
55 int batch_sh_fd = -1;
56 int batch_stream_flags;
57
58 static int tweaked_append;
59 static int tweaked_append_verify;
60 static int tweaked_iconv;
61
62 static int *flag_ptr[] = {
63         &recurse,               /* 0 */
64         &preserve_uid,          /* 1 */
65         &preserve_gid,          /* 2 */
66         &preserve_links,        /* 3 */
67         &preserve_devices,      /* 4 */
68         &preserve_hard_links,   /* 5 */
69         &always_checksum,       /* 6 */
70         &xfer_dirs,             /* 7 (protocol 29) */
71         &do_compression,        /* 8 (protocol 29) */
72         &tweaked_iconv,         /* 9  (protocol 30) */
73         &preserve_acls,         /* 10 (protocol 30) */
74         &preserve_xattrs,       /* 11 (protocol 30) */
75         &inplace,               /* 12 (protocol 30) */
76         &tweaked_append,        /* 13 (protocol 30) */
77         &tweaked_append_verify, /* 14 (protocol 30) */
78         NULL
79 };
80
81 static char *flag_name[] = {
82         "--recurse (-r)",
83         "--owner (-o)",
84         "--group (-g)",
85         "--links (-l)",
86         "--devices (-D)",
87         "--hard-links (-H)",
88         "--checksum (-c)",
89         "--dirs (-d)",
90         "--compress (-z)",
91         "--iconv",
92         "--acls (-A)",
93         "--xattrs (-X)",
94         "--inplace",
95         "--append",
96         "--append-verify",
97         NULL
98 };
99
100 void write_stream_flags(int fd)
101 {
102         int i, flags;
103
104         tweaked_append = append_mode == 1;
105         tweaked_append_verify = append_mode == 2;
106 #ifdef ICONV_OPTION
107         tweaked_iconv = iconv_opt != NULL;
108 #endif
109
110         /* Start the batch file with a bitmap of data-stream-affecting
111          * flags. */
112         for (i = 0, flags = 0; flag_ptr[i]; i++) {
113                 if (*flag_ptr[i])
114                         flags |= 1 << i;
115         }
116         write_int(fd, flags);
117 }
118
119 void read_stream_flags(int fd)
120 {
121         batch_stream_flags = read_int(fd);
122 }
123
124 void check_batch_flags(void)
125 {
126         int i;
127
128         if (protocol_version < 29)
129                 flag_ptr[7] = NULL;
130         else if (protocol_version < 30)
131                 flag_ptr[9] = NULL;
132         tweaked_append = append_mode == 1;
133         tweaked_append_verify = append_mode == 2;
134 #ifdef ICONV_OPTION
135         tweaked_iconv = iconv_opt != NULL;
136 #endif
137         for (i = 0; flag_ptr[i]; i++) {
138                 int set = batch_stream_flags & (1 << i) ? 1 : 0;
139                 if (*flag_ptr[i] != set) {
140                         if (i == 9) {
141                                 rprintf(FERROR,
142                                         "%s specify the --iconv option to use this batch file.\n",
143                                         set ? "Please" : "Do not");
144                                 exit_cleanup(RERR_SYNTAX);
145                         }
146                         if (INFO_GTE(MISC, 1)) {
147                                 rprintf(FINFO,
148                                         "%sing the %s option to match the batchfile.\n",
149                                         set ? "Sett" : "Clear", flag_name[i]);
150                         }
151                         *flag_ptr[i] = set;
152                 }
153         }
154         if (protocol_version < 29) {
155                 if (recurse)
156                         xfer_dirs |= 1;
157                 else if (xfer_dirs < 2)
158                         xfer_dirs = 0;
159         }
160
161         if (tweaked_append)
162                 append_mode = 1;
163         else if (tweaked_append_verify)
164                 append_mode = 2;
165 }
166
167 static int write_arg(const char *arg)
168 {
169         const char *x, *s;
170         int len, err = 0;
171
172         if (*arg == '-' && (x = strchr(arg, '=')) != NULL) {
173                 err |= write(batch_sh_fd, arg, x - arg + 1) != x - arg + 1;
174                 arg += x - arg + 1;
175         }
176
177         if (strpbrk(arg, " \"'&;|[]()$#!*?^\\") != NULL) {
178                 err |= write(batch_sh_fd, "'", 1) != 1;
179                 for (s = arg; (x = strchr(s, '\'')) != NULL; s = x + 1) {
180                         err |= write(batch_sh_fd, s, x - s + 1) != x - s + 1;
181                         err |= write(batch_sh_fd, "'", 1) != 1;
182                 }
183                 len = strlen(s);
184                 err |= write(batch_sh_fd, s, len) != len;
185                 err |= write(batch_sh_fd, "'", 1) != 1;
186                 return err;
187         }
188
189         len = strlen(arg);
190         err |= write(batch_sh_fd, arg, len) != len;
191
192         return err;
193 }
194
195 /* Writes out a space and then an option (or other string) with an optional "=" + arg suffix. */
196 static int write_opt(const char *opt, const char *arg)
197 {
198         int len = strlen(opt);
199         int err = write(batch_sh_fd, " ", 1) != 1;
200         err = write(batch_sh_fd, opt, len) != len ? 1 : 0; 
201         if (arg) {
202                 err |= write(batch_sh_fd, "=", 1) != 1;
203                 err |= write_arg(arg);
204         }
205         return err;
206 }
207
208 static void write_filter_rules(int fd)
209 {
210         filter_rule *ent;
211
212         write_sbuf(fd, " <<'#E#'\n");
213         for (ent = filter_list.head; ent; ent = ent->next) {
214                 unsigned int plen;
215                 char *p = get_rule_prefix(ent, "- ", 0, &plen);
216                 write_buf(fd, p, plen);
217                 write_sbuf(fd, ent->pattern);
218                 if (ent->rflags & FILTRULE_DIRECTORY)
219                         write_byte(fd, '/');
220                 write_byte(fd, eol_nulls ? 0 : '\n');
221         }
222         if (eol_nulls)
223                 write_sbuf(fd, ";\n");
224         write_sbuf(fd, "#E#");
225 }
226
227 /* This sets batch_fd and (for --write-batch) batch_sh_fd. */
228 void open_batch_files(void)
229 {
230         if (write_batch) {
231                 char filename[MAXPATHLEN];
232
233                 stringjoin(filename, sizeof filename, batch_name, ".sh", NULL);
234
235                 batch_sh_fd = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IXUSR);
236                 if (batch_sh_fd < 0) {
237                         rsyserr(FERROR, errno, "Batch file %s open error", full_fname(filename));
238                         exit_cleanup(RERR_FILESELECT);
239                 }
240
241                 batch_fd = do_open(batch_name, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
242         } else if (strcmp(batch_name, "-") == 0)
243                 batch_fd = STDIN_FILENO;
244         else
245                 batch_fd = do_open(batch_name, O_RDONLY, S_IRUSR | S_IWUSR);
246
247         if (batch_fd < 0) {
248                 rsyserr(FERROR, errno, "Batch file %s open error", full_fname(batch_name));
249                 exit_cleanup(RERR_FILEIO);
250         }
251 }
252
253 /* This routine tries to write out an equivalent --read-batch command
254  * given the user's --write-batch args.  However, it doesn't really
255  * understand most of the options, so it uses some overly simple
256  * heuristics to munge the command line into something that will
257  * (hopefully) work. */
258 void write_batch_shell_file(void)
259 {
260         int i, j, len, err = 0;
261         char *p, *p2;
262
263         /* Write argvs info to BATCH.sh file */
264         err |= write_arg(raw_argv[0]);
265         if (filter_list.head) {
266                 if (protocol_version >= 29)
267                         err |= write_opt("--filter", "._-");
268                 else
269                         err |= write_opt("--exclude-from", "-");
270         }
271
272         /* We need to make sure that any protocol-based or negotiated choices get accurately
273          * reflected in the options we save AND that we avoid any need for --read-batch to
274          * do a string-based negotiation (since we don't write them into the file). */
275         if (do_compression)
276                 err |= write_opt("--compress-choice", compress_choice);
277         if (strchr(checksum_choice, ',') || xfersum_type != parse_csum_name(NULL, -1))
278                 err |= write_opt("--checksum-choice", checksum_choice);
279
280         /* Elide the filename args from the option list, but scan for them in reverse. */
281         for (i = raw_argc-1, j = cooked_argc-1; i > 0 && j >= 0; i--) {
282                 if (strcmp(raw_argv[i], cooked_argv[j]) == 0) {
283                         raw_argv[i] = NULL;
284                         j--;
285                 }
286         }
287
288         for (i = 1; i < raw_argc; i++) {
289                 if (!(p = raw_argv[i]))
290                         continue;
291                 if (strncmp(p, "--files-from", 12) == 0
292                  || strncmp(p, "--filter", 8) == 0
293                  || strncmp(p, "--include", 9) == 0
294                  || strncmp(p, "--exclude", 9) == 0) {
295                         if (strchr(p, '=') == NULL)
296                                 i++;
297                         continue;
298                 }
299                 if (strcmp(p, "-f") == 0) {
300                         i++;
301                         continue;
302                 }
303                 if (strncmp(p, "--write-batch", len = 13) == 0
304                  || strncmp(p, "--only-write-batch", len = 18) == 0)
305                         err |= write_opt("--read-batch", p[len] == '=' ? p + len + 1 : NULL);
306                 else {
307                         err |= write(batch_sh_fd, " ", 1) != 1;
308                         err |= write_arg(p);
309                 }
310         }
311         if (!(p = check_for_hostspec(cooked_argv[cooked_argc - 1], &p2, &i)))
312                 p = cooked_argv[cooked_argc - 1];
313         err |= write_opt("${1:-", NULL);
314         err |= write_arg(p);
315         err |= write(batch_sh_fd, "}", 1) != 1;
316         if (filter_list.head)
317                 write_filter_rules(batch_sh_fd);
318         if (write(batch_sh_fd, "\n", 1) != 1 || close(batch_sh_fd) < 0 || err) {
319                 rsyserr(FERROR, errno, "Batch file %s.sh write error", batch_name);
320                 exit_cleanup(RERR_FILEIO);
321         }
322         batch_sh_fd = -1;
323 }