1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
|
/* simple program to check implanted md5sum in an iso 9660 image */
/* Copyright 2001 Red Hat, Inc. */
/* Michael Fulbright msf@redhat.com */
/* 4/2005 Dustin Kirkland (dustin.kirkland@gmail.com) */
/* Added support for checkpoint fragment sums; */
/* Exits media check as soon as bad fragment md5sum'ed */
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <newt.h>
#include "md5.h"
#include "log.h"
#include "mediacheck.h"
#define APPDATA_OFFSET 883
#define SIZE_OFFSET 84
#define MAX(x, y) ((x > y) ? x : y)
#define MIN(x, y) ((x < y) ? x : y)
typedef void (*checkCallback)(void *, long long offset);
struct progressCBdata {
newtComponent scale;
newtComponent label;
};
#ifdef TESTING
#define _(x) (x)
#else
#include "lang.h"
#endif
/* finds primary volume descriptor and returns info from it */
/* mediasum must be a preallocated buffer at least 33 bytes long */
/* fragmentsums must be a preallocated buffer at least FRAGMENT_SUM_LENGTH+1 bytes long */
int parsepvd(int isofd, char *mediasum, int *skipsectors, long long *isosize, int *supported, char *fragmentsums, long long *fragmentcount) {
unsigned char buf[2048];
char buf2[512];
char tmpbuf[512];
int skipfnd, md5fnd, supportedfnd, fragsumfnd, fragcntfnd;
unsigned int loc;
long long offset;
char *p;
if (lseek(isofd, (off_t)(16L * 2048L), SEEK_SET) == -1)
return ((long long)-1);
offset = (16L * 2048L);
for (;1;) {
if (read(isofd, buf, 2048) <= 0)
return ((long long)-1);
if (buf[0] == 1)
/* found primary volume descriptor */
break;
else if (buf[0] == 255)
/* hit end and didn't find primary volume descriptor */
return ((long long)-1);
offset += 2048L;
}
/* read out md5sum */
memcpy(buf2, buf + APPDATA_OFFSET, 512);
buf2[511] = '\0';
*supported = 0;
md5fnd = 0;
skipfnd = 0;
fragsumfnd = 0;
fragcntfnd = 0;
supportedfnd = 0;
loc = 0;
while (loc < 512) {
if (!strncmp(buf2 + loc, "ISO MD5SUM = ", 13)) {
/* make sure we dont walk off end */
if ((loc + 32) > 511)
return -1;
memcpy(mediasum, buf2 + loc + 13, 32);
mediasum[32] = '\0';
md5fnd = 1;
logMessage(DEBUGLVL, "MD5SUM -> %s", mediasum);
loc += 45;
for (p=buf2+loc; *p != ';' && loc < 512; p++, loc++);
} else if (!strncmp(buf2 + loc, "SKIPSECTORS = ", 14)) {
char *errptr;
/* make sure we dont walk off end */
if ((loc + 14) > 511)
return -1;
loc = loc + 14;
for (p=tmpbuf; buf2[loc] != ';' && loc < 512; p++, loc++)
*p = buf2[loc];
*p = '\0';
/* logMessage(DEBUGLVL, "SKIPSECTORS -> |%s|", tmpbuf); */
*skipsectors = strtol(tmpbuf, &errptr, 10);
if (errptr && *errptr) {
logMessage(ERROR, "Could not parse |%s|", errptr);
return -1;
} else {
logMessage(DEBUGLVL, "skipsectors = %d", *skipsectors);
skipfnd = 1;
}
for (p=buf2+loc; *p != ';' && loc < 512; p++, loc++);
} else if (!strncmp(buf2 + loc, "RHLISOSTATUS=1", 14)) {
*supported = 1;
supportedfnd = 1;
logMessage(INFO, "supported = 1");
for (p=buf2+loc; *p != ';' && loc < 512; p++, loc++);
} else if (!strncmp(buf2 + loc, "RHLISOSTATUS=0", 14)) {
*supported = 0;
supportedfnd = 1;
logMessage(INFO, "supported = 0");
for (p=buf2+loc; *p != ';' && loc < 512; p++, loc++);
} else if (!strncmp(buf2 + loc, "FRAGMENT SUMS = ", 16)) {
/* make sure we dont walk off end */
if ((loc + FRAGMENT_SUM_LENGTH) > 511)
return -1;
memcpy(fragmentsums, buf2 + loc + 16, FRAGMENT_SUM_LENGTH);
fragmentsums[FRAGMENT_SUM_LENGTH] = '\0';
fragsumfnd = 1;
loc += FRAGMENT_SUM_LENGTH + 16;
for (p=buf2+loc; *p != ';' && loc < 512; p++, loc++);
} else if (!strncmp(buf2 + loc, "FRAGMENT COUNT = ", 17)) {
char *errptr;
/* make sure we dont walk off end */
if ((loc + 17) > 511)
return -1;
loc = loc + 17;
for (p=tmpbuf; buf2[loc] != ';' && loc < 512; p++, loc++)
*p = buf2[loc];
*p = '\0';
*fragmentcount = strtol(tmpbuf, &errptr, 10);
if (errptr && *errptr) {
return -1;
} else {
fragcntfnd = 1;
}
for (p=buf2+loc; *p != ';' && loc < 512; p++, loc++);
} else {
loc++;
}
if ((skipfnd & md5fnd & fragsumfnd & fragcntfnd) & supportedfnd)
break;
}
if (!(skipfnd & md5fnd))
return -1;
/* get isosize */
*isosize = (buf[SIZE_OFFSET]*0x1000000+buf[SIZE_OFFSET+1]*0x10000 +
buf[SIZE_OFFSET+2]*0x100 + buf[SIZE_OFFSET+3]) * 2048LL;
return offset;
}
/* returns -1 if no checksum encoded in media, 0 if no match, 1 if match */
/* mediasum is the sum encoded in media, computedsum is one we compute */
/* both strings must be pre-allocated at least 33 chars in length */
static int checkmd5sum(int isofd, char *mediasum, char *computedsum,
checkCallback cb, void *cbdata) {
int nread;
int i, j;
int appdata_start_offset, appdata_end_offset;
int nattempt;
int skipsectors;
int supported;
int current_fragment = 0;
int previous_fragment = 0;
unsigned int bufsize = 32768;
unsigned char md5sum[16];
unsigned char fragmd5sum[16];
unsigned int len;
unsigned char *buf;
long long isosize, offset, pvd_offset, apoff;
char fragmentsums[FRAGMENT_SUM_LENGTH+1];
char thisfragsum[FRAGMENT_SUM_LENGTH+1];
long long fragmentcount = 0;
MD5_CTX md5ctx, fragmd5ctx;
if ((pvd_offset = parsepvd(isofd, mediasum, &skipsectors, &isosize, &supported, fragmentsums, &fragmentcount)) < 0)
return -1;
/* rewind, compute md5sum */
lseek(isofd, 0L, SEEK_SET);
MD5_Init(&md5ctx);
offset = 0;
apoff = pvd_offset + APPDATA_OFFSET;
buf = malloc(bufsize * sizeof(unsigned char));
while (offset < isosize - skipsectors*2048) {
nattempt = MIN(isosize - skipsectors*2048 - offset, bufsize);
nread = read(isofd, buf, nattempt);
if (nread <= 0)
break;
if (nread > nattempt) {
fprintf(stderr, "Warning: read got more data than requested\n");
nread = nattempt;
lseek(isofd, offset+nread, SEEK_SET);
}
/* overwrite md5sum we implanted with original data */
if (offset < apoff && offset+nread >= apoff) {
appdata_start_offset = apoff - offset;
appdata_end_offset = MIN(appdata_start_offset+MIN(nread, 512),
offset + nread - apoff);
len = appdata_end_offset - appdata_start_offset;
memset(buf+appdata_start_offset, ' ', len);
} else if (offset >= apoff && offset+nread < apoff + 512) {
appdata_start_offset = 0;
appdata_end_offset = nread;
len = appdata_end_offset - appdata_start_offset;
memset(buf+appdata_start_offset, ' ', len);
} else if (offset < apoff + 512 && offset+nread >= apoff + 512) {
appdata_start_offset = 0;
appdata_end_offset = apoff + 512 - offset;
len = appdata_end_offset - appdata_start_offset;
memset(buf+appdata_start_offset, ' ', len);
}
MD5_Update(&md5ctx, buf, nread);
if (fragmentcount) {
current_fragment = offset * (fragmentcount+1) / (isosize - skipsectors*2048);
/* if we're onto the next fragment, calculate the previous sum and check */
if ( current_fragment != previous_fragment ) {
memcpy(&fragmd5ctx, &md5ctx, sizeof(MD5_CTX));
MD5_Final(fragmd5sum, &fragmd5ctx);
*computedsum = '\0';
j = (current_fragment-1)*FRAGMENT_SUM_LENGTH/fragmentcount;
for (i=0; i<FRAGMENT_SUM_LENGTH/fragmentcount; i++) {
char tmpstr[2];
snprintf(tmpstr, 2, "%01x", fragmd5sum[i]);
strncat(computedsum, tmpstr, 2);
thisfragsum[i] = fragmentsums[j++];
}
thisfragsum[j] = '\0';
/* printf("\nFragment [%i]: %s ?= %s\n", previous_fragment, computedsum, thisfragsum); */
previous_fragment = current_fragment;
/* Exit immediatiately if current fragment sum is incorrect */
if (strcmp(thisfragsum, computedsum) != 0) {
free(buf);
return 0;
}
}
}
offset = offset + nread;
if (cb)
cb(cbdata, offset);
}
if (cb)
cb(cbdata, isosize);
sleep(1);
free(buf);
MD5_Final(md5sum, &md5ctx);
*computedsum = '\0';
for (i=0; i<16; i++) {
char tmpstr[4];
snprintf (tmpstr, 4, "%02x", md5sum[i]);
strncat(computedsum, tmpstr, 2);
}
if (strcmp(mediasum, computedsum))
return 0;
else
return 1;
}
static void readCB(void *co, long long pos) {
struct progressCBdata *data = co;
static int tick = 0;
char tickmark[2] = "-";
char * ticks = "-\\|/";
newtScaleSet(data->scale, pos);
tick++;
if (tick > 399) tick = 0;
*tickmark = ticks[tick / 100];
newtLabelSetText(data->label, tickmark);
newtRefresh();
}
static int doMediaCheck(int isofd, char *descr, char *mediasum, char *computedsum, long long *isosize, int *supported) {
struct progressCBdata data;
newtComponent t, f, scale, label;
int rc;
int dlen;
int llen;
int skipsectors;
char tmpstr[1024];
long long fragmentcount = 0;
char fragmentsums[FRAGMENT_SUM_LENGTH+1];
if (parsepvd(isofd, mediasum, &skipsectors, isosize, supported, fragmentsums, &fragmentcount) < 0) {
newtWinMessage(_("Error"), _("OK"),
_("Unable to read the disc checksum from the "
"primary volume descriptor. This probably "
"means the disc was created without adding the "
"checksum."));
return -1;
}
if (descr)
snprintf(tmpstr, sizeof(tmpstr), _("Checking \"%s\"..."), descr);
else
snprintf(tmpstr, sizeof(tmpstr), _("Checking media now..."));
dlen = strlen(tmpstr);
if (dlen > 65)
dlen = 65;
newtCenteredWindow(dlen+8, 6, _("Media Check"));
t = newtTextbox(1, 1, dlen+4, 3, NEWT_TEXTBOX_WRAP);
newtTextboxSetText(t, tmpstr);
llen = strlen(tmpstr);
label = newtLabel(llen+1, 1, "-");
f = newtForm(NULL, NULL, 0);
newtFormAddComponent(f, t);
scale = newtScale(3, 3, dlen, *isosize);
newtFormAddComponent(f, scale);
newtDrawForm(f);
newtRefresh();
data.scale = scale;
data.label = label;
rc = checkmd5sum(isofd, mediasum, computedsum, readCB, &data);
newtFormDestroy(f);
newtPopWindow();
return rc;
}
int mediaCheckFile(char *file, char *descr) {
int isofd;
int rc;
int supported;
char *result;
char mediasum[33], computedsum[33];
char tmpstr[512];
char descrstr[256];
long long isosize;
newtComponent t, f;
isofd = open(file, O_RDONLY);
if (isofd < 0) {
newtWinMessage(_("Error"), _("OK"), _("Unable to find install image "
"%s"), file);
return -1;
}
supported = 0;
rc = doMediaCheck(isofd, descr, mediasum, computedsum, &isosize, &supported);
close(isofd);
if (rc == 0) {
result = _("FAIL.\n\n"
"The image which was just tested has errors. "
"This could be due to a "
"corrupt download or a bad disc. "
"If applicable, please clean the disc "
"and try again. If this test continues to fail you "
"should not continue the install.");
logMessage(ERROR, "mediacheck: %s (%s) FAILED", file, descr);
logMessage(ERROR, "value of supported iso flag is %d", supported);
} else if (rc > 0) {
result = _("PASS.\n\nIt is OK to install from this media.");
logMessage(INFO, "mediacheck: %s (%s) PASSED", file, descr);
logMessage(INFO, "value of supported iso flag is %d", supported);
} else {
result = _("NA.\n\nNo checksum information available, unable to verify media.");
logMessage(WARNING, "mediacheck: %s (%s) has no checksum info", file, descr);
}
newtCenteredWindow(60, 20, _("Media Check Result"));
t = newtTextbox(4, 1, 56, 18, NEWT_TEXTBOX_WRAP);
if (descr)
snprintf(descrstr, sizeof(descrstr),
_("of the image:\n\n%s\n\n"), descr);
else
descrstr[0] = '\0';
snprintf(tmpstr, sizeof(tmpstr), _("The media check %sis complete, and "
"the result is: %s\n"), descrstr, result);
newtTextboxSetText(t, tmpstr);
f = newtForm(NULL, NULL, 0);
newtFormAddComponent(f, t);
newtFormAddComponent(f, newtButton(26, 15, _("OK")));
newtRunForm(f);
newtFormDestroy(f);
newtPopWindow();
return rc;
}
#ifdef TESTING
int main(int argc, char **argv) {
int rc;
if (argc < 2) {
printf("Usage: checkisomd5 <isofilename>\n\n");
exit(1);
}
newtInit();
newtCls();
rc = mediaCheckFile(argv[1], "TESTING");
newtFinished();
exit (0);
}
#endif
|