8 #if defined(HAVE_MACHINE_TYPES_H)
9 # include <machine/types.h>
12 #include <netinet/in.h>
14 #define _RPMIOB_INTERNAL
22 #define _RPMTAG_INTERNAL
28 #define _RPMTS_INTERNAL
58 #define timedRead (ufdio->read)
74 const void * uh = NULL;
80 fprintf(stderr,
"--> rpmWriteHeader(%p, %p, %p)\n", fd, h, msg);
84 *msg =
xstrdup(
_(
"write of NULL header"));
91 *msg =
xstrdup(
_(
"headerUnload failed"));
95 {
unsigned char * hmagic = NULL;
99 nb =
Fwrite(hmagic,
sizeof(hmagic[0]), nmagic, fd);
100 if (nb != nmagic ||
Ferror(fd)) {
103 ?
xstrdup(
_(
"short write of header magic"))
110 nb =
Fwrite(uh,
sizeof(
char), length, fd);
112 if (nb != length ||
Ferror(fd)) {
115 ?
xstrdup(
_(
"short write of header"))
132 if (ts != NULL && (
int)opx >= 0 && (
int)opx <
RPMTS_OP_MAX)
159 HE_t he = memset(
alloca(
sizeof(*he)), 0,
sizeof(*he));
164 const char * pubkeysource = NULL;
170 assert(sigp != NULL);
171 assert(pubp != NULL);
177 fprintf(stderr,
"==> find sig id %08x %08x ts pubkey id %08x %08x\n",
183 if (memcmp(sigp->signid, ts->pksignid,
sizeof(ts->pksignid))) {
185 fprintf(stderr,
"*** free pkt %p[%d] id %08x %08x\n", ts->pkpkt, ts->pkpktlen,
pgpGrab(ts->pksignid, 4),
pgpGrab(ts->pksignid+4, 4));
187 ts->pkpkt =
_free(ts->pkpkt);
189 memset(ts->pksignid, 0,
sizeof(ts->pksignid));
193 if (ts->pkpkt == NULL) {
202 pubkeysource =
xstrdup(
"keyutils");
204 ts->pkpkt = memcpy(
xmalloc(iob->blen), iob->b, iob->blen);
205 ts->pkpktlen = iob->blen;
211 if (ts->pkpkt == NULL) {
212 unsigned hx = 0xffffffff;
213 unsigned ix = 0xffffffff;
218 if (ts->rdb == NULL) {
219 xx =
rpmdbOpen(ts->rootDir, &ts->rdb, ts->dbmode, 0644);
221 const char * dn =
rpmGetPath(ts->rootDir,
"%{_dbpath}", NULL);
223 _(
"cannot open Packages database in %s\n"), dn);
239 if (ix >= (
unsigned) he->
c
240 || b64decode(he->
p.
argv[ix], (
void **) &ts->pkpkt, &ts->pkpktlen))
248 if (ix < 0xffffffff) {
250 sprintf(hnum,
"h#%u", hx);
253 ts->pkpkt =
_free(ts->pkpkt);
259 if (ts->pkpkt == NULL) {
260 const char * fn =
rpmExpand(
"%{_hkp_keyserver_query}",
261 pgpHexStr(sigp->signid,
sizeof(sigp->signid)), NULL);
264 if (fn && *fn !=
'%') {
269 ts->pkpkt =
_free(ts->pkpkt);
273 pubkeysource =
xstrdup(
"keyserver");
279 if (ts->pkpkt == NULL) {
280 const char * fn =
rpmExpand(
"%{_gpg_pubkey}", NULL);
283 if (fn && *fn !=
'%')
287 ts->pkpkt =
_free(ts->pkpkt);
290 pubkeysource =
xstrdup(
"macro");
296 if (ts->pkpkt == NULL || ts->pkpktlen == 0)
303 if (sigp->pubkey_algo == pubp->pubkey_algo
305 && sigp->hash_algo == pubp->hash_algo
307 && !memcmp(sigp->signid, pubp->signid,
sizeof(sigp->signid)) )
316 iob->b = memcpy(iob->b, ts->pkpkt, iob->blen);
322 memcpy(ts->pksignid, pubp->signid,
sizeof(ts->pksignid));
336 pubkeysource =
_free(pubkeysource);
338 ts->pkpkt =
_free(ts->pkpkt);
349 if (ts->dig == NULL) {
400 0xed, 0xab, 0xee, 0xdb, 0x00, 0x00, 0x00, 0x00
420 fprintf(stderr,
"--> wrLead(%p, %p, %p)\n", fd, ptr, msg);
422 memcpy(&l, ptr,
sizeof(l));
425 if ((
int)l.
major == 0)
426 l.
major = (
unsigned char) 3;
430 (void) strncpy(l.
name, *msg,
sizeof(l.
name));
433 l.
type = (
unsigned short) htons(l.
type);
438 if (
Fwrite(&l, 1,
sizeof(l), fd) !=
sizeof(l))
464 fprintf(stderr,
"--> rdLead(%p, %p, %p)\n", fd, ptr, msg);
467 if (leadp != NULL) *leadp = NULL;
470 if ((xx = (
int)
timedRead(fd, (
char *)l,
sizeof(*l))) != (
int)
sizeof(*l)) {
473 _(
"lead size(%u): BAD, read(%d), %s(%d)"),
478 _(
"lead size(%u): BAD, read(%d), %s(%d)"),
479 (unsigned)
sizeof(*l), xx, strerror(
errno),
errno);
487 unsigned char * bh = (
unsigned char *)l;
488 if (bh[0] ==
'x' && bh[1] ==
'a' && bh[2] ==
'r' && bh[3] ==
'!') {
500 unsigned char *b = NULL;
502 const char item[] =
"Lead";
505 _(
"XAR file not found (or no XAR support)"));
510 if (nb !=
sizeof(*l)) {
512 _(
"lead size(%u): BAD, xar read(%u)"),
513 (unsigned)
sizeof(*l), (unsigned)nb);
522 l->
type = (
unsigned short) ntohs(l->
type);
538 _(
"lead version(%u): UNSUPPORTED"), (unsigned) l->
major);
557 if (rc ==
RPMRC_OK && leadp != NULL)
562 if (msg != NULL && buf[0] !=
'\0') {
563 buf[
sizeof(buf)-1] =
'\0';
584 static unsigned char zero[8]
585 = {
'\0',
'\0',
'\0',
'\0',
'\0',
'\0',
'\0',
'\0' };
591 fprintf(stderr,
"--> wrSignature(%p, %p, %p)\n", fd, ptr, msg);
598 pad = (8 - (sigSize % 8)) % 8;
600 if (
Fwrite(zero,
sizeof(zero[0]), pad, fd) != pad)
619 struct stat sb, * st = &sb;
626 if (fdno == 123456789) {
629 st->st_size -= nl + siglen + pad + datalen;
633 if (
Fstat(fd, st) < 0)
636 expected = nl + siglen + pad + datalen;
638 D_(
"Expected size: %12lu = lead(%u)+sigs(%u)+pad(%u)+data(%lu)\n"),
639 (
unsigned long)expected,
640 (
unsigned)nl, (
unsigned) siglen, (
unsigned) pad,
641 (
unsigned long)datalen);
643 D_(
" Actual size: %12lu\n"), (
unsigned long)st->st_size);
661 HE_t he = memset(
alloca(
sizeof(*he)), 0,
sizeof(*he));
674 unsigned char * dataStart;
675 unsigned char * dataEnd = NULL;
683 fprintf(stderr,
"--> rdSignature(%p, %p, %p)\n", fd, ptr, msg);
689 memset(block, 0,
sizeof(block));
691 const char item[] =
"Signature";
694 _(
"XAR file not found (or no XAR support)"));
700 if ((xx = (
int)
timedRead(fd, (
void *)block,
sizeof(block))) != (int)
sizeof(block)) {
702 _(
"sigh size(%d): BAD, read returned %d"), (int)
sizeof(block), xx);
706 {
unsigned char * hmagic = NULL;
711 if (memcmp(block, hmagic, nmagic)) {
712 unsigned char * x = (
unsigned char *)block;
714 (void)
snprintf(buf,
sizeof(buf),
_(
"sigh magic: BAD, read %02x%02x%02x%02x%02x%02x%02x%02x"), x[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7]);
722 _(
"sigh tags: BAD, no. of tags(%u) out of range"), (unsigned) il);
728 _(
"sigh data: BAD, no. of bytes(%u) out of range"), (unsigned) dl);
736 size_t pvlen = (
sizeof(il) +
sizeof(dl) + nb);
737 static const int prot = PROT_READ | PROT_WRITE;
738 static const int flags = MAP_PRIVATE| MAP_ANONYMOUS;
739 static const int fdno = -1;
740 static const off_t off = 0;
742 ei = mmap(NULL, pvlen, prot, flags, fdno, off);
743 if (ei == NULL || ei == (
void *)-1)
745 "==> mmap(%p[%u], 0x%x, 0x%x, %d, 0x%x) error(%d): %s\n",
746 NULL, pvlen, prot, flags, fdno, (
unsigned)off,
749 size_t pvlen = (
sizeof(il) +
sizeof(dl) + nb);
753 if ((xx = (
int)
timedRead(fd, (
void *)&ei[2], nb)) != (
int) nb) {
755 _(
"sigh blob(%u): BAD, read returned %d"), (unsigned) nb, xx);
762 size_t pvlen = (
sizeof(il) +
sizeof(dl) + nb);
763 if (mprotect(ei, pvlen, PROT_READ) != 0)
764 fprintf(stderr,
"==> mprotect(%p[%u],0x%x) error(%d): %s\n",
765 ei, pvlen, PROT_READ,
770 dataStart = (
unsigned char *) (pe + il);
776 _(
"tag[%d]: BAD, tag %u type %u offset %d count %u"),
797 _(
"region offset: BAD, tag %u type %u offset %d count %u"),
811 memcpy(dataEnd, &stag,
sizeof(stag));
822 _(
"region trailer: BAD, tag %u type %u offset %d count %u"),
828 memset(info, 0,
sizeof(*info));
832 if ((entry->
info.
offset %
sizeof(*pe)) || ril > il) {
834 _(
"region size: BAD, ril(%u) > il(%u)"), (unsigned) ril, (
unsigned) il);
840 memset(info, 0,
sizeof(*info));
841 for (i = 1; i < (unsigned) il; i++) {
845 _(
"sigh tag[%u]: BAD, tag %u type %u offset %d count %u"),
855 (void)
snprintf(buf,
sizeof(buf),
_(
"sigh load: BAD"));
866 size_t pad = (8 - (sigSize % 8)) % 8;
869 if (pad && (xx = (
int)
timedRead(fd, (
void *)block, pad)) != (
int) pad)
872 _(
"sigh pad(%u): BAD, read %d bytes"), (unsigned) pad, xx);
881 size_t datasize = he->
p.
ui32p[0];
882 rc =
printSize(fd, sigSize, pad, datasize);
885 _(
"sigh sigSize(%u): BAD, Fstat(2) failed"), (unsigned) sigSize);
893 if (sighp && sigh && rc ==
RPMRC_OK)
899 buf[
sizeof(buf)-1] =
'\0';
931 size_t pvlen =
sizeof(ildl) + (il *
sizeof(*pe)) + dl;
932 unsigned char * dataStart = (
unsigned char *) (pe + il);
935 const void * sig = NULL;
943 unsigned char * regionEnd = NULL;
949 fprintf(stderr,
"--> headerCheck(%p, %p[%u], %p)\n", dig, uh, (
unsigned) uc, msg);
954 if (uc > 0 && pvlen != uc) {
956 _(
"blob size(%d): BAD, 8 + 16 * il(%u) + dl(%u)"),
957 (int)uc, (
unsigned)il, (unsigned)dl);
965 _(
"tag[%d]: BAD, tag %u type %u offset %d count %u"),
985 _(
"region offset: BAD, tag %u type %u offset %d count %u"),
1004 _(
"region trailer: BAD, tag %u type %u offset %d count %u"),
1010 memset(info, 0,
sizeof(*info));
1014 if ((entry->
info.
offset %
sizeof(*pe)) || ril > il) {
1016 _(
"region size: BAD, ril(%u) > il(%u)"), (unsigned) ril, (
unsigned)il);
1021 for (i = ril; i < (unsigned) il; i++) {
1025 _(
"tag[%u]: BAD, tag %u type %u offset %d count %u"),
1026 (unsigned) i, (
unsigned) entry->
info.
tag, (unsigned) entry->
info.
type,
1036 for (b = dataStart + entry->
info.
offset; *b !=
'\0'; b++) {
1037 if (strchr(
"0123456789abcdefABCDEF", *b) == NULL)
1043 (void)
snprintf(buf,
sizeof(buf),
_(
"hdr SHA1: BAD, not hex"));
1046 if (info->
tag == 0) {
1047 *info = entry->
info;
1055 (void)
snprintf(buf,
sizeof(buf),
_(
"hdr RSA: BAD, not binary"));
1058 *info = entry->
info;
1059 siglen = info->
count;
1065 (void)
snprintf(buf,
sizeof(buf),
_(
"hdr DSA: BAD, not binary"));
1068 *info = entry->
info;
1069 siglen = info->
count;
1080 buf[
sizeof(buf)-1] =
'\0';
1083 fprintf(stderr,
"--> headerCheck #1: rc %d \"%s\"\n", rc, (msg ? *msg:
""));
1088 if (info->
tag == 0) {
1092 _(
"tag[%d]: BAD, tag %u type %u offset %d count %u"),
1097 (void)
snprintf(buf,
sizeof(buf),
"Header sanity check: OK");
1100 buf[
sizeof(buf)-1] =
'\0';
1103 fprintf(stderr,
"--> headerCheck #2: rc %d \"%s\"\n", rc, (msg ? *msg:
""));
1108 assert(dig != NULL);
1111 sig = memcpy(
xmalloc(siglen), dataStart + info->
offset, siglen);
1120 switch (info->
tag) {
1128 _(
"skipping header with unverifiable V%u signature\n"),
1129 (
unsigned) dig->signature.version);
1149 b = (
unsigned char *) ildl;
1154 b = (
unsigned char *) pe;
1155 nb = (size_t) (htonl(ildl[0]) *
sizeof(*pe));
1159 b = (
unsigned char *) dataStart;
1160 nb = (size_t) htonl(ildl[1]);
1173 _(
"skipping header with unverifiable V%u signature\n"),
1174 (
unsigned) dig->signature.version);
1195 b = (
unsigned char *) ildl;
1200 b = (
unsigned char *) pe;
1201 nb = (size_t) (htonl(ildl[0]) *
sizeof(*pe));
1205 b = (
unsigned char *) dataStart;
1206 nb = (size_t) htonl(ildl[1]);
1220 buf[
sizeof(buf)-1] =
'\0';
1224 fprintf(stderr,
"--> headerCheck #3: rc %d \"%s\"\n", rc, (msg ? *msg:
""));
1237 assert(ptr != NULL);
1238 memcpy(p, ptr,
sizeof(p));
1239 return (8 + 8 + 16 * ntohl(p[2]) + ntohl(p[3]));
1291 const char * origin = NULL;
1297 fprintf(stderr,
"--> rpmReadHeader(%p, %p, %p)\n", fd, hdrp, msg);
1310 memset(block, 0,
sizeof(block));
1312 const char item[] =
"Header";
1315 _(
"XAR file not found (or no XAR support)"));
1322 if ((xx = (
int)
timedRead(fd, (
char *)block,
sizeof(block))) != (int)
sizeof(block)) {
1328 _(
"hdr size(%u): BAD, read returned %d"), (
unsigned)
sizeof(block), xx);
1335 if (memcmp(block, b, nb)) {
1336 unsigned char * x = (
unsigned char *) block;
1338 (void)
snprintf(buf,
sizeof(buf),
_(
"hdr magic: BAD, read %02x%02x%02x%02x%02x%02x%02x%02x"), x[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7]);
1346 _(
"hdr tags: BAD, no. of tags(%u) out of range"), (unsigned) il);
1353 _(
"hdr data: BAD, no. of bytes(%u) out of range\n"), (unsigned) dl);
1360 uc =
sizeof(il) +
sizeof(dl) + nb;
1362 static const int prot = PROT_READ | PROT_WRITE;
1363 static const int flags = MAP_PRIVATE| MAP_ANONYMOUS;
1364 static const int fdno = -1;
1365 static const off_t off = 0;
1367 ei = mmap(NULL, uc, prot, flags, fdno, off);
1368 if (ei == NULL || ei == (
void *)-1)
1370 "==> mmap(%p[%u], 0x%x, 0x%x, %d, 0x%x) error(%d): %s\n",
1371 NULL, uc, prot, flags, fdno, (
unsigned)off,
1377 if ((xx = (
int)
timedRead(fd, (
char *)&ei[2], nb)) != (int) nb) {
1379 _(
"hdr blob(%u): BAD, read returned %d"), (unsigned)nb, xx);
1386 if (mprotect(ei, uc, PROT_READ) != 0)
1387 fprintf(stderr,
"==> mprotect(%p[%u],0x%x) error(%d): %s\n",
1400 (void)
snprintf(buf,
sizeof(buf),
_(
"hdr load: BAD\n"));
1413 if (origin != NULL) {
1414 const char * lpath = NULL;
1415 int ut =
urlPath(origin, &lpath);
1417 if (lpath && *lpath !=
'/') {
1418 char * rpath =
Realpath(origin, NULL);
1420 rpath =
_free(rpath);
1427 (void)
Fstat(fd, st);
1437 if (ei != NULL && uc > 0) {
1439 if (munmap(ei, uc) != 0)
1440 fprintf(stderr,
"==> munmap(%p[%u]) error(%d): %s\n",
1450 if (msg != NULL && *msg == NULL && buf[0] !=
'\0') {
1451 buf[
sizeof(buf)-1] =
'\0';
1456 fprintf(stderr,
"--> rpmReadHeader: rc %d \"%s\"\n", rc, (msg ? *msg:
""));
1501 if (!strcmp(fn,
"Lead"))
1504 if (!strcmp(fn,
"Signature")) {
1506 len += ((8 - (len % 8)) % 8);
1508 if (!strcmp(fn,
"Header"))
1520 if (!strcmp(fn,
"Header"))
1532 if (!strcmp(fn,
"Lead"))
1533 rc =
rdLead(fd, ptr, msg);
1535 if (!strcmp(fn,
"Signature"))
1538 if (!strcmp(fn,
"Header"))
1550 if (!strcmp(fn,
"Lead"))
1551 rc =
wrLead(fd, ptr, msg);
1553 if (!strcmp(fn,
"Signature"))
1556 if (!strcmp(fn,
"Header"))