void printMD5SUM(char *file) { int isofd; char mediasum[64]; long long isosize; char fragmentsums[FRAGMENT_SUM_LENGTH+1]; long long fragmentcount = 0; int supported; int skipsectors; isofd = open(file, O_RDONLY); if (isofd < 0) { fprintf(stderr, "%s: Unable to find install image.\n", file); exit(1); } if (parsepvd(isofd, mediasum, &skipsectors, &isosize, &supported, fragmentsums, &fragmentcount) < 0) { fprintf(stderr, "%s: Could not get pvd data", file); fprintf(stderr, "\nUnable to read the disc checksum from the " "primary volume descriptor.\nThis probably " "means the disc was created without adding the " "checksum."); exit(1); } close(isofd); printf("%s: %s\n", file, mediasum); if ( (strlen(fragmentsums) > 0) && (fragmentcount > 0) ) { printf("Fragment sums: %s\n", fragmentsums); printf("Fragment count: %lld\n", fragmentcount); } }
int printMD5SUM(char *file) { int isofd; char mediasum[64]; long long isosize; char fragmentsums[FRAGMENT_SUM_LENGTH+1]; long long fragmentcount = 0; int supported; int skipsectors; isofd = open(file, O_RDONLY); if (isofd < 0) { return ISOMD5SUM_FILE_NOT_FOUND; } if (parsepvd(isofd, mediasum, &skipsectors, &isosize, &supported, fragmentsums, &fragmentcount) < 0) { return ISOMD5SUM_CHECK_NOT_FOUND; } close(isofd); printf("%s: %s\n", file, mediasum); if ( (strlen(fragmentsums) > 0) && (fragmentcount > 0) ) { printf("Fragment sums: %s\n", fragmentsums); printf("Fragment count: %lld\n", fragmentcount); printf("Supported ISO: %s\n", supported ? "yes" : "no"); } return 0; }
void printMD5SUM(char *file) { int isofd; char mediasum[64]; long long isosize; int supported; int skipsectors; isofd = open(file, O_RDONLY); if (isofd < 0) { fprintf(stderr, "%s: Unable to find install image.\n", file); exit(1); } if (parsepvd(isofd, mediasum, &skipsectors, &isosize, &supported) < 0) { fprintf(stderr, "%s: Could not get pvd data", file); fprintf(stderr, "\nUnable to read the disc checksum from the " "primary volume descriptor.\nThis probably " "means the disc was created without adding the " "checksum."); exit(1); } close(isofd); printf("%s: %s\n", file, mediasum); }
static int doMediaCheck(int isofd, char *mediasum, char *computedsum, long long *isosize, int *supported, checkCallback cb, void *cbdata) { int rc; int skipsectors; long long fragmentcount = 0; char fragmentsums[FRAGMENT_SUM_LENGTH+1]; if (parsepvd(isofd, mediasum, &skipsectors, isosize, supported, fragmentsums, &fragmentcount) < 0) { return ISOMD5SUM_CHECK_NOT_FOUND; } rc = checkmd5sum(isofd, mediasum, computedsum, cb, cbdata); return rc; }
static int doMediaCheck(int isofd, char *mediasum, char *computedsum, long long *isosize, int *supported, int quiet) { int rc; int skipsectors; if (parsepvd(isofd, mediasum, &skipsectors, isosize, supported) < 0) { fprintf(stderr, "Unable to read the disc checksum from the " "primary volume descriptor.\nThis probably " "means the disc was created without adding the " "checksum.\n"); return -1; } rc = checkmd5sum(isofd, mediasum, computedsum, quiet); return rc; }
/* if returns 1 we found status, and mediasum will be checksum */ static int getISOStatusFromFD(int isofd, char *mediasum) { char tmpsum[33]; char fragmentsums[FRAGMENT_SUM_LENGTH+1]; int skipsectors, isostatus; long long isosize, pvd_offset, fragmentcount = 0; if (mediasum) mediasum[0] = '\0'; fragmentsums[0] = '\0'; pvd_offset = parsepvd(isofd, tmpsum, &skipsectors, &isosize, &isostatus, fragmentsums, &fragmentcount); if (pvd_offset < 0) { logMessage(ERROR, "Could not parse pvd"); return 0; } if (mediasum) strcpy(mediasum, tmpsum); return isostatus; }
int implantISOFile(char *fname, int supported, int forceit, int quiet, char **errstr) { int i; int isofd; int nread; int dirty; int pvd_offset; int current_fragment = 0; int previous_fragment = 0; int nattempt; long long isosize, total; unsigned char md5sum[16]; unsigned char fragmd5sum[16]; unsigned int loc; unsigned int bufsize = 32768; unsigned char *buf; unsigned char orig_appdata[512]; unsigned char new_appdata[512]; char mediasum[33]; char md5str[40]; char fragstr[FRAGMENT_SUM_LENGTH+1]; MD5_CTX md5ctx, fragmd5ctx; isofd = open(fname, O_RDWR); if (isofd < 0) { *errstr = "Error - Unable to open file %s\n\n"; return -1; } pvd_offset = parsepvd(isofd, mediasum, &isosize); if (pvd_offset < 0) { *errstr = "Could not find primary volumne!\n\n"; return -1; } lseek(isofd, pvd_offset + APPDATA_OFFSET, SEEK_SET); nread = read(isofd, orig_appdata, 512); if (!forceit) { dirty = 0; for (i=0; i < 512; i++) if (orig_appdata[i] != ' ') dirty = 1; if (dirty) { *errstr = "Application data has been used - not implanting md5sum!\n"; return -1; } } else { /* write out blanks to erase old app data */ lseek(isofd, pvd_offset + APPDATA_OFFSET, SEEK_SET); memset(new_appdata, ' ', 512); i = write(isofd, new_appdata, 512); if (i<0) { printf("write failed %d\n", i); perror(""); } } /* now do md5sum */ lseek(isofd, 0L, SEEK_SET); MD5_Init(&md5ctx); *fragstr = '\0'; buf = malloc(bufsize * sizeof(unsigned char)); total = 0; /* read up to 15 sectors from end, due to problems reading last few */ /* sectors on burned CDs */ while (total < isosize - SKIPSECTORS*2048) { nattempt = MIN(isosize - SKIPSECTORS*2048 - total, bufsize); nread = read(isofd, buf, nattempt); if (nread <= 0) break; MD5_Update(&md5ctx, buf, nread); /* if we're onto the next fragment, calculate the previous sum and write */ current_fragment = total * (FRAGMENT_COUNT+1) / (isosize - SKIPSECTORS*2048); if ( current_fragment != previous_fragment ) { memcpy(&fragmd5ctx, &md5ctx, sizeof(MD5_CTX)); MD5_Final(fragmd5sum, &fragmd5ctx); for (i=0; i<FRAGMENT_SUM_LENGTH/FRAGMENT_COUNT; i++) { char tmpstr[2]; snprintf(tmpstr, 2, "%01x", fragmd5sum[i]); strncat(fragstr, tmpstr, 2); } /* printf("\nFragment [%i]: %s\n", previous_fragment, fragstr); */ previous_fragment = current_fragment; } total = total + nread; } free(buf); MD5_Final(md5sum, &md5ctx); *md5str = '\0'; for (i=0; i<16; i++) { char tmpstr[4]; snprintf (tmpstr, 4, "%02x", md5sum[i]); strncat(md5str, tmpstr, 2); } if (!quiet) { printf("Inserting md5sum into iso image...\n"); printf("md5 = %s\n", md5str); printf("Inserting fragment md5sums into iso image...\n"); printf("fragmd5 = %s\n", fragstr); printf("frags = %d\n", FRAGMENT_COUNT); } /* memcpy(new_appdata, orig_appdata, 512); */ memset(new_appdata, ' ', 512); loc = 0; loc = writeAppData(new_appdata, "ISO MD5SUM = ", loc); loc = writeAppData(new_appdata, md5str, loc); loc = writeAppData(new_appdata, ";", loc); buf = malloc(512 * sizeof(unsigned char)); snprintf((char *)buf, 512, "SKIPSECTORS = %d", SKIPSECTORS); loc = writeAppData(new_appdata, (char *)buf, loc); loc = writeAppData(new_appdata, ";", loc); free(buf); if (supported) { if (!quiet) printf("Setting supported flag to 1\n"); loc = writeAppData(new_appdata, "RHLISOSTATUS=1", loc); } else { if (!quiet) printf("Setting supported flag to 0\n"); loc = writeAppData(new_appdata, "RHLISOSTATUS=0", loc); } loc = writeAppData(new_appdata, ";", loc); loc = writeAppData(new_appdata, "FRAGMENT SUMS = ", loc); loc = writeAppData(new_appdata, fragstr, loc); loc = writeAppData(new_appdata, ";", loc); buf = malloc(512 * sizeof(unsigned char)); snprintf((char *)buf, 512, "FRAGMENT COUNT = %d", FRAGMENT_COUNT); loc = writeAppData(new_appdata, (char *)buf, loc); loc = writeAppData(new_appdata, ";", loc); free(buf); loc = writeAppData(new_appdata, "THIS IS NOT THE SAME AS RUNNING MD5SUM ON THIS ISO!!", loc); i = lseek(isofd, pvd_offset + APPDATA_OFFSET, SEEK_SET); if (i<0) printf("seek failed\n"); i = write(isofd, new_appdata, 512); if (i<0) { printf("write failed %d\n", i); perror(""); } close(isofd); errstr = NULL; return 0; }
/* 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 ISOMD5SUM_CHECK_NOT_FOUND; /* printf("Mediasum = %s\n",mediasum); */ /* rewind, compute md5sum */ lseek(isofd, 0L, SEEK_SET); MD5_Init(&md5ctx); offset = 0; apoff = pvd_offset + APPDATA_OFFSET; buf = malloc(bufsize * sizeof(unsigned char)); if (cb) cb(cbdata, 0, isosize - skipsectors*2048); while (offset < isosize - skipsectors*2048) { nattempt = MIN(isosize - skipsectors*2048 - offset, bufsize); /* printf("%lld %lld %lld %d\n", offset, isosize, isosize-SKIPSECTORS*2048, nattempt); */ nread = read(isofd, buf, nattempt); if (nread <= 0) break; if (nread > nattempt) { 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'; previous_fragment = current_fragment; /* Exit immediately if current fragment sum is incorrect */ if (strcmp(thisfragsum, computedsum) != 0) { return ISOMD5SUM_CHECK_FAILED; } } } offset = offset + nread; if (cb) if(cb(cbdata, offset, isosize - skipsectors*2048)) return ISOMD5SUM_CHECK_ABORTED; } if (cb) cb(cbdata, isosize, isosize - skipsectors*2048); 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); } /* printf("mediasum, computedsum = %s %s\n", mediasum, computedsum); */ if (strcmp(mediasum, computedsum)) return ISOMD5SUM_CHECK_FAILED; else return ISOMD5SUM_CHECK_PASSED; }
/* both strings must be pre-allocated at least 33 chars in length */ static int checkmd5sum(int isofd, char *mediasum, char *computedsum, int quiet) { 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; int printed_frag_status = 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]; char thisfragsum[FRAGMENT_SUM_LENGTH]; long long fragmentcount = 0; MD5_CTX md5ctx, fragmd5ctx; if ((pvd_offset = parsepvd(isofd, mediasum, &skipsectors, &isosize, &supported, fragmentsums, &fragmentcount)) < 0) return -1; /* printf("Mediasum = %s\n",mediasum); */ /* rewind, compute md5sum */ lseek(isofd, 0L, SEEK_SET); MD5_Init(&md5ctx); offset = 0; apoff = pvd_offset + APPDATA_OFFSET; buf = malloc(bufsize * sizeof(unsigned char)); if (!quiet) { printf("Percent complete: %05.1f%%", (100.0*offset)/(isosize-skipsectors*2048.0)); fflush(stdout); } while (offset < isosize - skipsectors*2048) { nattempt = MIN(isosize - skipsectors*2048 - offset, bufsize); /* printf("%lld %lld %lld %d\n", offset, isosize, isosize-SKIPSECTORS*2048, nattempt); */ 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'; if (!quiet) { printf(" Fragment[%02i/%02lld] -> OK", previous_fragment+1, fragmentcount); printed_frag_status = 1; fflush(stdout); } previous_fragment = current_fragment; /* Exit immediately if current fragment sum is incorrect */ if (strcmp(thisfragsum, computedsum) != 0) { if (!quiet) { printf("\nFragment %02i of %02lld is BAD!\n", previous_fragment+1, fragmentcount); } free(buf); return 0; } } } offset = offset + nread; if (!quiet) { if (printed_frag_status) { printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"); printed_frag_status = 0; } printf("\b\b\b\b\b\b%05.1f%%", (100.0*offset)/(isosize-skipsectors*2048.0)); fflush(stdout); } } if (!quiet) { printf("\b\b\b\b\b\b\n%05.1f\n", (100.0*offset)/(isosize-skipsectors*2048.0)); } 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); } /* printf("mediasum, computedsum = %s %s\n", mediasum, computedsum); */ if (strcmp(mediasum, computedsum)) return 0; else return 1; }
/* both strings must be pre-allocated at least 33 chars in length */ static int checkmd5sum(int isofd, char *mediasum, char *computedsum, int quiet) { int nread; int i; int appdata_start_offset, appdata_end_offset; int nattempt; int skipsectors; int supported; unsigned int bufsize = 32768; unsigned char md5sum[16]; unsigned int len; unsigned char *buf; long long isosize, offset, pvd_offset, apoff; MD5_CTX md5ctx; if ((pvd_offset = parsepvd(isofd, mediasum, &skipsectors, &isosize, &supported)) < 0) return -1; /* printf("Mediasum = %s\n",mediasum); */ /* rewind, compute md5sum */ lseek(isofd, 0L, SEEK_SET); MD5_Init(&md5ctx); offset = 0; apoff = pvd_offset + APPDATA_OFFSET; buf = malloc(bufsize * sizeof(unsigned char)); if (!quiet) { printf("Percent complete: %05.1f%%", (100.0*offset)/(isosize-skipsectors*2048.0)); fflush(stdout); } while (offset < isosize - skipsectors*2048) { nattempt = MIN(isosize - skipsectors*2048 - offset, bufsize); /* printf("%lld %lld %lld %d\n", offset, isosize, isosize-SKIPSECTORS*2048, nattempt); */ nread = read(isofd, buf, nattempt); if (nread <= 0) break; /* 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); offset = offset + nread; /* XXX: print only once every few 100ms, in order not to overload a serial console. -stefanp */ if (!quiet) { printf("\b\b\b\b\b\b%05.1f%%", (100.0*offset)/(isosize-skipsectors*2048.0)); fflush(stdout); } } if (!quiet) { printf("\b\b\b\b\b\b%05.1f%%\n", (100.0*offset)/(isosize-skipsectors*2048.0)); } 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]); strcat(computedsum, tmpstr); } /* printf("mediasum, computedsum = %s %s\n", mediasum, computedsum); */ if (strcmp(mediasum, computedsum)) return 0; else return 1; }