Make sure secrets file is not other-accessible, and owned by root if the
[rsync/rsync.git] / token.c
CommitLineData
70d794dc
AT
1/*
2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18*/
19
20#include "rsync.h"
5914bf15 21#include "zlib/zlib.h"
70d794dc
AT
22
23extern int do_compression;
83fff1aa 24static int compression_level = Z_DEFAULT_COMPRESSION;
70d794dc 25
83fff1aa
AT
26/* determine the compression level based on a wildcard filename list */
27void set_compression(char *fname)
28{
29 extern int module_id;
30 char *dont;
31 char *tok;
32
33 if (!do_compression) return;
34
35 compression_level = Z_DEFAULT_COMPRESSION;
36 dont = lp_dont_compress(module_id);
37
38 if (!dont || !*dont) return;
39
40 dont = strdup(dont);
41 fname = strdup(fname);
42 if (!dont || !fname) return;
43
44 strlower(dont);
45 strlower(fname);
46
47 for (tok=strtok(dont," ");tok;tok=strtok(NULL," ")) {
48 if (fnmatch(tok, fname, 0) == 0) {
49 compression_level = 0;
50 break;
51 }
52 }
53 free(dont);
54 free(fname);
55}
70d794dc
AT
56
57/* non-compressing recv token */
58static int simple_recv_token(int f,char **data)
59{
c5eb3650
AT
60 static int residue;
61 static char *buf;
62 int n;
70d794dc 63
c5eb3650
AT
64 if (!buf) {
65 buf = (char *)malloc(CHUNK_SIZE);
66 if (!buf) out_of_memory("simple_recv_token");
67 }
70d794dc 68
c5eb3650
AT
69 if (residue == 0) {
70 int i = read_int(f);
71 if (i <= 0) return i;
72 residue = i;
73 }
70d794dc 74
c5eb3650
AT
75 *data = buf;
76 n = MIN(CHUNK_SIZE,residue);
77 residue -= n;
78 read_buf(f,buf,n);
79 return n;
70d794dc
AT
80}
81
82
83/* non-compressing send token */
c6e7fcb4 84static void simple_send_token(int f,int token,
45f133b9 85 struct map_struct *buf,OFF_T offset,int n)
70d794dc 86{
c5eb3650
AT
87 if (n > 0) {
88 int l = 0;
89 while (l < n) {
90 int n1 = MIN(CHUNK_SIZE,n-l);
91 write_int(f,n1);
92 write_buf(f,map_ptr(buf,offset+l,n1),n1);
93 l += n1;
94 }
95 }
45f133b9
AT
96 /* a -2 token means to send data only and no token */
97 if (token != -2) {
98 write_int(f,-(token+1));
99 }
70d794dc
AT
100}
101
102
861c20b4
PM
103/* Flag bytes in compressed stream are encoded as follows: */
104#define END_FLAG 0 /* that's all folks */
105#define TOKEN_LONG 0x20 /* followed by 32-bit token number */
106#define TOKENRUN_LONG 0x21 /* ditto with 16-bit run count */
107#define DEFLATED_DATA 0x40 /* + 6-bit high len, then low len byte */
108#define TOKEN_REL 0x80 /* + 6-bit relative token number */
109#define TOKENRUN_REL 0xc0 /* ditto with 16-bit run count */
110
111#define MAX_DATA_COUNT 16383 /* fit 14 bit count into 2 bytes with flags */
112
113/* For coding runs of tokens */
114static int last_token = -1;
115static int run_start;
116static int last_run_end;
117
118/* Deflation state */
119static z_stream tx_strm;
120
121/* Output buffer */
3a6a366f 122static char *obuf;
861c20b4
PM
123
124/* Send a deflated token */
125static void
126send_deflated_token(int f, int token,
45f133b9 127 struct map_struct *buf, OFF_T offset, int nb, int toklen)
861c20b4 128{
5914bf15
PM
129 int n, r;
130 static int init_done, flush_pending;
131
132 if (last_token == -1) {
133 /* initialization */
134 if (!init_done) {
135 tx_strm.next_in = NULL;
136 tx_strm.zalloc = NULL;
137 tx_strm.zfree = NULL;
83fff1aa 138 if (deflateInit2(&tx_strm, compression_level,
5914bf15
PM
139 Z_DEFLATED, -15, 8,
140 Z_DEFAULT_STRATEGY) != Z_OK) {
141 rprintf(FERROR, "compression init failed\n");
65417579 142 exit_cleanup(RERR_STREAMIO);
5914bf15
PM
143 }
144 if ((obuf = malloc(MAX_DATA_COUNT+2)) == NULL)
145 out_of_memory("send_deflated_token");
146 init_done = 1;
147 } else
148 deflateReset(&tx_strm);
149 last_run_end = 0;
150 run_start = token;
151 flush_pending = 0;
152
153 } else if (last_token == -2) {
154 run_start = token;
155
156 } else if (nb != 0 || token != last_token + 1
157 || token >= run_start + 65536) {
158 /* output previous run */
159 r = run_start - last_run_end;
160 n = last_token - run_start;
161 if (r >= 0 && r <= 63) {
162 write_byte(f, (n==0? TOKEN_REL: TOKENRUN_REL) + r);
163 } else {
164 write_byte(f, (n==0? TOKEN_LONG: TOKENRUN_LONG));
165 write_int(f, run_start);
166 }
167 if (n != 0) {
168 write_byte(f, n);
169 write_byte(f, n >> 8);
170 }
171 last_run_end = last_token;
172 run_start = token;
861c20b4 173 }
5914bf15
PM
174
175 last_token = token;
176
177 if (nb != 0 || flush_pending) {
178 /* deflate the data starting at offset */
179 int flush = Z_NO_FLUSH;
180 tx_strm.avail_in = 0;
181 tx_strm.avail_out = 0;
182 do {
183 if (tx_strm.avail_in == 0 && nb != 0) {
184 /* give it some more input */
185 n = MIN(nb, CHUNK_SIZE);
186 tx_strm.next_in = (Bytef *)
187 map_ptr(buf, offset, n);
188 tx_strm.avail_in = n;
189 nb -= n;
190 offset += n;
191 }
192 if (tx_strm.avail_out == 0) {
193 tx_strm.next_out = (Bytef *)(obuf + 2);
194 tx_strm.avail_out = MAX_DATA_COUNT;
195 if (flush != Z_NO_FLUSH) {
196 /*
197 * We left the last 4 bytes in the
198 * buffer, in case they are the
199 * last 4. Move them to the front.
200 */
201 memcpy(tx_strm.next_out,
202 obuf+MAX_DATA_COUNT-2, 4);
203 tx_strm.next_out += 4;
204 tx_strm.avail_out -= 4;
205 }
206 }
207 if (nb == 0 && token != -2)
208 flush = Z_SYNC_FLUSH;
209 r = deflate(&tx_strm, flush);
210 if (r != Z_OK) {
211 rprintf(FERROR, "deflate returned %d\n", r);
65417579 212 exit_cleanup(RERR_STREAMIO);
5914bf15
PM
213 }
214 if (nb == 0 || tx_strm.avail_out == 0) {
215 n = MAX_DATA_COUNT - tx_strm.avail_out;
216 if (flush != Z_NO_FLUSH) {
217 /*
218 * We have to trim off the last 4
219 * bytes of output when flushing
220 * (they are just 0, 0, ff, ff).
221 */
222 n -= 4;
223 }
224 if (n > 0) {
225 obuf[0] = DEFLATED_DATA + (n >> 8);
226 obuf[1] = n;
227 write_buf(f, obuf, n+2);
228 }
229 }
230 } while (nb != 0 || tx_strm.avail_out == 0);
231 flush_pending = token == -2;
861c20b4 232 }
861c20b4 233
5914bf15
PM
234 if (token == -1) {
235 /* end of file - clean up */
236 write_byte(f, END_FLAG);
861c20b4 237
5914bf15
PM
238 } else if (token != -2) {
239 /* add the data in the current block to the compressor's
240 history and hash table */
241 tx_strm.next_in = (Bytef *) map_ptr(buf, offset, toklen);
242 tx_strm.avail_in = toklen;
243 tx_strm.next_out = (Bytef *) obuf;
861c20b4 244 tx_strm.avail_out = MAX_DATA_COUNT;
5914bf15
PM
245 r = deflate(&tx_strm, Z_INSERT_ONLY);
246 if (r != Z_OK || tx_strm.avail_in != 0) {
247 rprintf(FERROR, "deflate on token returned %d (%d bytes left)\n",
248 r, tx_strm.avail_in);
65417579 249 exit_cleanup(RERR_STREAMIO);
861c20b4 250 }
861c20b4 251 }
861c20b4
PM
252}
253
254
255/* tells us what the receiver is in the middle of doing */
256static enum { r_init, r_idle, r_running, r_inflating, r_inflated } recv_state;
70d794dc 257
861c20b4
PM
258/* for inflating stuff */
259static z_stream rx_strm;
260static char *cbuf;
261static char *dbuf;
262
263/* for decoding runs of tokens */
264static int rx_token;
265static int rx_run;
266
267/* Receive a deflated token and inflate it */
268static int
269recv_deflated_token(int f, char **data)
270{
5914bf15
PM
271 int n, r, flag;
272 static int init_done;
273 static int saved_flag;
274
275 for (;;) {
276 switch (recv_state) {
277 case r_init:
278 if (!init_done) {
279 rx_strm.next_out = NULL;
280 rx_strm.zalloc = NULL;
281 rx_strm.zfree = NULL;
282 if (inflateInit2(&rx_strm, -15) != Z_OK) {
283 rprintf(FERROR, "inflate init failed\n");
65417579 284 exit_cleanup(RERR_STREAMIO);
5914bf15
PM
285 }
286 if ((cbuf = malloc(MAX_DATA_COUNT)) == NULL
287 || (dbuf = malloc(CHUNK_SIZE)) == NULL)
288 out_of_memory("recv_deflated_token");
289 init_done = 1;
290 } else {
291 inflateReset(&rx_strm);
292 }
b8d4524b 293 recv_state = r_idle;
5914bf15
PM
294 rx_token = 0;
295 break;
296
297 case r_idle:
298 case r_inflated:
299 if (saved_flag) {
300 flag = saved_flag & 0xff;
301 saved_flag = 0;
302 } else
303 flag = read_byte(f);
304 if ((flag & 0xC0) == DEFLATED_DATA) {
305 n = ((flag & 0x3f) << 8) + read_byte(f);
306 read_buf(f, cbuf, n);
307 rx_strm.next_in = (Bytef *)cbuf;
308 rx_strm.avail_in = n;
309 recv_state = r_inflating;
310 break;
311 }
312 if (recv_state == r_inflated) {
313 /* check previous inflated stuff ended correctly */
314 rx_strm.avail_in = 0;
315 rx_strm.next_out = (Bytef *)dbuf;
316 rx_strm.avail_out = CHUNK_SIZE;
317 r = inflate(&rx_strm, Z_SYNC_FLUSH);
318 n = CHUNK_SIZE - rx_strm.avail_out;
319 /*
320 * Z_BUF_ERROR just means no progress was
321 * made, i.e. the decompressor didn't have
322 * any pending output for us.
323 */
324 if (r != Z_OK && r != Z_BUF_ERROR) {
325 rprintf(FERROR, "inflate flush returned %d (%d bytes)\n",
326 r, n);
65417579 327 exit_cleanup(RERR_STREAMIO);
5914bf15
PM
328 }
329 if (n != 0 && r != Z_BUF_ERROR) {
330 /* have to return some more data and
331 save the flag for later. */
332 saved_flag = flag + 0x10000;
333 *data = dbuf;
334 return n;
335 }
336 /*
337 * At this point the decompressor should
338 * be expecting to see the 0, 0, ff, ff bytes.
339 */
340 if (!inflateSyncPoint(&rx_strm)) {
341 rprintf(FERROR, "decompressor lost sync!\n");
65417579 342 exit_cleanup(RERR_STREAMIO);
5914bf15
PM
343 }
344 rx_strm.avail_in = 4;
345 rx_strm.next_in = (Bytef *)cbuf;
346 cbuf[0] = cbuf[1] = 0;
347 cbuf[2] = cbuf[3] = 0xff;
348 inflate(&rx_strm, Z_SYNC_FLUSH);
349 recv_state = r_idle;
350 }
351 if (flag == END_FLAG) {
352 /* that's all folks */
353 recv_state = r_init;
354 return 0;
355 }
356
357 /* here we have a token of some kind */
358 if (flag & TOKEN_REL) {
359 rx_token += flag & 0x3f;
360 flag >>= 6;
361 } else
362 rx_token = read_int(f);
363 if (flag & 1) {
364 rx_run = read_byte(f);
365 rx_run += read_byte(f) << 8;
366 recv_state = r_running;
367 }
368 return -1 - rx_token;
369
370 case r_inflating:
371 rx_strm.next_out = (Bytef *)dbuf;
372 rx_strm.avail_out = CHUNK_SIZE;
373 r = inflate(&rx_strm, Z_NO_FLUSH);
374 n = CHUNK_SIZE - rx_strm.avail_out;
375 if (r != Z_OK) {
376 rprintf(FERROR, "inflate returned %d (%d bytes)\n", r, n);
65417579 377 exit_cleanup(RERR_STREAMIO);
5914bf15
PM
378 }
379 if (rx_strm.avail_in == 0)
380 recv_state = r_inflated;
381 if (n != 0) {
382 *data = dbuf;
383 return n;
384 }
385 break;
386
387 case r_running:
388 ++rx_token;
389 if (--rx_run == 0)
390 recv_state = r_idle;
391 return -1 - rx_token;
b8d4524b 392 }
861c20b4 393 }
861c20b4
PM
394}
395
396/*
397 * put the data corresponding to a token that we've just returned
398 * from recv_deflated_token into the decompressor's history buffer.
399 */
6e4fb64e 400static void see_deflate_token(char *buf, int len)
861c20b4 401{
5914bf15
PM
402 int r, blklen;
403 unsigned char hdr[5];
404
405 rx_strm.avail_in = 0;
406 blklen = 0;
407 hdr[0] = 0;
408 do {
409 if (rx_strm.avail_in == 0 && len != 0) {
410 if (blklen == 0) {
411 /* Give it a fake stored-block header. */
412 rx_strm.next_in = (Bytef *)hdr;
413 rx_strm.avail_in = 5;
414 blklen = len;
415 if (blklen > 0xffff)
416 blklen = 0xffff;
417 hdr[1] = blklen;
418 hdr[2] = blklen >> 8;
419 hdr[3] = ~hdr[1];
420 hdr[4] = ~hdr[2];
421 } else {
422 rx_strm.next_in = (Bytef *)buf;
423 rx_strm.avail_in = blklen;
424 len -= blklen;
425 blklen = 0;
426 }
427 }
428 rx_strm.next_out = (Bytef *)dbuf;
429 rx_strm.avail_out = CHUNK_SIZE;
430 r = inflate(&rx_strm, Z_SYNC_FLUSH);
431 if (r != Z_OK) {
432 rprintf(FERROR, "inflate (token) returned %d\n", r);
65417579 433 exit_cleanup(RERR_STREAMIO);
5914bf15
PM
434 }
435 } while (len || rx_strm.avail_out == 0);
861c20b4 436}
70d794dc
AT
437
438/*
439 * transmit a verbatim buffer of length n followed by a token
440 * If token == -1 then we have reached EOF
441 * If n == 0 then don't send a buffer
70d794dc 442 */
45f133b9 443void send_token(int f,int token,struct map_struct *buf,OFF_T offset,
9e31c482 444 int n,int toklen)
70d794dc 445{
5914bf15
PM
446 if (!do_compression) {
447 simple_send_token(f,token,buf,offset,n);
448 } else {
449 send_deflated_token(f, token, buf, offset, n, toklen);
450 }
70d794dc
AT
451}
452
453
454/*
455 * receive a token or buffer from the other end. If the reurn value is >0 then
456 * it is a data buffer of that length, and *data will point at the data.
457 * if the return value is -i then it represents token i-1
458 * if the return value is 0 then the end has been reached
459 */
460int recv_token(int f,char **data)
461{
5914bf15
PM
462 int tok;
463
464 if (!do_compression) {
465 tok = simple_recv_token(f,data);
466 } else {
467 tok = recv_deflated_token(f, data);
468 }
469 return tok;
861c20b4
PM
470}
471
472/*
473 * look at the data corresponding to a token, if necessary
474 */
475void see_token(char *data, int toklen)
476{
5914bf15
PM
477 if (do_compression)
478 see_deflate_token(data, toklen);
70d794dc 479}