Signedness security patch from Sebastian Krahmer <krahmer@suse.de> --
[rsync/rsync.git] / batch.c
... / ...
CommitLineData
1/* -*- c-file-style: "linux" -*-
2
3 Weiss 1/1999
4 Batch utilities for rsync.
5
6*/
7
8#include "rsync.h"
9#include <time.h>
10
11char rsync_flist_file[27] = "rsync_flist.";
12char rsync_csums_file[27] = "rsync_csums.";
13char rsync_delta_file[27] = "rsync_delta.";
14char rsync_argvs_file[27] = "rsync_argvs.";
15
16char batch_file_ext[15];
17
18int fdb;
19int fdb_delta;
20int fdb_open;
21int fdb_close;
22
23struct file_list *batch_flist;
24
25void create_batch_file_ext()
26{
27 struct tm *timeptr;
28 time_t elapsed_seconds;
29
30 /* Save run date and time to use for batch file extensions */
31 time(&elapsed_seconds);
32 timeptr = localtime(&elapsed_seconds);
33
34 sprintf(batch_file_ext, "%4d%02d%02d%02d%02d%02d",
35 timeptr->tm_year + 1900, timeptr->tm_mon + 1,
36 timeptr->tm_mday, timeptr->tm_hour, timeptr->tm_min,
37 timeptr->tm_sec);
38}
39
40void set_batch_file_ext(char *ext)
41{
42 strcpy(batch_file_ext, ext);
43}
44
45void write_batch_flist_file(char *buff, int bytes_to_write)
46{
47
48 if (fdb_open) {
49 /* Set up file extension */
50 strcat(rsync_flist_file, batch_file_ext);
51
52 /* Open batch flist file for writing; create it if it doesn't exist */
53 fdb =
54 do_open(rsync_flist_file, O_WRONLY | O_CREAT | O_TRUNC,
55 S_IREAD | S_IWRITE);
56 if (fdb == -1) {
57 rprintf(FERROR, "Batch file %s open error: %s\n",
58 rsync_flist_file, strerror(errno));
59 close(fdb);
60 exit_cleanup(1);
61 }
62 fdb_open = 0;
63 }
64
65 /* Write buffer to batch flist file */
66
67 if (write(fdb, buff, bytes_to_write) == -1) {
68 rprintf(FERROR, "Batch file %s write error: %s\n",
69 rsync_flist_file, strerror(errno));
70 close(fdb);
71 exit_cleanup(1);
72 }
73
74
75 if (fdb_close) {
76 close(fdb);
77 }
78}
79
80void write_batch_flist_info(int flist_count, struct file_struct **fptr)
81{
82 int i;
83 int bytes_to_write;
84
85 /* Write flist info to batch file */
86
87 bytes_to_write = sizeof(unsigned) +
88 sizeof(time_t) +
89 sizeof(OFF_T) +
90 sizeof(mode_t) +
91 sizeof(INO64_T) +
92 (2 * sizeof(DEV64_T)) + sizeof(uid_t) + sizeof(gid_t);
93
94 fdb_open = 1;
95 fdb_close = 0;
96
97 for (i = 0; i < flist_count; i++) {
98 write_batch_flist_file((char *) fptr[i], bytes_to_write);
99 write_char_bufs(fptr[i]->basename);
100 write_char_bufs(fptr[i]->dirname);
101 write_char_bufs(fptr[i]->basedir);
102 write_char_bufs(fptr[i]->link);
103 if (i == flist_count - 1) {
104 fdb_close = 1;
105 }
106 write_char_bufs(fptr[i]->sum);
107 }
108
109}
110
111void write_char_bufs(char *buf)
112{
113 /* Write the size of the string which will follow */
114
115 char b[4];
116 if (buf != NULL)
117 SIVAL(b, 0, strlen(buf));
118 else {
119 SIVAL(b, 0, 0);
120 }
121
122 write_batch_flist_file(b, sizeof(int));
123
124 /* Write the string if there is one */
125
126 if (buf != NULL) {
127 write_batch_flist_file(buf, strlen(buf));
128 }
129}
130
131void write_batch_argvs_file(int orig_argc, int argc, char **argv)
132{
133 int fdb;
134 int i;
135 char buff[256];
136
137 strcat(rsync_argvs_file, batch_file_ext);
138
139
140 /* Open batch argvs file for writing; create it if it doesn't exist */
141 fdb = do_open(rsync_argvs_file, O_WRONLY | O_CREAT | O_TRUNC,
142 S_IREAD | S_IWRITE | S_IEXEC);
143 if (fdb == -1) {
144 rprintf(FERROR, "Batch file %s open error: %s\n",
145 rsync_argvs_file, strerror(errno));
146 close(fdb);
147 exit_cleanup(1);
148 }
149 buff[0] = '\0';
150 /* Write argvs info to batch file */
151
152 for (i = argc - orig_argc; i < argc; i++) {
153 /* FIXME: This apparently crashes if rsync is run with
154 * just "rsync -F". I think directly manipulating
155 * argv[] is probably bogus -- what if -F is part of a
156 * run of several short options? */
157 if (!strcmp(argv[i], "-F")) { /* safer to change it here than script */
158 strncat(buff, "-f ", 3); /* chg to -f + ext to get ready for remote */
159 strncat(buff, batch_file_ext,
160 strlen(batch_file_ext));
161 } else {
162 strncat(buff, argv[i], strlen(argv[i]));
163 }
164
165 if (i < (argc - 1)) {
166 strncat(buff, " ", 1);
167 }
168 }
169 if (!write(fdb, buff, strlen(buff))) {
170 rprintf(FERROR, "Batch file %s write error: %s\n",
171 rsync_argvs_file, strerror(errno));
172 close(fdb);
173 exit_cleanup(1);
174 }
175 close(fdb);
176}
177
178struct file_list *create_flist_from_batch()
179{
180 unsigned char flags;
181
182 fdb_open = 1;
183 fdb_close = 0;
184
185 batch_flist = (struct file_list *) malloc(sizeof(batch_flist[0]));
186 if (!batch_flist) {
187 out_of_memory("create_flist_from_batch");
188 }
189 batch_flist->count = 0;
190 batch_flist->malloced = 1000;
191 batch_flist->files =
192 (struct file_struct **) malloc(sizeof(batch_flist->files[0]) *
193 batch_flist->malloced);
194 if (!batch_flist->files) {
195 out_of_memory("create_flist_from_batch"); /* dw -- will exit */
196 }
197
198 for (flags = read_batch_flags(); flags; flags = read_batch_flags()) {
199
200 int i = batch_flist->count;
201
202 if (i >= batch_flist->malloced) {
203 if (batch_flist->malloced < 1000)
204 batch_flist->malloced += 1000;
205 else
206 batch_flist->malloced *= 2;
207 batch_flist->files =
208 (struct file_struct **) realloc(batch_flist->
209 files,
210 sizeof
211 (batch_flist->
212 files[0]) *
213 batch_flist->
214 malloced);
215 if (!batch_flist->files)
216 out_of_memory("create_flist_from_batch");
217 }
218 read_batch_flist_info(&batch_flist->files[i]);
219 batch_flist->files[i]->flags = flags;
220
221 batch_flist->count++;
222 }
223
224 return batch_flist;
225
226}
227
228int read_batch_flist_file(char *buff, int len)
229{
230 int bytes_read;
231
232 if (fdb_open) {
233
234 /* Set up file extension */
235 strcat(rsync_flist_file, batch_file_ext);
236
237 /* Open batch flist file for reading */
238 fdb = do_open(rsync_flist_file, O_RDONLY, 0);
239 if (fdb == -1) {
240 rprintf(FERROR, "Batch file %s open error: %s\n",
241 rsync_flist_file, strerror(errno));
242 close(fdb);
243 exit_cleanup(1);
244 }
245 fdb_open = 0;
246 }
247
248 /* Read flist batch file */
249
250 bytes_read = read(fdb, buff, len);
251
252 if (bytes_read == -1) {
253 rprintf(FERROR, "Batch file %s read error: %s\n",
254 rsync_flist_file, strerror(errno));
255 close(fdb);
256 exit_cleanup(1);
257 }
258 if (bytes_read == 0) { /* EOF */
259 close(fdb);
260 }
261 return bytes_read;
262}
263
264unsigned char read_batch_flags()
265{
266 int flags;
267
268 if (read_batch_flist_file((char *) &flags, 4)) {
269 return 1;
270 } else {
271 return 0;
272 }
273}
274
275void read_batch_flist_info(struct file_struct **fptr)
276{
277 int int_str_len;
278 char char_str_len[4];
279 char buff[256];
280 struct file_struct *file;
281
282 file = (struct file_struct *) malloc(sizeof(*file));
283 if (!file)
284 out_of_memory("read_batch_flist_info");
285 memset((char *) file, 0, sizeof(*file));
286
287 (*fptr) = file;
288
289 read_batch_flist_file((char *) &file->modtime, sizeof(time_t));
290 read_batch_flist_file((char *) &file->length, sizeof(OFF_T));
291 read_batch_flist_file((char *) &file->mode, sizeof(mode_t));
292 read_batch_flist_file((char *) &file->inode, sizeof(INO64_T));
293 read_batch_flist_file((char *) &file->dev, sizeof(DEV64_T));
294 read_batch_flist_file((char *) &file->rdev, sizeof(dev_t));
295 read_batch_flist_file((char *) &file->uid, sizeof(uid_t));
296 read_batch_flist_file((char *) &file->gid, sizeof(gid_t));
297 read_batch_flist_file(char_str_len, sizeof(char_str_len));
298 int_str_len = IVAL(char_str_len, 0);
299 if (int_str_len > 0) {
300 read_batch_flist_file(buff, int_str_len);
301 buff[int_str_len] = '\0';
302 file->basename = strdup(buff);
303 } else {
304 file->basename = NULL;
305 }
306
307 read_batch_flist_file(char_str_len, sizeof(char_str_len));
308 int_str_len = IVAL(char_str_len, 0);
309 if (int_str_len > 0) {
310 read_batch_flist_file(buff, int_str_len);
311 buff[int_str_len] = '\0';
312 file[0].dirname = strdup(buff);
313 } else {
314 file[0].dirname = NULL;
315 }
316
317 read_batch_flist_file(char_str_len, sizeof(char_str_len));
318 int_str_len = IVAL(char_str_len, 0);
319 if (int_str_len > 0) {
320 read_batch_flist_file(buff, int_str_len);
321 buff[int_str_len] = '\0';
322 file[0].basedir = strdup(buff);
323 } else {
324 file[0].basedir = NULL;
325 }
326
327 read_batch_flist_file(char_str_len, sizeof(char_str_len));
328 int_str_len = IVAL(char_str_len, 0);
329 if (int_str_len > 0) {
330 read_batch_flist_file(buff, int_str_len);
331 buff[int_str_len] = '\0';
332 file[0].link = strdup(buff);
333 } else {
334 file[0].link = NULL;
335 }
336
337 read_batch_flist_file(char_str_len, sizeof(char_str_len));
338 int_str_len = IVAL(char_str_len, 0);
339 if (int_str_len > 0) {
340 read_batch_flist_file(buff, int_str_len);
341 buff[int_str_len] = '\0';
342 file[0].sum = strdup(buff);
343 } else {
344 file[0].sum = NULL;
345 }
346}
347
348void write_batch_csums_file(char *buff, int bytes_to_write)
349{
350
351 static int fdb_open = 1;
352
353 if (fdb_open) {
354 /* Set up file extension */
355 strcat(rsync_csums_file, batch_file_ext);
356
357 /* Open batch csums file for writing; create it if it doesn't exist */
358 fdb =
359 do_open(rsync_csums_file, O_WRONLY | O_CREAT | O_TRUNC,
360 S_IREAD | S_IWRITE);
361 if (fdb == -1) {
362 rprintf(FERROR, "Batch file %s open error: %s\n",
363 rsync_csums_file, strerror(errno));
364 close(fdb);
365 exit_cleanup(1);
366 }
367 fdb_open = 0;
368 }
369
370 /* Write buffer to batch csums file */
371
372 if (write(fdb, buff, bytes_to_write) == -1) {
373 rprintf(FERROR, "Batch file %s write error: %s\n",
374 rsync_csums_file, strerror(errno));
375 close(fdb);
376 exit_cleanup(1);
377 }
378}
379
380void close_batch_csums_file()
381{
382 close(fdb);
383
384}
385
386void write_batch_csum_info(int *flist_entry, int flist_count,
387 struct sum_struct *s)
388{
389 int i;
390 int int_zero = 0;
391 extern int csum_length;
392
393 fdb_open = 1;
394
395 /* Write csum info to batch file */
396
397 write_batch_csums_file((char *) flist_entry, sizeof(int));
398 write_batch_csums_file((char *) (s ? &s->count : &int_zero),
399 sizeof(int));
400 if (s) {
401 for (i = 0; i < s->count; i++) {
402 write_batch_csums_file((char *) &s->sums[i].sum1,
403 sizeof(uint32));
404 if ((*flist_entry == flist_count - 1)
405 && (i == s->count - 1)) {
406 fdb_close = 1;
407 }
408 write_batch_csums_file(s->sums[i].sum2,
409 csum_length);
410 }
411 }
412}
413
414int read_batch_csums_file(char *buff, int len)
415{
416 static int fdb_open = 1;
417 int bytes_read;
418
419 if (fdb_open) {
420
421 /* Set up file extension */
422 strcat(rsync_csums_file, batch_file_ext);
423
424 /* Open batch flist file for reading */
425 fdb = do_open(rsync_csums_file, O_RDONLY, 0);
426 if (fdb == -1) {
427 rprintf(FERROR, "Batch file %s open error: %s\n",
428 rsync_csums_file, strerror(errno));
429 close(fdb);
430 exit_cleanup(1);
431 }
432 fdb_open = 0;
433 }
434
435 /* Read csums batch file */
436
437 bytes_read = read(fdb, buff, len);
438
439 if (bytes_read == -1) {
440 rprintf(FERROR, "Batch file %s read error: %s\n",
441 rsync_csums_file, strerror(errno));
442 close(fdb);
443 exit_cleanup(1);
444 }
445 return bytes_read;
446}
447
448
449void read_batch_csum_info(int flist_entry, struct sum_struct *s,
450 int *checksums_match)
451{
452 int i;
453 int file_flist_entry;
454 int file_chunk_ct;
455 uint32 file_sum1;
456 char file_sum2[SUM_LENGTH];
457 extern int csum_length;
458
459
460 read_batch_csums_file((char *) &file_flist_entry, sizeof(int));
461 if (file_flist_entry != flist_entry) {
462 rprintf(FINFO, "file_list_entry NE flist_entry\n");
463 rprintf(FINFO, "file_flist_entry = %d flist_entry = %d\n",
464 file_flist_entry, flist_entry);
465 close(fdb);
466 exit_cleanup(1);
467
468 } else {
469 read_batch_csums_file((char *) &file_chunk_ct,
470 sizeof(int));
471 *checksums_match = 1;
472 for (i = 0; i < file_chunk_ct; i++) {
473
474 read_batch_csums_file((char *) &file_sum1,
475 sizeof(uint32));
476 read_batch_csums_file(file_sum2, csum_length);
477
478 if ((s->sums[i].sum1 != file_sum1) ||
479 (memcmp
480 (s->sums[i].sum2, file_sum2,
481 csum_length) != 0)) {
482 *checksums_match = 0;
483 }
484 } /* end for */
485 }
486
487}
488
489void write_batch_delta_file(char *buff, int bytes_to_write)
490{
491 static int fdb_delta_open = 1;
492
493 if (fdb_delta_open) {
494 /* Set up file extension */
495 strcat(rsync_delta_file, batch_file_ext);
496
497 /* Open batch delta file for writing; create it if it doesn't exist */
498 fdb_delta =
499 do_open(rsync_delta_file, O_WRONLY | O_CREAT | O_TRUNC,
500 S_IREAD | S_IWRITE);
501 if (fdb_delta == -1) {
502 rprintf(FERROR, "Batch file %s open error: %s\n",
503 rsync_delta_file, strerror(errno));
504 close(fdb_delta);
505 exit_cleanup(1);
506 }
507 fdb_delta_open = 0;
508 }
509
510 /* Write buffer to batch delta file */
511
512 if (write(fdb_delta, buff, bytes_to_write) == -1) {
513 rprintf(FERROR, "Batch file %s write error: %s\n",
514 rsync_delta_file, strerror(errno));
515 close(fdb_delta);
516 exit_cleanup(1);
517 }
518}
519void close_batch_delta_file()
520{
521 close(fdb_delta);
522
523}
524
525int read_batch_delta_file(char *buff, int len)
526{
527 static int fdb_delta_open = 1;
528 int bytes_read;
529
530 if (fdb_delta_open) {
531
532 /* Set up file extension */
533 strcat(rsync_delta_file, batch_file_ext);
534
535 /* Open batch flist file for reading */
536 fdb_delta = do_open(rsync_delta_file, O_RDONLY, 0);
537 if (fdb_delta == -1) {
538 rprintf(FERROR, "Batch file %s open error: %s\n",
539 rsync_delta_file, strerror(errno));
540 close(fdb_delta);
541 exit_cleanup(1);
542 }
543 fdb_delta_open = 0;
544 }
545
546 /* Read delta batch file */
547
548 bytes_read = read(fdb_delta, buff, len);
549
550 if (bytes_read == -1) {
551 rprintf(FERROR, "Batch file %s read error: %s\n",
552 rsync_delta_file, strerror(errno));
553 close(fdb_delta);
554 exit_cleanup(1);
555 }
556 return bytes_read;
557}
558
559
560void show_flist(int index, struct file_struct **fptr)
561{
562 /* for debugging show_flist(flist->count, flist->files * */
563
564 int i;
565 for (i = 0; i < index; i++) {
566 rprintf(FINFO, "flist->flags=%#x\n", fptr[i]->flags);
567 rprintf(FINFO, "flist->modtime=%#lx\n",
568 (long unsigned) fptr[i]->modtime);
569 rprintf(FINFO, "flist->length=%.0f\n",
570 (double) fptr[i]->length);
571 rprintf(FINFO, "flist->mode=%#o\n", (int) fptr[i]->mode);
572 rprintf(FINFO, "flist->basename=%s\n", fptr[i]->basename);
573 if (fptr[i]->dirname)
574 rprintf(FINFO, "flist->dirname=%s\n",
575 fptr[i]->dirname);
576 if (fptr[i]->basedir)
577 rprintf(FINFO, "flist->basedir=%s\n",
578 fptr[i]->basedir);
579 }
580}
581
582void show_argvs(int argc, char *argv[])
583{
584 /* for debugging * */
585
586 int i;
587 rprintf(FINFO, "BATCH.C:show_argvs,argc=%d\n", argc);
588 for (i = 0; i < argc; i++) {
589 /* if (argv[i]) */
590 rprintf(FINFO, "i=%d,argv[i]=%s\n", i, argv[i]);
591
592 }
593}