Fix daemon-filter crash issue (bug 7489).
[rsync.git] / compat.c
1 /*
2  * Compatibility routines for older rsync protocol versions.
3  *
4  * Copyright (C) Andrew Tridgell 1996
5  * Copyright (C) Paul Mackerras 1996
6  * Copyright (C) 2004-2009 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
24 int remote_protocol = 0;
25 int file_extra_cnt = 0; /* count of file-list extras that everyone gets */
26 int inc_recurse = 0;
27 int use_safe_inc_flist = 0;
28
29 extern int verbose;
30 extern int am_server;
31 extern int am_sender;
32 extern int local_server;
33 extern int inplace;
34 extern int recurse;
35 extern int use_qsort;
36 extern int allow_inc_recurse;
37 extern int append_mode;
38 extern int fuzzy_basis;
39 extern int read_batch;
40 extern int delay_updates;
41 extern int checksum_seed;
42 extern int basis_dir_cnt;
43 extern int prune_empty_dirs;
44 extern int protocol_version;
45 extern int protect_args;
46 extern int preserve_uid;
47 extern int preserve_gid;
48 extern int preserve_acls;
49 extern int preserve_xattrs;
50 extern int need_messages_from_generator;
51 extern int delete_mode, delete_before, delete_during, delete_after;
52 extern char *shell_cmd;
53 extern char *partial_dir;
54 extern char *dest_option;
55 extern char *files_from;
56 extern char *filesfrom_host;
57 extern struct filter_list_struct filter_list;
58 extern int need_unsorted_flist;
59 #ifdef ICONV_OPTION
60 extern iconv_t ic_send, ic_recv;
61 extern char *iconv_opt;
62 #endif
63
64 /* These index values are for the file-list's extra-attribute array. */
65 int uid_ndx, gid_ndx, acls_ndx, xattrs_ndx, unsort_ndx;
66
67 int receiver_symlink_times = 0; /* receiver can set the time on a symlink */
68 int sender_symlink_iconv = 0;   /* sender should convert symlink content */
69
70 #ifdef ICONV_OPTION
71 int filesfrom_convert = 0;
72 #endif
73
74 #define CF_INC_RECURSE   (1<<0)
75 #define CF_SYMLINK_TIMES (1<<1)
76 #define CF_SYMLINK_ICONV (1<<2)
77 #define CF_SAFE_FLIST    (1<<3)
78
79 static const char *client_info;
80
81 /* The server makes sure that if either side only supports a pre-release
82  * version of a protocol, that both sides must speak a compatible version
83  * of that protocol for it to be advertised as available. */
84 static void check_sub_protocol(void)
85 {
86         char *dot;
87         int their_protocol, their_sub;
88 #if SUBPROTOCOL_VERSION != 0
89         int our_sub = protocol_version < PROTOCOL_VERSION ? 0 : SUBPROTOCOL_VERSION;
90 #else
91         int our_sub = 0;
92 #endif
93
94         /* client_info starts with VER.SUB string if client is a pre-release. */
95         if (!(their_protocol = atoi(client_info))
96          || !(dot = strchr(client_info, '.'))
97          || !(their_sub = atoi(dot+1))) {
98 #if SUBPROTOCOL_VERSION != 0
99                 if (our_sub)
100                         protocol_version--;
101 #endif
102                 return;
103         }
104
105         if (their_protocol < protocol_version) {
106                 if (their_sub)
107                         protocol_version = their_protocol - 1;
108                 return;
109         }
110
111         if (their_protocol > protocol_version)
112                 their_sub = 0; /* 0 == final version of older protocol */
113         if (their_sub != our_sub)
114                 protocol_version--;
115 }
116
117 void set_allow_inc_recurse(void)
118 {
119         client_info = shell_cmd ? shell_cmd : "";
120
121         if (!recurse || use_qsort)
122                 allow_inc_recurse = 0;
123         else if (!am_sender
124          && (delete_before || delete_after
125           || delay_updates || prune_empty_dirs))
126                 allow_inc_recurse = 0;
127         else if (am_server && !local_server
128          && (strchr(client_info, 'i') == NULL))
129                 allow_inc_recurse = 0;
130 }
131
132 void setup_protocol(int f_out,int f_in)
133 {
134         if (am_sender)
135                 file_extra_cnt += PTR_EXTRA_CNT;
136         else
137                 file_extra_cnt++;
138         if (preserve_uid)
139                 uid_ndx = ++file_extra_cnt;
140         if (preserve_gid)
141                 gid_ndx = ++file_extra_cnt;
142         if (preserve_acls && !am_sender)
143                 acls_ndx = ++file_extra_cnt;
144         if (preserve_xattrs)
145                 xattrs_ndx = ++file_extra_cnt;
146
147         if (am_server)
148                 set_allow_inc_recurse();
149
150         if (remote_protocol == 0) {
151                 if (am_server && !local_server)
152                         check_sub_protocol();
153                 if (!read_batch)
154                         write_int(f_out, protocol_version);
155                 remote_protocol = read_int(f_in);
156                 if (protocol_version > remote_protocol)
157                         protocol_version = remote_protocol;
158         }
159         if (read_batch && remote_protocol > protocol_version) {
160                 rprintf(FERROR, "The protocol version in the batch file is too new (%d > %d).\n",
161                         remote_protocol, protocol_version);
162                 exit_cleanup(RERR_PROTOCOL);
163         }
164
165         if (verbose > 3) {
166                 rprintf(FINFO, "(%s) Protocol versions: remote=%d, negotiated=%d\n",
167                         am_server? "Server" : "Client", remote_protocol, protocol_version);
168         }
169         if (remote_protocol < MIN_PROTOCOL_VERSION
170          || remote_protocol > MAX_PROTOCOL_VERSION) {
171                 rprintf(FERROR,"protocol version mismatch -- is your shell clean?\n");
172                 rprintf(FERROR,"(see the rsync man page for an explanation)\n");
173                 exit_cleanup(RERR_PROTOCOL);
174         }
175         if (remote_protocol < OLD_PROTOCOL_VERSION) {
176                 rprintf(FINFO,"%s is very old version of rsync, upgrade recommended.\n",
177                         am_server? "Client" : "Server");
178         }
179         if (protocol_version < MIN_PROTOCOL_VERSION) {
180                 rprintf(FERROR, "--protocol must be at least %d on the %s.\n",
181                         MIN_PROTOCOL_VERSION, am_server? "Server" : "Client");
182                 exit_cleanup(RERR_PROTOCOL);
183         }
184         if (protocol_version > PROTOCOL_VERSION) {
185                 rprintf(FERROR, "--protocol must be no more than %d on the %s.\n",
186                         PROTOCOL_VERSION, am_server? "Server" : "Client");
187                 exit_cleanup(RERR_PROTOCOL);
188         }
189         if (read_batch)
190                 check_batch_flags();
191
192         if (protocol_version < 30) {
193                 if (append_mode == 1)
194                         append_mode = 2;
195                 if (preserve_acls && !local_server) {
196                         rprintf(FERROR,
197                             "--acls requires protocol 30 or higher"
198                             " (negotiated %d).\n",
199                             protocol_version);
200                         exit_cleanup(RERR_PROTOCOL);
201                 }
202                 if (preserve_xattrs && !local_server) {
203                         rprintf(FERROR,
204                             "--xattrs requires protocol 30 or higher"
205                             " (negotiated %d).\n",
206                             protocol_version);
207                         exit_cleanup(RERR_PROTOCOL);
208                 }
209         }
210
211         if (delete_mode && !(delete_before+delete_during+delete_after)) {
212                 if (protocol_version < 30)
213                         delete_before = 1;
214                 else
215                         delete_during = 1;
216         }
217
218         if (protocol_version < 29) {
219                 if (fuzzy_basis) {
220                         rprintf(FERROR,
221                             "--fuzzy requires protocol 29 or higher"
222                             " (negotiated %d).\n",
223                             protocol_version);
224                         exit_cleanup(RERR_PROTOCOL);
225                 }
226
227                 if (basis_dir_cnt && inplace) {
228                         rprintf(FERROR,
229                             "%s with --inplace requires protocol 29 or higher"
230                             " (negotiated %d).\n",
231                             dest_option, protocol_version);
232                         exit_cleanup(RERR_PROTOCOL);
233                 }
234
235                 if (basis_dir_cnt > 1) {
236                         rprintf(FERROR,
237                             "Using more than one %s option requires protocol"
238                             " 29 or higher (negotiated %d).\n",
239                             dest_option, protocol_version);
240                         exit_cleanup(RERR_PROTOCOL);
241                 }
242
243                 if (prune_empty_dirs) {
244                         rprintf(FERROR,
245                             "--prune-empty-dirs requires protocol 29 or higher"
246                             " (negotiated %d).\n",
247                             protocol_version);
248                         exit_cleanup(RERR_PROTOCOL);
249                 }
250         } else if (protocol_version >= 30) {
251                 int compat_flags;
252                 if (am_server) {
253                         compat_flags = allow_inc_recurse ? CF_INC_RECURSE : 0;
254 #if defined HAVE_LUTIMES && defined HAVE_UTIMES
255                         compat_flags |= CF_SYMLINK_TIMES;
256 #endif
257 #ifdef ICONV_OPTION
258                         compat_flags |= CF_SYMLINK_ICONV;
259 #endif
260                         if (local_server || strchr(client_info, 'f') != NULL)
261                                 compat_flags |= CF_SAFE_FLIST;
262                         write_byte(f_out, compat_flags);
263                 } else
264                         compat_flags = read_byte(f_in);
265                 /* The inc_recurse var MUST be set to 0 or 1. */
266                 inc_recurse = compat_flags & CF_INC_RECURSE ? 1 : 0;
267                 if (am_sender) {
268                         receiver_symlink_times = am_server
269                             ? strchr(client_info, 'L') != NULL
270                             : !!(compat_flags & CF_SYMLINK_TIMES);
271                 }
272 #if defined HAVE_LUTIMES && defined HAVE_UTIMES
273                 else
274                         receiver_symlink_times = 1;
275 #endif
276 #ifdef ICONV_OPTION
277                 sender_symlink_iconv = iconv_opt && (am_server
278                     ? local_server || strchr(client_info, 's') != NULL
279                     : !!(compat_flags & CF_SYMLINK_ICONV));
280 #endif
281                 if (inc_recurse && !allow_inc_recurse) {
282                         /* This should only be able to happen in a batch. */
283                         fprintf(stderr,
284                             "Incompatible options specified for inc-recursive %s.\n",
285                             read_batch ? "batch file" : "connection");
286                         exit_cleanup(RERR_SYNTAX);
287                 }
288                 use_safe_inc_flist = !!(compat_flags & CF_SAFE_FLIST);
289                 need_messages_from_generator = 1;
290 #if defined HAVE_LUTIMES && defined HAVE_UTIMES
291         } else if (!am_sender) {
292                 receiver_symlink_times = 1;
293 #endif
294         }
295
296         if (need_unsorted_flist && (!am_sender || inc_recurse))
297                 unsort_ndx = ++file_extra_cnt;
298
299         if (partial_dir && *partial_dir != '/' && (!am_server || local_server)) {
300                 int flags = MATCHFLG_NO_PREFIXES | MATCHFLG_DIRECTORY;
301                 if (!am_sender || protocol_version >= 30)
302                         flags |= MATCHFLG_PERISHABLE;
303                 parse_rule(&filter_list, partial_dir, flags, 0);
304         }
305
306
307 #ifdef ICONV_OPTION
308         if (protect_args && files_from) {
309                 if (am_sender)
310                         filesfrom_convert = filesfrom_host && ic_send != (iconv_t)-1;
311                 else
312                         filesfrom_convert = !filesfrom_host && ic_recv != (iconv_t)-1;
313         }
314 #endif
315
316         if (am_server) {
317                 if (!checksum_seed)
318                         checksum_seed = time(NULL);
319                 write_int(f_out, checksum_seed);
320         } else {
321                 checksum_seed = read_int(f_in);
322         }
323 }