Avoid leaving a file open on error return.
[rsync.git] / checksum.c
1 /*
2  * Routines to support checksumming of bytes.
3  *
4  * Copyright (C) 1996 Andrew Tridgell
5  * Copyright (C) 1996 Paul Mackerras
6  * Copyright (C) 2004-2018 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 extern int checksum_seed;
25 extern int protocol_version;
26 extern int proper_seed_order;
27 extern char *checksum_choice;
28
29 #define CSUM_NONE 0
30 #define CSUM_MD4_ARCHAIC 1
31 #define CSUM_MD4_BUSTED 2
32 #define CSUM_MD4_OLD 3
33 #define CSUM_MD4 4
34 #define CSUM_MD5 5
35
36 int xfersum_type = 0; /* used for the file transfer checksums */
37 int checksum_type = 0; /* used for the pre-transfer (--checksum) checksums */
38
39 /* Returns 1 if --whole-file must be enabled. */
40 int parse_checksum_choice(void)
41 {
42         char *cp = checksum_choice ? strchr(checksum_choice, ',') : NULL;
43         if (cp) {
44                 xfersum_type = parse_csum_name(checksum_choice, cp - checksum_choice);
45                 checksum_type = parse_csum_name(cp+1, -1);
46         } else
47                 xfersum_type = checksum_type = parse_csum_name(checksum_choice, -1);
48         return xfersum_type == CSUM_NONE;
49 }
50
51 int parse_csum_name(const char *name, int len)
52 {
53         if (len < 0 && name)
54                 len = strlen(name);
55
56         if (!name || (len == 4 && strncasecmp(name, "auto", 4) == 0)) {
57                 if (protocol_version >= 30)
58                         return CSUM_MD5;
59                 if (protocol_version >= 27)
60                         return CSUM_MD4_OLD;
61                 if (protocol_version >= 21)
62                         return CSUM_MD4_BUSTED;
63                 return CSUM_MD4_ARCHAIC;
64         }
65         if (len == 3 && strncasecmp(name, "md4", 3) == 0)
66                 return CSUM_MD4;
67         if (len == 3 && strncasecmp(name, "md5", 3) == 0)
68                 return CSUM_MD5;
69         if (len == 4 && strncasecmp(name, "none", 4) == 0)
70                 return CSUM_NONE;
71
72         rprintf(FERROR, "unknown checksum name: %s\n", name);
73         exit_cleanup(RERR_UNSUPPORTED);
74 }
75
76 int csum_len_for_type(int cst, BOOL flist_csum)
77 {
78         switch (cst) {
79           case CSUM_NONE:
80                 return 1;
81           case CSUM_MD4_ARCHAIC:
82                 /* The oldest checksum code is rather weird: the file-list code only sent
83                  * 2-byte checksums, but all other checksums were full MD4 length. */
84                 return flist_csum ? 2 : MD4_DIGEST_LEN;
85           case CSUM_MD4:
86           case CSUM_MD4_OLD:
87           case CSUM_MD4_BUSTED:
88                 return MD4_DIGEST_LEN;
89           case CSUM_MD5:
90                 return MD5_DIGEST_LEN;
91           default: /* paranoia to prevent missing case values */
92                 exit_cleanup(RERR_UNSUPPORTED);
93         }
94         return 0;
95 }
96
97 int canonical_checksum(int csum_type)
98 {
99     return csum_type >= CSUM_MD4 ? 1 : 0;
100 }
101
102 /*
103   a simple 32 bit checksum that can be upadted from either end
104   (inspired by Mark Adler's Adler-32 checksum)
105   */
106 uint32 get_checksum1(char *buf1, int32 len)
107 {
108     int32 i;
109     uint32 s1, s2;
110     schar *buf = (schar *)buf1;
111
112     s1 = s2 = 0;
113     for (i = 0; i < (len-4); i+=4) {
114         s2 += 4*(s1 + buf[i]) + 3*buf[i+1] + 2*buf[i+2] + buf[i+3] +
115           10*CHAR_OFFSET;
116         s1 += (buf[i+0] + buf[i+1] + buf[i+2] + buf[i+3] + 4*CHAR_OFFSET);
117     }
118     for (; i < len; i++) {
119         s1 += (buf[i]+CHAR_OFFSET); s2 += s1;
120     }
121     return (s1 & 0xffff) + (s2 << 16);
122 }
123
124 void get_checksum2(char *buf, int32 len, char *sum)
125 {
126         md_context m;
127
128         switch (xfersum_type) {
129           case CSUM_MD5: {
130                 uchar seedbuf[4];
131                 md5_begin(&m);
132                 if (proper_seed_order) {
133                         if (checksum_seed) {
134                                 SIVALu(seedbuf, 0, checksum_seed);
135                                 md5_update(&m, seedbuf, 4);
136                         }
137                         md5_update(&m, (uchar *)buf, len);
138                 } else {
139                         md5_update(&m, (uchar *)buf, len);
140                         if (checksum_seed) {
141                                 SIVALu(seedbuf, 0, checksum_seed);
142                                 md5_update(&m, seedbuf, 4);
143                         }
144                 }
145                 md5_result(&m, (uchar *)sum);
146                 break;
147           }
148           case CSUM_MD4:
149           case CSUM_MD4_OLD:
150           case CSUM_MD4_BUSTED:
151           case CSUM_MD4_ARCHAIC: {
152                 int32 i;
153                 static char *buf1;
154                 static int32 len1;
155
156                 mdfour_begin(&m);
157
158                 if (len > len1) {
159                         if (buf1)
160                                 free(buf1);
161                         buf1 = new_array(char, len+4);
162                         len1 = len;
163                         if (!buf1)
164                                 out_of_memory("get_checksum2");
165                 }
166
167                 memcpy(buf1, buf, len);
168                 if (checksum_seed) {
169                         SIVAL(buf1,len,checksum_seed);
170                         len += 4;
171                 }
172
173                 for (i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK)
174                         mdfour_update(&m, (uchar *)(buf1+i), CSUM_CHUNK);
175
176                 /*
177                  * Prior to version 27 an incorrect MD4 checksum was computed
178                  * by failing to call mdfour_tail() for block sizes that
179                  * are multiples of 64.  This is fixed by calling mdfour_update()
180                  * even when there are no more bytes.
181                  */
182                 if (len - i > 0 || xfersum_type > CSUM_MD4_BUSTED)
183                         mdfour_update(&m, (uchar *)(buf1+i), len-i);
184
185                 mdfour_result(&m, (uchar *)sum);
186                 break;
187           }
188           default: /* paranoia to prevent missing case values */
189                 exit_cleanup(RERR_UNSUPPORTED);
190         }
191 }
192
193 void file_checksum(const char *fname, const STRUCT_STAT *st_p, char *sum)
194 {
195         struct map_struct *buf;
196         OFF_T i, len = st_p->st_size;
197         md_context m;
198         int32 remainder;
199         int fd;
200
201         memset(sum, 0, MAX_DIGEST_LEN);
202
203         fd = do_open(fname, O_RDONLY, 0);
204         if (fd == -1)
205                 return;
206
207         buf = map_file(fd, len, MAX_MAP_SIZE, CSUM_CHUNK);
208
209         switch (checksum_type) {
210           case CSUM_MD5:
211                 md5_begin(&m);
212
213                 for (i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK) {
214                         md5_update(&m, (uchar *)map_ptr(buf, i, CSUM_CHUNK),
215                                    CSUM_CHUNK);
216                 }
217
218                 remainder = (int32)(len - i);
219                 if (remainder > 0)
220                         md5_update(&m, (uchar *)map_ptr(buf, i, remainder), remainder);
221
222                 md5_result(&m, (uchar *)sum);
223                 break;
224           case CSUM_MD4:
225           case CSUM_MD4_OLD:
226           case CSUM_MD4_BUSTED:
227           case CSUM_MD4_ARCHAIC:
228                 mdfour_begin(&m);
229
230                 for (i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK) {
231                         mdfour_update(&m, (uchar *)map_ptr(buf, i, CSUM_CHUNK),
232                                       CSUM_CHUNK);
233                 }
234
235                 /* Prior to version 27 an incorrect MD4 checksum was computed
236                  * by failing to call mdfour_tail() for block sizes that
237                  * are multiples of 64.  This is fixed by calling mdfour_update()
238                  * even when there are no more bytes. */
239                 remainder = (int32)(len - i);
240                 if (remainder > 0 || checksum_type > CSUM_MD4_BUSTED)
241                         mdfour_update(&m, (uchar *)map_ptr(buf, i, remainder), remainder);
242
243                 mdfour_result(&m, (uchar *)sum);
244                 break;
245           default:
246                 rprintf(FERROR, "invalid checksum-choice for the --checksum option (%d)\n", checksum_type);
247                 exit_cleanup(RERR_UNSUPPORTED);
248         }
249
250         close(fd);
251         unmap_file(buf);
252 }
253
254 static int32 sumresidue;
255 static md_context md;
256 static int cursum_type;
257
258 void sum_init(int csum_type, int seed)
259 {
260         char s[4];
261
262         if (csum_type < 0)
263                 csum_type = parse_csum_name(NULL, 0);
264         cursum_type = csum_type;
265
266         switch (csum_type) {
267           case CSUM_MD5:
268                 md5_begin(&md);
269                 break;
270           case CSUM_MD4:
271                 mdfour_begin(&md);
272                 sumresidue = 0;
273                 break;
274           case CSUM_MD4_OLD:
275           case CSUM_MD4_BUSTED:
276           case CSUM_MD4_ARCHAIC:
277                 mdfour_begin(&md);
278                 sumresidue = 0;
279                 SIVAL(s, 0, seed);
280                 sum_update(s, 4);
281                 break;
282           case CSUM_NONE:
283                 break;
284           default: /* paranoia to prevent missing case values */
285                 exit_cleanup(RERR_UNSUPPORTED);
286         }
287 }
288
289 /**
290  * Feed data into an MD4 accumulator, md.  The results may be
291  * retrieved using sum_end().  md is used for different purposes at
292  * different points during execution.
293  *
294  * @todo Perhaps get rid of md and just pass in the address each time.
295  * Very slightly clearer and slower.
296  **/
297 void sum_update(const char *p, int32 len)
298 {
299         switch (cursum_type) {
300           case CSUM_MD5:
301                 md5_update(&md, (uchar *)p, len);
302                 break;
303           case CSUM_MD4:
304           case CSUM_MD4_OLD:
305           case CSUM_MD4_BUSTED:
306           case CSUM_MD4_ARCHAIC:
307                 if (len + sumresidue < CSUM_CHUNK) {
308                         memcpy(md.buffer + sumresidue, p, len);
309                         sumresidue += len;
310                         break;
311                 }
312
313                 if (sumresidue) {
314                         int32 i = CSUM_CHUNK - sumresidue;
315                         memcpy(md.buffer + sumresidue, p, i);
316                         mdfour_update(&md, (uchar *)md.buffer, CSUM_CHUNK);
317                         len -= i;
318                         p += i;
319                 }
320
321                 while (len >= CSUM_CHUNK) {
322                         mdfour_update(&md, (uchar *)p, CSUM_CHUNK);
323                         len -= CSUM_CHUNK;
324                         p += CSUM_CHUNK;
325                 }
326
327                 sumresidue = len;
328                 if (sumresidue)
329                         memcpy(md.buffer, p, sumresidue);
330                 break;
331           case CSUM_NONE:
332                 break;
333           default: /* paranoia to prevent missing case values */
334                 exit_cleanup(RERR_UNSUPPORTED);
335         }
336 }
337
338 /* NOTE: all the callers of sum_end() pass in a pointer to a buffer that is
339  * MAX_DIGEST_LEN in size, so even if the csum-len is shorter that that (i.e.
340  * CSUM_MD4_ARCHAIC), we don't have to worry about limiting the data we write
341  * into the "sum" buffer. */
342 int sum_end(char *sum)
343 {
344         switch (cursum_type) {
345           case CSUM_MD5:
346                 md5_result(&md, (uchar *)sum);
347                 break;
348           case CSUM_MD4:
349           case CSUM_MD4_OLD:
350                 mdfour_update(&md, (uchar *)md.buffer, sumresidue);
351                 mdfour_result(&md, (uchar *)sum);
352                 break;
353           case CSUM_MD4_BUSTED:
354           case CSUM_MD4_ARCHAIC:
355                 if (sumresidue)
356                         mdfour_update(&md, (uchar *)md.buffer, sumresidue);
357                 mdfour_result(&md, (uchar *)sum);
358                 break;
359           case CSUM_NONE:
360                 *sum = '\0';
361                 break;
362           default: /* paranoia to prevent missing case values */
363                 exit_cleanup(RERR_UNSUPPORTED);
364         }
365
366         return csum_len_for_type(cursum_type, 0);
367 }