1996-02-23 02:50:41 +08:00
|
|
|
/* checksig.c: verify the signature of an RPM */
|
|
|
|
|
1998-07-26 05:00:26 +08:00
|
|
|
#include "system.h"
|
1996-02-23 02:50:41 +08:00
|
|
|
|
1998-07-31 06:09:42 +08:00
|
|
|
#include "build/rpmbuild.h"
|
|
|
|
|
1996-02-26 06:27:43 +08:00
|
|
|
#include "checksig.h"
|
1997-05-01 23:15:11 +08:00
|
|
|
#include "intl.h"
|
1996-02-23 02:50:41 +08:00
|
|
|
#include "rpmlead.h"
|
|
|
|
#include "signature.h"
|
|
|
|
|
1996-07-08 11:25:57 +08:00
|
|
|
int doReSign(int add, char *passPhrase, char **argv)
|
1996-03-29 07:56:49 +08:00
|
|
|
{
|
|
|
|
int fd, ofd, count;
|
|
|
|
struct rpmlead lead;
|
|
|
|
unsigned short sigtype;
|
1997-08-29 03:13:54 +08:00
|
|
|
char *rpm, *sigtarget;
|
1996-03-29 07:56:49 +08:00
|
|
|
char tmprpm[1024];
|
|
|
|
unsigned char buffer[8192];
|
1996-07-08 11:25:57 +08:00
|
|
|
Header sig;
|
1996-03-29 07:56:49 +08:00
|
|
|
|
|
|
|
while (*argv) {
|
|
|
|
rpm = *argv++;
|
1996-09-18 21:28:50 +08:00
|
|
|
printf("%s:\n", rpm);
|
1996-03-29 07:56:49 +08:00
|
|
|
if ((fd = open(rpm, O_RDONLY, 0644)) < 0) {
|
1997-05-01 23:15:11 +08:00
|
|
|
fprintf(stderr, _("%s: Open failed\n"), rpm);
|
1996-03-29 07:56:49 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (readLead(fd, &lead)) {
|
1997-05-01 23:15:11 +08:00
|
|
|
fprintf(stderr, _("%s: readLead failed\n"), rpm);
|
1996-03-29 07:56:49 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (lead.major == 1) {
|
1997-05-01 23:15:11 +08:00
|
|
|
fprintf(stderr, _("%s: Can't sign v1.0 RPM\n"), rpm);
|
1996-03-29 07:56:49 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
1996-07-08 11:25:57 +08:00
|
|
|
if (lead.major == 2) {
|
1997-05-01 23:15:11 +08:00
|
|
|
fprintf(stderr, _("%s: Can't re-sign v2.0 RPM\n"), rpm);
|
1996-07-08 11:25:57 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
1996-11-19 02:02:36 +08:00
|
|
|
if (rpmReadSignature(fd, &sig, lead.signature_type)) {
|
1997-05-01 23:15:11 +08:00
|
|
|
fprintf(stderr, _("%s: rpmReadSignature failed\n"), rpm);
|
1996-03-29 07:56:49 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
1996-07-08 11:25:57 +08:00
|
|
|
if (add != ADD_SIGNATURE) {
|
1996-11-19 02:02:36 +08:00
|
|
|
rpmFreeSignature(sig);
|
1996-03-29 07:56:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Write the rest to a temp file */
|
1997-08-29 03:13:54 +08:00
|
|
|
if (makeTempFile(NULL, &sigtarget, &ofd))
|
|
|
|
exit(1);
|
|
|
|
|
1996-03-29 07:56:49 +08:00
|
|
|
while ((count = read(fd, buffer, sizeof(buffer))) > 0) {
|
|
|
|
if (count == -1) {
|
1997-05-01 23:15:11 +08:00
|
|
|
perror(_("Couldn't read the header/archive"));
|
1996-03-29 07:56:49 +08:00
|
|
|
close(ofd);
|
|
|
|
unlink(sigtarget);
|
1997-08-29 03:13:54 +08:00
|
|
|
free(sigtarget);
|
1996-03-29 07:56:49 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (write(ofd, buffer, count) < 0) {
|
1997-05-01 23:15:11 +08:00
|
|
|
perror(_("Couldn't write header/archive to temp file"));
|
1996-03-29 07:56:49 +08:00
|
|
|
close(ofd);
|
|
|
|
unlink(sigtarget);
|
1997-08-29 03:13:54 +08:00
|
|
|
free(sigtarget);
|
1996-03-29 07:56:49 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
close(ofd);
|
|
|
|
|
|
|
|
/* Start writing the new RPM */
|
|
|
|
sprintf(tmprpm, "%s.tmp", rpm);
|
|
|
|
ofd = open(tmprpm, O_WRONLY|O_CREAT|O_TRUNC, 0644);
|
1996-07-08 11:25:57 +08:00
|
|
|
lead.signature_type = RPMSIG_HEADERSIG;
|
1996-03-29 07:56:49 +08:00
|
|
|
if (writeLead(ofd, &lead)) {
|
|
|
|
perror("writeLead()");
|
|
|
|
close(ofd);
|
|
|
|
unlink(sigtarget);
|
|
|
|
unlink(tmprpm);
|
1997-08-29 03:13:54 +08:00
|
|
|
free(sigtarget);
|
1996-03-29 07:56:49 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Generate the signature */
|
1996-11-19 02:02:36 +08:00
|
|
|
sigtype = rpmLookupSignatureType();
|
|
|
|
rpmMessage(RPMMESS_VERBOSE, "Generating signature: %d\n", sigtype);
|
1996-07-08 11:25:57 +08:00
|
|
|
if (add != ADD_SIGNATURE) {
|
1996-11-19 02:02:36 +08:00
|
|
|
sig = rpmNewSignature();
|
|
|
|
rpmAddSignature(sig, sigtarget, RPMSIGTAG_SIZE, passPhrase);
|
|
|
|
rpmAddSignature(sig, sigtarget, RPMSIGTAG_MD5, passPhrase);
|
1996-07-08 11:25:57 +08:00
|
|
|
}
|
|
|
|
if (sigtype>0) {
|
1996-11-19 02:02:36 +08:00
|
|
|
rpmAddSignature(sig, sigtarget, sigtype, passPhrase);
|
1996-07-08 11:25:57 +08:00
|
|
|
}
|
1996-11-19 02:02:36 +08:00
|
|
|
if (rpmWriteSignature(ofd, sig)) {
|
1996-03-29 07:56:49 +08:00
|
|
|
close(ofd);
|
|
|
|
unlink(sigtarget);
|
|
|
|
unlink(tmprpm);
|
1997-08-29 03:13:54 +08:00
|
|
|
free(sigtarget);
|
1996-11-19 02:02:36 +08:00
|
|
|
rpmFreeSignature(sig);
|
1996-03-29 07:56:49 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
1996-11-19 02:02:36 +08:00
|
|
|
rpmFreeSignature(sig);
|
1996-03-29 07:56:49 +08:00
|
|
|
|
|
|
|
/* Append the header and archive */
|
|
|
|
fd = open(sigtarget, O_RDONLY);
|
|
|
|
while ((count = read(fd, buffer, sizeof(buffer))) > 0) {
|
|
|
|
if (count == -1) {
|
1997-05-01 23:15:11 +08:00
|
|
|
perror(_("Couldn't read sigtarget"));
|
1996-03-29 07:56:49 +08:00
|
|
|
close(ofd);
|
|
|
|
close(fd);
|
|
|
|
unlink(sigtarget);
|
|
|
|
unlink(tmprpm);
|
1997-08-29 03:13:54 +08:00
|
|
|
free(sigtarget);
|
1996-03-29 07:56:49 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (write(ofd, buffer, count) < 0) {
|
1997-05-01 23:15:11 +08:00
|
|
|
perror(_("Couldn't write package"));
|
1996-03-29 07:56:49 +08:00
|
|
|
close(ofd);
|
|
|
|
close(fd);
|
|
|
|
unlink(sigtarget);
|
|
|
|
unlink(tmprpm);
|
1997-08-29 03:13:54 +08:00
|
|
|
free(sigtarget);
|
1996-03-29 07:56:49 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
close(ofd);
|
|
|
|
unlink(sigtarget);
|
1997-08-29 03:13:54 +08:00
|
|
|
free(sigtarget);
|
1996-03-29 07:56:49 +08:00
|
|
|
|
|
|
|
/* Move it in to place */
|
|
|
|
unlink(rpm);
|
|
|
|
rename(tmprpm, rpm);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1997-01-24 04:55:52 +08:00
|
|
|
int doCheckSig(int flags, char **argv)
|
1996-02-23 02:50:41 +08:00
|
|
|
{
|
1996-07-13 03:26:31 +08:00
|
|
|
int fd, ofd, res, res2, res3, missingKeys;
|
1996-02-23 02:50:41 +08:00
|
|
|
struct rpmlead lead;
|
1996-07-08 11:25:57 +08:00
|
|
|
char *rpm;
|
1997-08-29 03:13:54 +08:00
|
|
|
char result[1024], * sigtarget;
|
1996-07-08 11:25:57 +08:00
|
|
|
unsigned char buffer[8192];
|
|
|
|
Header sig;
|
|
|
|
HeaderIterator sigIter;
|
|
|
|
int_32 tag, type, count;
|
|
|
|
void *ptr;
|
1996-07-10 23:42:48 +08:00
|
|
|
|
|
|
|
res = 0;
|
1996-02-23 02:50:41 +08:00
|
|
|
while (*argv) {
|
|
|
|
rpm = *argv++;
|
|
|
|
if ((fd = open(rpm, O_RDONLY, 0644)) < 0) {
|
1997-05-01 23:15:11 +08:00
|
|
|
fprintf(stderr, _("%s: Open failed\n"), rpm);
|
1996-07-08 11:25:57 +08:00
|
|
|
res++;
|
1996-02-23 05:11:22 +08:00
|
|
|
continue;
|
1996-02-23 02:50:41 +08:00
|
|
|
}
|
|
|
|
if (readLead(fd, &lead)) {
|
1997-05-01 23:15:11 +08:00
|
|
|
fprintf(stderr, _("%s: readLead failed\n"), rpm);
|
1996-07-08 11:25:57 +08:00
|
|
|
res++;
|
1996-02-23 05:11:22 +08:00
|
|
|
continue;
|
1996-02-23 02:50:41 +08:00
|
|
|
}
|
1996-02-28 06:50:55 +08:00
|
|
|
if (lead.major == 1) {
|
1997-05-01 23:15:11 +08:00
|
|
|
fprintf(stderr, _("%s: No signature available (v1.0 RPM)\n"), rpm);
|
1996-07-08 11:25:57 +08:00
|
|
|
res++;
|
1996-02-28 06:50:55 +08:00
|
|
|
continue;
|
|
|
|
}
|
1996-11-19 02:02:36 +08:00
|
|
|
if (rpmReadSignature(fd, &sig, lead.signature_type)) {
|
1997-05-01 23:15:11 +08:00
|
|
|
fprintf(stderr, _("%s: rpmReadSignature failed\n"), rpm);
|
1996-07-08 11:25:57 +08:00
|
|
|
res++;
|
1996-02-23 05:11:22 +08:00
|
|
|
continue;
|
1996-02-23 02:50:41 +08:00
|
|
|
}
|
1996-07-08 11:25:57 +08:00
|
|
|
if (! sig) {
|
1997-05-01 23:15:11 +08:00
|
|
|
fprintf(stderr, _("%s: No signature available\n"), rpm);
|
1996-07-08 11:25:57 +08:00
|
|
|
res++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* Write the rest to a temp file */
|
1997-08-29 03:13:54 +08:00
|
|
|
if (makeTempFile(NULL, &sigtarget, &ofd))
|
|
|
|
exit(1);
|
1996-07-08 11:25:57 +08:00
|
|
|
while ((count = read(fd, buffer, sizeof(buffer))) > 0) {
|
|
|
|
if (count == -1) {
|
1997-05-01 23:15:11 +08:00
|
|
|
perror(_("Couldn't read the header/archive"));
|
1996-07-08 11:25:57 +08:00
|
|
|
close(ofd);
|
|
|
|
unlink(sigtarget);
|
1997-08-29 03:13:54 +08:00
|
|
|
free(sigtarget);
|
1996-07-08 11:25:57 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (write(ofd, buffer, count) < 0) {
|
1997-05-01 23:15:11 +08:00
|
|
|
fprintf(stderr, _("Unable to write %s"), sigtarget);
|
1996-09-18 03:14:00 +08:00
|
|
|
perror("");
|
1996-07-08 11:25:57 +08:00
|
|
|
close(ofd);
|
|
|
|
unlink(sigtarget);
|
1997-08-29 03:13:54 +08:00
|
|
|
free(sigtarget);
|
1996-07-08 11:25:57 +08:00
|
|
|
exit(1);
|
1996-02-23 05:11:22 +08:00
|
|
|
}
|
1996-07-08 11:25:57 +08:00
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
close(ofd);
|
|
|
|
|
1996-11-19 02:02:36 +08:00
|
|
|
sigIter = headerInitIterator(sig);
|
1996-07-08 11:25:57 +08:00
|
|
|
res2 = 0;
|
1996-07-13 03:26:31 +08:00
|
|
|
missingKeys = 0;
|
1996-11-19 02:02:36 +08:00
|
|
|
if (rpmIsVerbose()) {
|
1996-07-08 11:25:57 +08:00
|
|
|
sprintf(buffer, "%s:\n", rpm);
|
|
|
|
} else {
|
|
|
|
sprintf(buffer, "%s: ", rpm);
|
|
|
|
}
|
1996-11-19 02:02:36 +08:00
|
|
|
while (headerNextIterator(sigIter, &tag, &type, &ptr, &count)) {
|
1997-01-24 04:55:52 +08:00
|
|
|
if ((tag == RPMSIGTAG_PGP) && !(flags & CHECKSIG_PGP))
|
1996-07-08 11:58:03 +08:00
|
|
|
continue;
|
1997-01-24 04:55:52 +08:00
|
|
|
else if ((tag == RPMSIGTAG_MD5 ||
|
1997-03-29 01:44:38 +08:00
|
|
|
tag == RPMSIGTAG_LEMD5_2 ||
|
|
|
|
tag == RPMSIGTAG_LEMD5_1)
|
1997-01-24 04:55:52 +08:00
|
|
|
&& !(flags & CHECKSIG_MD5))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if ((res3 = rpmVerifySignature(sigtarget, tag, ptr, count,
|
|
|
|
result))) {
|
1996-11-19 02:02:36 +08:00
|
|
|
if (rpmIsVerbose()) {
|
1996-07-08 11:25:57 +08:00
|
|
|
strcat(buffer, result);
|
1996-07-13 03:26:31 +08:00
|
|
|
res2 = 1;
|
1996-06-20 23:55:35 +08:00
|
|
|
} else {
|
1996-07-08 11:25:57 +08:00
|
|
|
switch (tag) {
|
1996-11-19 02:02:36 +08:00
|
|
|
case RPMSIGTAG_SIZE:
|
1996-07-08 11:25:57 +08:00
|
|
|
strcat(buffer, "SIZE ");
|
1996-07-13 03:26:31 +08:00
|
|
|
res2 = 1;
|
1996-07-08 11:25:57 +08:00
|
|
|
break;
|
1996-11-19 02:02:36 +08:00
|
|
|
case RPMSIGTAG_MD5:
|
1997-03-29 01:44:38 +08:00
|
|
|
case RPMSIGTAG_LEMD5_1:
|
|
|
|
case RPMSIGTAG_LEMD5_2:
|
1996-07-08 11:25:57 +08:00
|
|
|
strcat(buffer, "MD5 ");
|
1996-07-13 03:26:31 +08:00
|
|
|
res2 = 1;
|
1996-07-08 11:25:57 +08:00
|
|
|
break;
|
1996-11-19 02:02:36 +08:00
|
|
|
case RPMSIGTAG_PGP:
|
1996-07-13 03:26:31 +08:00
|
|
|
if (res3 == RPMSIG_NOKEY) {
|
|
|
|
/* Do not consedier this a failure */
|
|
|
|
strcat(buffer, "(PGP) ");
|
|
|
|
missingKeys = 1;
|
|
|
|
} else {
|
|
|
|
strcat(buffer, "PGP ");
|
|
|
|
res2 = 1;
|
|
|
|
}
|
1996-07-08 11:25:57 +08:00
|
|
|
break;
|
|
|
|
default:
|
1997-04-15 10:23:52 +08:00
|
|
|
strcat(buffer, "?UnknownSignatureType? ");
|
1996-07-13 03:26:31 +08:00
|
|
|
res2 = 1;
|
1996-07-08 11:25:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
1996-11-19 02:02:36 +08:00
|
|
|
if (rpmIsVerbose()) {
|
1996-07-08 11:25:57 +08:00
|
|
|
strcat(buffer, result);
|
|
|
|
} else {
|
|
|
|
switch (tag) {
|
1996-11-19 02:02:36 +08:00
|
|
|
case RPMSIGTAG_SIZE:
|
1996-07-08 11:25:57 +08:00
|
|
|
strcat(buffer, "size ");
|
|
|
|
break;
|
1996-11-19 02:02:36 +08:00
|
|
|
case RPMSIGTAG_MD5:
|
1997-03-29 01:44:38 +08:00
|
|
|
case RPMSIGTAG_LEMD5_1:
|
|
|
|
case RPMSIGTAG_LEMD5_2:
|
1996-07-08 11:25:57 +08:00
|
|
|
strcat(buffer, "md5 ");
|
|
|
|
break;
|
1996-11-19 02:02:36 +08:00
|
|
|
case RPMSIGTAG_PGP:
|
1996-07-08 11:25:57 +08:00
|
|
|
strcat(buffer, "pgp ");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
strcat(buffer, "??? ");
|
|
|
|
}
|
1996-06-20 23:55:35 +08:00
|
|
|
}
|
|
|
|
}
|
1996-07-08 11:25:57 +08:00
|
|
|
}
|
1996-11-19 02:02:36 +08:00
|
|
|
headerFreeIterator(sigIter);
|
1996-07-08 11:25:57 +08:00
|
|
|
res += res2;
|
1996-07-12 07:42:46 +08:00
|
|
|
unlink(sigtarget);
|
1997-08-29 03:13:54 +08:00
|
|
|
free(sigtarget);
|
1996-07-08 11:25:57 +08:00
|
|
|
|
|
|
|
if (res2) {
|
1996-11-19 02:02:36 +08:00
|
|
|
if (rpmIsVerbose()) {
|
1996-07-08 11:25:57 +08:00
|
|
|
fprintf(stderr, "%s", buffer);
|
|
|
|
} else {
|
1996-07-13 03:26:31 +08:00
|
|
|
fprintf(stderr, "%sNOT OK%s\n", buffer,
|
1997-05-01 23:15:11 +08:00
|
|
|
missingKeys ? _(" (MISSING KEYS)") : "");
|
1996-02-23 05:11:22 +08:00
|
|
|
}
|
1996-07-08 11:25:57 +08:00
|
|
|
} else {
|
1996-11-19 02:02:36 +08:00
|
|
|
if (rpmIsVerbose()) {
|
1996-07-08 11:25:57 +08:00
|
|
|
printf("%s", buffer);
|
1996-02-24 06:28:19 +08:00
|
|
|
} else {
|
1996-07-13 03:26:31 +08:00
|
|
|
printf("%sOK%s\n", buffer,
|
1997-05-01 23:15:11 +08:00
|
|
|
missingKeys ? _(" (MISSING KEYS)") : "");
|
1996-02-24 06:28:19 +08:00
|
|
|
}
|
1996-02-23 02:50:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|