rpm  5.4.14
pkgio.c
Go to the documentation of this file.
1 
6 #include "system.h"
7 
8 #if defined(HAVE_MACHINE_TYPES_H)
9 # include <machine/types.h>
10 #endif
11 
12 #include <netinet/in.h>
13 
14 #define _RPMIOB_INTERNAL
15 #include <rpmiotypes.h>
16 #include <rpmio_internal.h>
17 #include <rpmcb.h>
18 #include <rpmbc.h> /* XXX beecrypt base64 */
19 #include <rpmmacro.h>
20 #define _RPMHKP_INTERNAL
21 #include <rpmhkp.h>
22 #include <rpmku.h>
23 
24 #define _RPMTAG_INTERNAL
25 #include "header_internal.h"
26 
27 #include <rpmdb.h>
28 #include <pkgio.h>
29 
30 #define _RPMTS_INTERNAL
31 #include "rpmts.h"
32 
33 #include <rpmxar.h>
34 
35 #include "signature.h"
36 #include "debug.h"
37 
38 /*@access rpmts @*/
39 /*@access rpmxar @*/
40 /*@access pgpDig @*/
41 /*@access pgpDigParams @*/
42 /*@access Header @*/ /* XXX compared with NULL */
43 /*@access entryInfo @*/
44 /*@access indexEntry @*/
45 /*@access FD_t @*/ /* XXX stealing digests */
46 /*@access FDSTAT_t @*/ /* XXX stealing digests */
47 
48 #ifdef __cplusplus
49 GENfree(struct rpmlead *)
50 GENfree(rpmuint8_t **)
51 GENfree(rpmuint32_t *)
52 #endif /* __cplusplus */
53 
54 /*@unchecked@*/
55 int _pkgio_debug = 0;
56 
59 /*@-exportheader@*/
60 /*@unused@*/ ssize_t timedRead(FD_t fd, /*@out@*/ void * bufptr, size_t length)
61  /*@globals fileSystem @*/
62  /*@modifies fd, *bufptr, fileSystem @*/;
63 #define timedRead (ufdio->read)
64 /*@=exportheader@*/
65 
66 /*===============================================*/
74 static
75 rpmRC rpmWriteHeader(FD_t fd, /*@null@*/ Header h, /*@null@*/ const char ** msg)
76  /*@globals fileSystem, internalState @*/
77  /*@modifies fd, h, *msg, fileSystem, internalState @*/
78 {
79  const void * uh = NULL;
80  size_t nb;
81  size_t length;
82  rpmRC rc = RPMRC_FAIL; /* assume failure */
83 
84 if (_pkgio_debug)
85 fprintf(stderr, "--> rpmWriteHeader(%p, %p, %p)\n", fd, h, msg);
86 
87  if (h == NULL) {
88  if (msg)
89  *msg = xstrdup(_("write of NULL header"));
90  goto exit;
91  }
92 
93  uh = headerUnload(h, &length);
94  if (uh == NULL) {
95  if (msg)
96  *msg = xstrdup(_("headerUnload failed"));
97  goto exit;
98  }
99 
100  { unsigned char * hmagic = NULL;
101  size_t nmagic = 0;
102 
103  (void) headerGetMagic(NULL, &hmagic, &nmagic);
104  nb = Fwrite(hmagic, sizeof(hmagic[0]), nmagic, fd);
105  if (nb != nmagic || Ferror(fd)) {
106  if (msg)
107  *msg = (nb > 0
108  ? xstrdup(_("short write of header magic"))
109  : xstrdup(Fstrerror(fd)) );
110  goto exit;
111  }
112  }
113 
114  /*@-sizeoftype@*/
115  nb = Fwrite(uh, sizeof(char), length, fd);
116  /*@=sizeoftype@*/
117  if (nb != length || Ferror(fd)) {
118  if (msg)
119  *msg = (nb > 0
120  ? xstrdup(_("short write of header"))
121  : xstrdup(Fstrerror(fd)) );
122  goto exit;
123  }
124  rc = RPMRC_OK;
125 
126 exit:
127  uh = _free(uh);
128  return rc;
129 }
130 
131 /*===============================================*/
132 
134 {
135  rpmop op = NULL;
136 
137  if (ts != NULL && (int)opx >= 0 && (int)opx < RPMTS_OP_MAX)
138  op = ts->ops + opx;
139 /*@-usereleased -compdef @*/
140  return op;
141 /*@=usereleased =compdef @*/
142 }
143 
145 {
146 /*@-onlytrans@*/
147  return pgpGetPubkey(rpmtsDig(ts));
148 /*@=onlytrans@*/
149 }
150 
152 {
153  rpmdb rdb = NULL;
154  if (ts != NULL) {
155  rdb = ts->rdb;
156  }
157 /*@-compdef -refcounttrans -usereleased @*/
158  return rdb;
159 /*@=compdef =refcounttrans =usereleased @*/
160 }
161 
162 rpmRC rpmtsFindPubkey(rpmts ts, void * _dig)
163 {
164  HE_t he = (HE_t) memset(alloca(sizeof(*he)), 0, sizeof(*he));
165  pgpDig dig = (pgpDig) (_dig ? _dig : rpmtsDig(ts));
167  pgpDigParams pubp = pgpGetPubkey(dig);
169  const char * pubkeysource = NULL;
170  rpmhkp hkp = NULL;
171  rpmbf awol;
172  rpmiob iob = NULL;
173  int krcache = 1; /* XXX assume pubkeys are cached in keyutils keyring. */
174 int validate = 0;
175  int xx;
176 
177 if (_rpmhkp_debug)
178 fprintf(stderr, "--> %s(%p,%p)\n", __FUNCTION__, ts, _dig);
179 
180 assert(dig != NULL);
181 assert(sigp != NULL);
182 assert(pubp != NULL);
183 /*@-sefparams@*/
184 assert(rpmtsDig(ts) == dig);
185 /*@=sefparams@*/
186  if (ts->hkp == NULL)
187  ts->hkp = rpmhkpNew(NULL, 0);
188  hkp = rpmhkpLink(ts->hkp);
189  awol = rpmbfLink(hkp->awol);
190 
191 #if 0
192 if (_rpmhkp_debug)
193 fprintf(stderr, "==> find sig id %08x %08x ts pubkey id %08x %08x\n",
194 pgpGrab(sigp->signid, 4), pgpGrab(sigp->signid+4, 4),
195 pgpGrab(hkp->signid, 4), pgpGrab(hkp->signid+4, 4));
196 #endif
197 
198  /* Lazy free of previous pubkey if pubkey does not match this signature. */
199  if (memcmp(sigp->signid, hkp->signid, sizeof(hkp->signid))) {
200 #if 0
201 if (_rpmhkp_debug)
202 fprintf(stderr, "*** free pkt %p[%d] id %08x %08x\n", hkp->pkt, hkp->pktlen, pgpGrab(hkp->signid, 4), pgpGrab(hkp->signid+4, 4));
203 #endif
204  hkp->pkt = _free(hkp->pkt);
205  hkp->pktlen = 0;
206  memset(hkp->signid, 0, sizeof(hkp->signid));
207  }
208 
209  /* Has this pubkey failled a previous lookup? */
210  if (hkp->pkt == NULL && awol != NULL
211  && rpmbfChk(awol, sigp->signid, sizeof(sigp->signid)))
212  goto leave;
213 
214  /* Try keyutils keyring lookup. */
215  if (hkp->pkt == NULL) {
216  iob = NULL;
217  switch (rpmkuFindPubkey(sigp, &iob)) {
218  case RPMRC_NOTFOUND:
219  case RPMRC_FAIL:
220  case RPMRC_NOTTRUSTED:
221  case RPMRC_NOKEY:
222  case RPMRC_NOSIG:
223  break;
224  case RPMRC_OK:
225  krcache = 0; /* XXX don't bother caching. */
226  hkp->pkt = (rpmuint8_t *)
227  memcpy(xmalloc(iob->blen), iob->b, iob->blen);
228  hkp->pktlen = iob->blen;
229  pubkeysource = xstrdup("keyutils");
230 validate = 0;
231  break;
232  }
233 if (_rpmhkp_debug)
234 fprintf(stderr, "\t%s: rpmku %p[%u]\n", __FUNCTION__, hkp->pkt, (unsigned) hkp->pktlen);
235  }
236 
237  /* Try rpmdb keyring lookup. */
238  if (hkp->pkt == NULL) {
239  unsigned hx = 0xffffffff;
240  unsigned ix = 0xffffffff;
241  rpmmi mi;
242  Header h;
243 
244  /* XXX Do a lazy open if not done already. */
245  if (ts->rdb == NULL) {
246  xx = rpmdbOpen(ts->rootDir, &ts->rdb, ts->dbmode, (mode_t)0644);
247  if (xx) {
248  const char * dn = rpmGetPath(ts->rootDir, "%{_dbpath}", NULL);
250  _("cannot open Packages database in %s\n"), dn);
251  dn = _free(dn);
252  }
253  }
254 
255  /* Retrieve the pubkey that matches the signature. */
256  he->tag = RPMTAG_PUBKEYS;
257 /*@-nullstate@*/
258  mi = rpmmiInit(rpmtsGetRdb(ts), RPMTAG_PUBKEYS, sigp->signid, sizeof(sigp->signid));
259 /*@=nullstate@*/
260  while ((h = rpmmiNext(mi)) != NULL) {
261  if (!headerGet(h, he, 0))
262  continue;
263  hx = rpmmiInstance(mi);
264  switch (he->t) {
265  default:
266  ix = 0xffffffff;
267  break;
269  ix = he->c - 1; /* XXX FIXME: assumes last pubkey */
270  if (b64decode(he->p.argv[ix], (void **)&hkp->pkt, &hkp->pktlen))
271  ix = 0xffffffff;
272  break;
273  }
274  he->p.ptr = _free(he->p.ptr);
275  break;
276  }
277  mi = rpmmiFree(mi);
278 
279  if (ix < 0xffffffff) {
280  char hnum[64];
281  sprintf(hnum, "h#%u[%u]", hx, ix);
282  pubkeysource = xstrdup(hnum);
283 validate = -1; /* XXX rpmhkpValidate is prerequisite for rpmhkpFindKey */
284  } else {
285  hkp->pkt = _free(hkp->pkt);
286  hkp->pktlen = 0;
287  }
288 if (_rpmhkp_debug)
289 fprintf(stderr, "\t%s: rpmdb %p[%u]\n", __FUNCTION__, hkp->pkt, (unsigned) hkp->pktlen);
290  }
291 
292  /* Try autosign package pubkey (if present). */
293  if (hkp->pkt == NULL && dig->pub && dig->publen > 0) {
294  uint8_t keyid[8];
295 
296  xx = pgpPubkeyFingerprint((const rpmuint8_t *)dig->pub, dig->publen, keyid);
297  if (!memcmp(sigp->signid, keyid, sizeof(keyid))) {
298  hkp->pkt = (uint8_t *) dig->pub; dig->pub = NULL;
299  hkp->pktlen = dig->publen; dig->publen = 0;
300  pubkeysource = xstrdup("package");
301 validate = -1; /* XXX rpmhkpValidate is prerequisite for rpmhkpFindKey */
302 if (_rpmhkp_debug)
303 fprintf(stderr, "\t%s: auto %p[%u]\n", __FUNCTION__, hkp->pkt, (unsigned) hkp->pktlen);
304  }
305  }
306 
307  /* Try keyserver lookup. */
308  if (hkp->pkt == NULL) {
309  const char * fn = rpmExpand("%{_hkp_keyserver_query}", "0x",
310  pgpHexStr(sigp->signid, sizeof(sigp->signid)), NULL);
311 
312  xx = (fn && *fn != '%')
313  ? (pgpReadPkts(fn, &hkp->pkt, &hkp->pktlen) != PGPARMOR_PUBKEY)
314  : 1; /* XXX assume failure */
315  fn = _free(fn);
316  if (xx) {
317  hkp->pkt = _free(hkp->pkt);
318  hkp->pktlen = 0;
319  } else {
320  /* Save new pubkey in local ts keyring for delayed import. */
321  pubkeysource = xstrdup("keyserver");
322 validate = 1;
323  }
324 if (_rpmhkp_debug)
325 fprintf(stderr, "\t%s: rpmhkp %p[%u]\n", __FUNCTION__, hkp->pkt, (unsigned) hkp->pktlen);
326  }
327 
328 #ifdef NOTYET
329  /* Try filename from macro lookup. */
330  if (hkp->pkt == NULL) {
331  const char * fn = rpmExpand("%{_gpg_pubkey}", NULL);
332 
333  xx = 0;
334  if (fn && *fn != '%')
335  xx = (pgpReadPkts(fn, &hkp->pkt, &hkp->pktlen) != PGPARMOR_PUBKEY);
336  fn = _free(fn);
337  if (xx) {
338  hkp->pkt = _free(hkp->pkt);
339  hkp->pktlen = 0;
340  } else {
341  pubkeysource = xstrdup("macro");
342  }
343  }
344 #endif
345 
346  /* Was a matching pubkey found? */
347  if (hkp->pkt == NULL || hkp->pktlen == 0)
348  goto exit;
349 if (_rpmhkp_debug)
350 fprintf(stderr, "\t%s: match %p[%u]\n", __FUNCTION__, hkp->pkt, (unsigned) hkp->pktlen);
351 
352  /* Split the result into packet array. */
353 hkp->pkts = _free(hkp->pkts); /* XXX memleaks */
354 hkp->npkts = 0;
355  xx = pgpGrabPkts(hkp->pkt, hkp->pktlen, &hkp->pkts, &hkp->npkts);
356 
357  if (!xx)
358  xx = pgpPubkeyFingerprint(hkp->pkt, hkp->pktlen, hkp->keyid);
359  memcpy(pubp->signid, hkp->keyid, sizeof(pubp->signid)); /* XXX useless */
360 
361  /* Validate pubkey self-signatures. */
362  if (validate) {
363  rpmRC rc = rpmhkpValidate(hkp, NULL);
364  switch (rc) {
365  case RPMRC_OK:
366  break;
367  case RPMRC_NOKEY:
368  if (validate < 0) /* XXX ignore NOKEY w rpmdb pubkey. */
369  break;
370  /*@fallthrough@*/
371  case RPMRC_NOTFOUND:
372  case RPMRC_FAIL: /* XXX remap to NOTFOUND? */
373  case RPMRC_NOTTRUSTED:
374  default:
375 if (_rpmhkp_debug)
376 fprintf(stderr, "*** rpmhkpValidate: validate %d rc %d\n", validate, rc);
377  res = rc;
378  goto exit;
379  }
380  }
381 
382  /* Retrieve parameters from pubkey/subkey packet(s). */
383  xx = rpmhkpFindKey(hkp, dig, sigp->signid, sigp->pubkey_algo);
384 
385 #ifdef DYING
386 if (_rpmhkp_debug)
387 _rpmhkpDumpDig(__FUNCTION__, dig);
388 #endif
389 
390  /* Do the parameters match the signature? */
391  if (sigp->pubkey_algo == pubp->pubkey_algo
392  && !memcmp(sigp->signid, pubp->signid, sizeof(sigp->signid)) )
393  {
394 
395  /* Save the pubkey in the keyutils keyring. */
396  if (krcache) {
397  if (iob == NULL) {
398  iob = rpmiobNew(hkp->pktlen);
399  iob->b = (rpmuint8_t *)memcpy(iob->b, hkp->pkt, iob->blen);
400  }
401  (void) rpmkuStorePubkey(sigp, iob);
402 if (_rpmhkp_debug)
403 fprintf(stderr, "\t%s: rpmku %p[%u]\n", __FUNCTION__, hkp->pkt, (unsigned) hkp->pktlen);
404  }
405 
406  /* Pubkey packet looks good, save the signer id. */
407  memcpy(hkp->signid, pubp->signid, sizeof(hkp->signid));
408 
409  if (pubkeysource)
410  rpmlog(RPMLOG_DEBUG, "========== %s pubkey id %08x %08x (%s)\n",
411  (sigp->pubkey_algo == (rpmuint8_t)PGPPUBKEYALGO_DSA ? "DSA" :
412  (sigp->pubkey_algo == (rpmuint8_t)PGPPUBKEYALGO_RSA ? "RSA" :
413  "???")),
414  pgpGrab(sigp->signid, 4), pgpGrab(sigp->signid+4, 4),
415  pubkeysource);
416 
417  res = RPMRC_OK;
418 
419  }
420 
421 exit:
422  pubkeysource = _free(pubkeysource);
423  if (res != RPMRC_OK) {
424  hkp->pkt = _free(hkp->pkt);
425  hkp->pktlen = 0;
426  if (awol)
427  xx = rpmbfAdd(awol, sigp->signid, sizeof(sigp->signid));
428  }
429 
430 leave:
431  (void) rpmbfFree(awol);
432  (void) rpmhkpFree(hkp);
433 
434 if (_rpmhkp_debug)
435 fprintf(stderr, "<-- %s(%p,%p) res %d\n", __FUNCTION__, ts, _dig, res);
436 
437 /*@-nullstate@*/
438  return res;
439 /*@=nullstate@*/
440 }
441 
443 {
444 /*@-mods@*/ /* FIX: hide lazy malloc for now */
445  if (ts->dig == NULL) {
446  ts->dig = pgpDigNew(RPMVSF_DEFAULT, (pgpPubkeyAlgo)0);
447 /*@-refcounttrans@*/
448  (void) pgpSetFindPubkey(ts->dig, (int (*)(void *, void *))rpmtsFindPubkey, ts);
449 /*@=refcounttrans@*/
450  }
451 /*@=mods@*/
452 /*@-compdef -retexpose -usereleased@*/
453  return ts->dig;
454 /*@=compdef =retexpose =usereleased@*/
455 }
456 
458 {
459  if (ts && ts->dig) {
460  rpmtsOpX opx;
461  opx = RPMTS_OP_DIGEST;
462  (void) rpmswAdd(rpmtsOp(ts, opx), (rpmop)pgpStatsAccumulator(ts->dig, opx));
463  opx = RPMTS_OP_SIGNATURE;
464  (void) rpmswAdd(rpmtsOp(ts, opx), (rpmop)pgpStatsAccumulator(ts->dig, opx));
465 /*@-onlytrans@*/
466  (void) pgpDigFree(ts->dig);
467  ts->dig = NULL; /* XXX make sure the ptr is __REALLY__ gone */
468 /*@=onlytrans@*/
469  }
470 }
471 
472 /*===============================================*/
473 
480 struct rpmlead {
481  unsigned char magic[4];
482  unsigned char major;
483  unsigned char minor;
484  unsigned short type;
485  unsigned short archnum;
486  char name[66];
487  unsigned short osnum;
488  unsigned short signature_type;
489 /*@unused@*/
490  char reserved[16];
491 } ;
492 
493 /*@-type@*/
494 /*@unchecked@*/ /*@observer@*/
495 static unsigned char lead_magic[] = {
496  0xed, 0xab, 0xee, 0xdb, 0x00, 0x00, 0x00, 0x00
497 };
498 /*@=type@*/
499 
500 /* The lead needs to be 8 byte aligned */
501 
509 static rpmRC wrLead(FD_t fd, const void * ptr, /*@null@*/ const char ** msg)
510  /*@globals fileSystem @*/
511  /*@modifies fd, fileSystem @*/
512 {
513  struct rpmlead l;
514 
515 if (_pkgio_debug)
516 fprintf(stderr, "--> wrLead(%p, %p, %p)\n", fd, ptr, msg);
517 
518  memcpy(&l, ptr, sizeof(l));
519 
520  /* Set some sane defaults */
521  if ((int)l.major == 0)
522  l.major = (unsigned char) 3;
523  if (l.signature_type == 0)
524  l.signature_type = 5; /* RPMSIGTYPE_HEADERSIG */
525  if (msg && *msg)
526  (void) strncpy(l.name, *msg, sizeof(l.name));
527 
528  memcpy(&l.magic, lead_magic, sizeof(l.magic));
529  l.type = (unsigned short) htons(l.type);
530  l.archnum = (unsigned short) htons(l.archnum);
531  l.osnum = (unsigned short) htons(l.osnum);
532  l.signature_type = (unsigned short) htons(l.signature_type);
533 
534  if (Fwrite(&l, 1, sizeof(l), fd) != sizeof(l))
535  return RPMRC_FAIL;
536 
537  return RPMRC_OK;
538 }
539 
547 static rpmRC rdLead(FD_t fd, /*@out@*/ /*@null@*/ void * ptr,
548  /*@null@*/ const char ** msg)
549  /*@globals fileSystem @*/
550  /*@modifies fd, *ptr, *msg, fileSystem @*/
551 {
552  rpmxar xar = fdGetXAR(fd);
553  struct rpmlead ** leadp = (struct rpmlead **) ptr;
554  struct rpmlead * l = (struct rpmlead *) xcalloc(1, sizeof(*l));
555  char buf[BUFSIZ];
556  rpmRC rc = RPMRC_FAIL; /* assume failure */
557  int xx;
558 
559 if (_pkgio_debug)
560 fprintf(stderr, "--> rdLead(%p, %p, %p)\n", fd, ptr, msg);
561 
562  buf[0] = '\0';
563  if (leadp != NULL) *leadp = NULL;
564 
565  /* Read the first 96 bytes of the file. */
566  if ((xx = (int) timedRead(fd, (char *)l, sizeof(*l))) != (int) sizeof(*l)) {
567  if (Ferror(fd)) {
568  (void) snprintf(buf, sizeof(buf),
569  _("lead size(%u): BAD, read(%d), %s(%d)"),
570  (unsigned)sizeof(*l), xx, Fstrerror(fd), errno);
571  rc = RPMRC_FAIL;
572  } else {
573  (void) snprintf(buf, sizeof(buf),
574  _("lead size(%u): BAD, read(%d), %s(%d)"),
575  (unsigned)sizeof(*l), xx, strerror(errno), errno);
576  rc = RPMRC_NOTFOUND;
577  }
578  goto exit;
579  }
580 
581  /* Attach rpmxar handler to fd if this is a xar archive. */
582  if (xar == NULL) {
583  unsigned char * bh = (unsigned char *)l;
584  if (bh[0] == 'x' && bh[1] == 'a' && bh[2] == 'r' && bh[3] == '!') {
585  const char * fn = fdGetOPath(fd);
586 assert(fn != NULL);
587  xar = rpmxarNew(fn, "r");
588  fdSetXAR(fd, xar);
589  (void) rpmxarFree(xar, "rdLead");
590  }
591  }
592 
593  /* With XAR, read lead from a xar archive file called "Lead". */
594  xar = fdGetXAR(fd);
595  if (xar != NULL) {
596  unsigned char *b = NULL;
597  size_t nb = 0;
598  const char item[] = "Lead";
599  if ((xx = rpmxarNext(xar)) != 0 || (xx = rpmxarPull(xar, item)) != 0) {
600  (void) snprintf(buf, sizeof(buf),
601  _("XAR file not found (or no XAR support)"));
602  rc = RPMRC_NOTFOUND;
603  goto exit;
604  }
605  (void) rpmxarSwapBuf(xar, NULL, 0, &b, &nb);
606  if (nb != sizeof(*l)) {
607  (void) snprintf(buf, sizeof(buf),
608  _("lead size(%u): BAD, xar read(%u)"),
609  (unsigned)sizeof(*l), (unsigned)nb);
610  b = _free(b);
611  rc = RPMRC_FAIL;
612  goto exit;
613  }
614  memcpy(l, b, nb);
615  b = _free(b);
616  }
617 
618  l->type = (unsigned short) ntohs(l->type);
619  l->archnum = (unsigned short) ntohs(l->archnum);
620  l->osnum = (unsigned short) ntohs(l->osnum);
621  l->signature_type = (unsigned short) ntohs(l->signature_type);
622 
623  if (memcmp(l->magic, lead_magic, sizeof(l->magic))) {
624 /*@+charint@*/
625  (void) snprintf(buf, sizeof(buf), _("lead magic: BAD, read %02x%02x%02x%02x"), l->magic[0], l->magic[1], l->magic[2], l->magic[3]);
626 /*@=charint@*/
627  rc = RPMRC_NOTFOUND;
628  goto exit;
629  }
630 
631  switch (l->major) {
632  default:
633  (void) snprintf(buf, sizeof(buf),
634  _("lead version(%u): UNSUPPORTED"), (unsigned) l->major);
635  rc = RPMRC_NOTFOUND;
636  goto exit;
637  /*@notreached@*/ break;
638  case 3:
639  case 4:
640  break;
641  }
642 
643  if (l->signature_type != 5) { /* RPMSIGTYPE_HEADERSIG */
644  (void) snprintf(buf, sizeof(buf),
645  _("sigh type(%u): UNSUPPORTED"), (unsigned) l->signature_type);
646  rc = RPMRC_NOTFOUND;
647  goto exit;
648  }
649 
650  rc = RPMRC_OK;
651 
652 exit:
653  if (rc == RPMRC_OK && leadp != NULL)
654  *leadp = l;
655  else
656  /*@-dependenttrans@*/ l = _free(l); /*@=dependenttrans@*/
657 
658  if (msg != NULL && buf[0] != '\0') {
659  buf[sizeof(buf)-1] = '\0';
660  *msg = xstrdup(buf);
661  }
662  return rc;
663 }
664 
665 /*===============================================*/
666 
674 static rpmRC wrSignature(FD_t fd, void * ptr,
675  /*@unused@*/ /*@null@*/ const char ** msg)
676  /*@globals fileSystem, internalState @*/
677  /*@modifies fd, ptr, *msg, fileSystem, internalState @*/
678 {
679  Header sigh = (Header) ptr;
680  static unsigned char zero[8]
681  = { '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0' };
682  size_t sigSize;
683  size_t pad;
684  rpmRC rc = RPMRC_OK;
685 
686 if (_pkgio_debug)
687 fprintf(stderr, "--> wrSignature(%p, %p, %p)\n", fd, ptr, msg);
688 
689  rc = rpmWriteHeader(fd, sigh, msg);
690  if (rc != RPMRC_OK)
691  return rc;
692 
693  sigSize = headerSizeof(sigh);
694  pad = (8 - (sigSize % 8)) % 8;
695  if (pad) {
696  if (Fwrite(zero, sizeof(zero[0]), pad, fd) != pad)
697  rc = RPMRC_FAIL;
698  }
699  rpmlog(RPMLOG_DEBUG, D_("Signature: size(%u)+pad(%u)\n"), (unsigned)sigSize, (unsigned)pad);
700  return rc;
701 }
702 
711 static inline rpmRC printSize(FD_t fd, size_t siglen, size_t pad, size_t datalen)
712  /*@globals fileSystem, internalState @*/
713  /*@modifies fileSystem, internalState @*/
714 {
715  struct stat sb, * st = &sb;
716  size_t expected;
717  size_t nl = rpmpkgSizeof("Lead", NULL);
718 
719 #ifndef DYING /* XXX Fstat(2) contentLength not gud enuf yet. */
720  int fdno = Fileno(fd);
721  /* HACK: workaround for davRead wiring. */
722  if (fdno == 123456789) {
723 /*@-type@*/
724  st->st_size = 0;
725  st->st_size -= nl + siglen + pad + datalen;
726 /*@=type@*/
727  } else
728 #endif
729  if (Fstat(fd, st) < 0)
730  return RPMRC_FAIL;
731 
732  expected = nl + siglen + pad + datalen;
734  D_("Expected size: %12lu = lead(%u)+sigs(%u)+pad(%u)+data(%lu)\n"),
735  (unsigned long)expected,
736  (unsigned)nl, (unsigned) siglen, (unsigned) pad,
737  (unsigned long)datalen);
739  D_(" Actual size: %12lu\n"), (unsigned long)st->st_size);
740 
741  return RPMRC_OK;
742 }
743 
751 static rpmRC rdSignature(FD_t fd, /*@out@*/ /*@null@*/ void * ptr,
752  /*@null@*/ const char ** msg)
753  /*@globals fileSystem, internalState @*/
754  /*@modifies *ptr, *msg, fileSystem, internalState @*/
755 {
756 rpmxar xar = fdGetXAR(fd);
757  HE_t he = (HE_t) memset(alloca(sizeof(*he)), 0, sizeof(*he));
758  Header * sighp = (Header *) ptr;
759  char buf[BUFSIZ];
760  rpmuint32_t block[4];
761  rpmuint32_t il;
762  rpmuint32_t dl;
763  rpmuint32_t * ei = NULL;
764  entryInfo pe;
765  size_t startoff;
766  size_t nb;
767  rpmuint32_t ril = 0;
768  indexEntry entry = (indexEntry)
769  memset(alloca(sizeof(*entry)), 0, sizeof(*entry));
770  entryInfo info = (entryInfo)
771  memset(alloca(sizeof(*info)), 0, sizeof(*info));
772  unsigned char * dataStart;
773  unsigned char * dataEnd = NULL;
774  Header sigh = NULL;
775  rpmRC rc = RPMRC_FAIL; /* assume failure */
776  int xx;
777  rpmuint32_t i;
778  static int map = 1;
779 
780 if (_pkgio_debug)
781 fprintf(stderr, "--> rdSignature(%p, %p, %p)\n", fd, ptr, msg);
782 
783  buf[0] = '\0';
784  if (sighp)
785  *sighp = NULL;
786 
787  memset(block, 0, sizeof(block));
788  if (xar != NULL) {
789  const char item[] = "Signature";
790  if ((xx = rpmxarNext(xar)) != 0 || (xx = rpmxarPull(xar, item)) != 0) {
791  (void) snprintf(buf, sizeof(buf),
792  _("XAR file not found (or no XAR support)"));
793  rc = RPMRC_NOTFOUND;
794  goto exit;
795  }
796  }
797  startoff = fd->stats->ops[FDSTAT_READ].bytes;
798  if ((xx = (int) timedRead(fd, (char *)block, sizeof(block))) != (int) sizeof(block)) {
799  (void) snprintf(buf, sizeof(buf),
800  _("sigh size(%d): BAD, read returned %d"), (int)sizeof(block), xx);
801  goto exit;
802  }
803 
804  { unsigned char * hmagic = NULL;
805  size_t nmagic = 0;
806 
807  (void) headerGetMagic(NULL, &hmagic, &nmagic);
808 
809  if (memcmp(block, hmagic, nmagic)) {
810  unsigned char * x = (unsigned char *)block;
811 /*@+charint@*/
812  (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]);
813 /*@=charint@*/
814  goto exit;
815  }
816  }
817  il = (rpmuint32_t) ntohl(block[2]);
818  if (il > 32) {
819  (void) snprintf(buf, sizeof(buf),
820  _("sigh tags: BAD, no. of tags(%u) out of range"), (unsigned) il);
821  goto exit;
822  }
823  dl = (rpmuint32_t) ntohl(block[3]);
824  if (dl > 8192) {
825  (void) snprintf(buf, sizeof(buf),
826  _("sigh data: BAD, no. of bytes(%u) out of range"), (unsigned) dl);
827  goto exit;
828  }
829 
830 /*@-sizeoftype@*/
831  nb = (il * sizeof(struct entryInfo_s)) + dl;
832 /*@=sizeoftype@*/
833  if (map) {
834  size_t pvlen = (sizeof(il) + sizeof(dl) + nb);
835  static const int prot = PROT_READ | PROT_WRITE;
836  static const int flags = MAP_PRIVATE| MAP_ANONYMOUS;
837  static const int fdno = -1;
838  static const off_t off = 0;
839 
840  ei = (rpmuint32_t *) mmap(NULL, pvlen, prot, flags, fdno, off);
841  if (ei == NULL || ei == (void *)-1)
842  fprintf(stderr,
843  "==> mmap(%p[%u], 0x%x, 0x%x, %d, 0x%x) error(%d): %s\n",
844  NULL, (unsigned)pvlen, prot, flags, fdno, (unsigned)off,
845  errno, strerror(errno));
846  } else {
847  size_t pvlen = (sizeof(il) + sizeof(dl) + nb);
848  ei = (rpmuint32_t *) xmalloc(pvlen);
849  }
850 
851  if ((xx = (int) timedRead(fd, (char *)&ei[2], nb)) != (int) nb) {
852  (void) snprintf(buf, sizeof(buf),
853  _("sigh blob(%u): BAD, read returned %d"), (unsigned) nb, xx);
854  goto exit;
855  }
856  ei[0] = block[2];
857  ei[1] = block[3];
858 
859  if (map) {
860  size_t pvlen = (sizeof(il) + sizeof(dl) + nb);
861  if (mprotect(ei, pvlen, PROT_READ) != 0)
862  fprintf(stderr, "==> mprotect(%p[%u],0x%x) error(%d): %s\n",
863  ei, (unsigned)pvlen, PROT_READ,
864  errno, strerror(errno));
865  }
866 
867  pe = (entryInfo) &ei[2];
868  dataStart = (unsigned char *) (pe + il);
869 
870  /* Check (and convert) the 1st tag element. */
871  xx = headerVerifyInfo(1, dl, pe, &entry->info, 0);
872  if (xx != -1) {
873  (void) snprintf(buf, sizeof(buf),
874  _("tag[%d]: BAD, tag %u type %u offset %d count %u"),
875  0, (unsigned) entry->info.tag, (unsigned) entry->info.type,
876  (int)entry->info.offset, (unsigned) entry->info.count);
877  goto exit;
878  }
879 
880  /* Is there an immutable header region tag? */
881 /*@-sizeoftype@*/
882  if (entry->info.tag == RPMTAG_HEADERSIGNATURES)
883  {
884  /* Is the region tag sane? */
885  if (!(entry->info.type == REGION_TAG_TYPE
886  && entry->info.count == (rpmTagCount)REGION_TAG_COUNT))
887  {
888  (void) snprintf(buf, sizeof(buf),
889  _("region tag: BAD, tag %u type %u offset %d count %u"),
890  (unsigned) entry->info.tag, (unsigned) entry->info.type,
891  (int)entry->info.offset, (unsigned) entry->info.count);
892  goto exit;
893  }
894 /*@=sizeoftype@*/
895 
896 /*
897  * XXX http://mysql.mirrors.pair.com/Downloads/MySQL-5.0/MySQL-client-community-5.0.51a-0.rhel4.i386.rpm
898  * built by rpm-4.3.3 (from REL4) has entry->info.offset == 0.
899  */
900 assert(entry->info.offset >= 0); /* XXX insurance */
901  if (entry->info.offset >= (rpmint32_t)dl) {
902  (void) snprintf(buf, sizeof(buf),
903  _("region offset: BAD, tag %u type %u offset %d count %u"),
904  (unsigned) entry->info.tag, (unsigned) entry->info.type,
905  (int)entry->info.offset, (unsigned) entry->info.count);
906  goto exit;
907  }
908 
909  /* Is there an immutable header region tag trailer? */
910  dataEnd = dataStart + entry->info.offset;
911 /*@-sizeoftype@*/
912  (void) memcpy(info, dataEnd, REGION_TAG_COUNT);
913  /* XXX Really old packages have HEADER_IMAGE, not HEADER_SIGNATURES. */
914  if (info->tag == (rpmuint32_t) htonl(RPMTAG_HEADERIMAGE)) {
916  info->tag = (rpmTag) stag;
917  memcpy(dataEnd, &stag, sizeof(stag));
918  }
919  dataEnd += REGION_TAG_COUNT;
920 
921  xx = headerVerifyInfo(1, il * sizeof(*pe), info, &entry->info, 1);
922  if (xx != -1 ||
923  !(entry->info.tag == RPMTAG_HEADERSIGNATURES
924  && entry->info.type == REGION_TAG_TYPE
925  && entry->info.count == (rpmTagCount)REGION_TAG_COUNT))
926  {
927  (void) snprintf(buf, sizeof(buf),
928  _("region trailer: BAD, tag %u type %u offset %d count %u"),
929  (unsigned) entry->info.tag, (unsigned) entry->info.type,
930  (int)entry->info.offset, (unsigned) entry->info.count);
931  goto exit;
932  }
933 /*@=sizeoftype@*/
934  memset(info, 0, sizeof(*info));
935 
936  /* Is the no. of tags in the region less than the total no. of tags? */
937  ril = (rpmuint32_t) (entry->info.offset/sizeof(*pe));
938  if ((entry->info.offset % sizeof(*pe)) || ril > il) {
939  (void) snprintf(buf, sizeof(buf),
940  _("region size: BAD, ril(%u) > il(%u)"), (unsigned) ril, (unsigned) il);
941  goto exit;
942  }
943  }
944 
945  /* Sanity check signature tags */
946  memset(info, 0, sizeof(*info));
947  for (i = 1; i < (unsigned) il; i++) {
948  xx = headerVerifyInfo(1, dl, pe+i, &entry->info, 0);
949  if (xx != -1) {
950  (void) snprintf(buf, sizeof(buf),
951  _("sigh tag[%u]: BAD, tag %u type %u offset %d count %u"),
952  (unsigned) i, (unsigned) entry->info.tag, (unsigned) entry->info.type,
953  (int)entry->info.offset, (unsigned) entry->info.count);
954  goto exit;
955  }
956  }
957 
958  /* OK, blob looks sane, load the header. */
959  sigh = headerLoad(ei);
960  if (sigh == NULL) {
961  (void) snprintf(buf, sizeof(buf), _("sigh load: BAD"));
962  goto exit;
963  }
964  if (map) {
965  sigh->flags |= HEADERFLAG_MAPPED;
966  sigh->flags |= HEADERFLAG_RDONLY;
967  } else
968  sigh->flags |= HEADERFLAG_ALLOCATED;
969  sigh->flags |= HEADERFLAG_SIGNATURE;
970 
971  { size_t sigSize = headerSizeof(sigh);
972  size_t pad = (8 - (sigSize % 8)) % 8; /* 8-byte pad */
973 
974  /* Position at beginning of header. */
975  if (pad && (xx = (int) timedRead(fd, (char *)block, pad)) != (int) pad)
976  {
977  (void) snprintf(buf, sizeof(buf),
978  _("sigh pad(%u): BAD, read %d bytes"), (unsigned) pad, xx);
979  goto exit;
980  }
981 
982  /* Print package component sizes. */
983 
984  he->tag = (rpmTag) RPMSIGTAG_SIZE;
985  xx = headerGet(sigh, he, 0);
986  if (xx) {
987  size_t datasize = he->p.ui32p[0];
988  rc = printSize(fd, sigSize, pad, datasize);
989  if (rc != RPMRC_OK)
990  (void) snprintf(buf, sizeof(buf),
991  _("sigh sigSize(%u): BAD, Fstat(2) failed"), (unsigned) sigSize);
992  }
993  he->p.ptr = _free(he->p.ptr);
994  }
995  (void) headerSetStartOff(sigh, (rpmuint32_t)startoff);
996  (void) headerSetEndOff(sigh, fd->stats->ops[FDSTAT_READ].bytes);
997 
998 exit:
999  if (sighp && sigh && rc == RPMRC_OK)
1000  *sighp = headerLink(sigh);
1001  (void)headerFree(sigh);
1002  sigh = NULL;
1003 
1004  if (msg != NULL) {
1005  buf[sizeof(buf)-1] = '\0';
1006  *msg = xstrdup(buf);
1007  }
1008 
1009  return rc;
1010 }
1011 
1012 /*===============================================*/
1013 
1027 rpmRC headerCheck(pgpDig dig, const void * uh, size_t uc, const char ** msg)
1028 {
1029  char buf[8*BUFSIZ];
1030  rpmuint32_t * ei = (rpmuint32_t *) uh;
1031  rpmuint32_t il = (rpmuint32_t) ntohl(ei[0]);
1032  rpmuint32_t dl = (rpmuint32_t) ntohl(ei[1]);
1033 /*@-castexpose@*/
1034  entryInfo pe = (entryInfo) &ei[2];
1035 /*@=castexpose@*/
1036  rpmuint32_t ildl[2];
1037  size_t pvlen = sizeof(ildl) + (il * sizeof(*pe)) + dl;
1038  unsigned char * dataStart = (unsigned char *) (pe + il);
1039  indexEntry entry = (indexEntry)
1040  memset(alloca(sizeof(*entry)), 0, sizeof(*entry));
1041  entryInfo info = (entryInfo)
1042  memset(alloca(sizeof(*info)), 0, sizeof(*info));
1043  const void * sig = NULL;
1044  unsigned char * b;
1046  rpmop op;
1047  size_t siglen = 0;
1048  int blen;
1049  size_t nb;
1050  rpmuint32_t ril = 0;
1051  unsigned char * regionEnd = NULL;
1052  rpmRC rc = RPMRC_FAIL; /* assume failure */
1053  int xx;
1054  rpmuint32_t i;
1055 pgpPkt pp = (pgpPkt) alloca(sizeof(*pp));
1056 size_t pleft;
1057 
1058 if (_pkgio_debug)
1059 fprintf(stderr, "--> headerCheck(%p, %p[%u], %p)\n", dig, uh, (unsigned) uc, msg);
1060 
1061  buf[0] = '\0';
1062 
1063  /* Is the blob the right size? */
1064  if (uc > 0 && pvlen != uc) {
1065  (void) snprintf(buf, sizeof(buf),
1066  _("blob size(%d): BAD, 8 + 16 * il(%u) + dl(%u)"),
1067  (int)uc, (unsigned)il, (unsigned)dl);
1068  goto exit;
1069  }
1070 
1071  /* Check (and convert) the 1st tag element. */
1072  xx = headerVerifyInfo(1, dl, pe, &entry->info, 0);
1073  if (xx != -1) {
1074  (void) snprintf(buf, sizeof(buf),
1075  _("tag[%d]: BAD, tag %u type %u offset %d count %u"),
1076  0, (unsigned) entry->info.tag, (unsigned) entry->info.type,
1077  (int)entry->info.offset, (unsigned) entry->info.count);
1078  goto exit;
1079  }
1080 
1081  /* Is there an immutable header region tag? */
1082 /*@-sizeoftype@*/
1083  if (entry->info.tag != RPMTAG_HEADERIMMUTABLE) {
1084  rc = RPMRC_NOTFOUND;
1085  goto exit;
1086  }
1087 
1088  /* Is the region tag sane? */
1089  if (!(entry->info.type == RPM_BIN_TYPE
1090  && entry->info.count == (rpmTagCount)REGION_TAG_COUNT))
1091  {
1092  (void) snprintf(buf, sizeof(buf),
1093  _("region tag: BAD, tag %u type %u offset %d count %u"),
1094  (unsigned) entry->info.tag, (unsigned) entry->info.type,
1095  (int)entry->info.offset, (unsigned) entry->info.count);
1096  goto exit;
1097  }
1098 /*@=sizeoftype@*/
1099 
1100  /* Is the offset within the data area? */
1101  if (entry->info.offset >= (int) dl) {
1102  (void) snprintf(buf, sizeof(buf),
1103  _("region offset: BAD, tag %u type %u offset %d count %u"),
1104  (unsigned) entry->info.tag, (unsigned) entry->info.type,
1105  (int)entry->info.offset, (unsigned) entry->info.count);
1106  goto exit;
1107  }
1108 
1109  /* Is there an immutable header region tag trailer? */
1110  regionEnd = dataStart + entry->info.offset;
1111 /*@-sizeoftype@*/
1112  (void) memcpy(info, regionEnd, REGION_TAG_COUNT);
1113  regionEnd += REGION_TAG_COUNT;
1114 
1115  xx = headerVerifyInfo(1, il * sizeof(*pe), info, &entry->info, 1);
1116  if (xx != -1 ||
1117  !(entry->info.tag == RPMTAG_HEADERIMMUTABLE
1118  && entry->info.type == REGION_TAG_TYPE
1119  && entry->info.count == (rpmTagCount)REGION_TAG_COUNT))
1120  {
1121  (void) snprintf(buf, sizeof(buf),
1122  _("region trailer: BAD, tag %u type %u offset %d count %u"),
1123  (unsigned) entry->info.tag, (unsigned) entry->info.type,
1124  (int)entry->info.offset, (unsigned) entry->info.count);
1125  goto exit;
1126  }
1127 /*@=sizeoftype@*/
1128  memset(info, 0, sizeof(*info));
1129 
1130  /* Is the no. of tags in the region less than the total no. of tags? */
1131  ril = (rpmuint32_t) (entry->info.offset/sizeof(*pe));
1132  if ((entry->info.offset % sizeof(*pe)) || ril > il) {
1133  (void) snprintf(buf, sizeof(buf),
1134  _("region size: BAD, ril(%u) > il(%u)"), (unsigned) ril, (unsigned)il);
1135  goto exit;
1136  }
1137 
1138  /* Find a header-only digest/signature tag. */
1139  for (i = ril; i < (unsigned) il; i++) {
1140  xx = headerVerifyInfo(1, dl, pe+i, &entry->info, 0);
1141  if (xx != -1) {
1142  (void) snprintf(buf, sizeof(buf),
1143  _("tag[%u]: BAD, tag %u type %u offset %d count %u"),
1144  (unsigned) i, (unsigned) entry->info.tag, (unsigned) entry->info.type,
1145  (int)entry->info.offset, (unsigned) entry->info.count);
1146  goto exit;
1147  }
1148 
1149  switch (entry->info.tag) {
1150  case RPMTAG_SHA1HEADER:
1151  if (vsflags & RPMVSF_NOSHA1HEADER)
1152  /*@switchbreak@*/ break;
1153  blen = 0;
1154  for (b = dataStart + entry->info.offset; *b != '\0'; b++) {
1155  if (strchr("0123456789abcdefABCDEF", *b) == NULL)
1156  /*@innerbreak@*/ break;
1157  blen++;
1158  }
1159  if (entry->info.type != RPM_STRING_TYPE || *b != '\0' || blen != 40)
1160  {
1161  (void) snprintf(buf, sizeof(buf), _("hdr SHA1: BAD, not hex"));
1162  goto exit;
1163  }
1164  if (info->tag == 0) {
1165  *info = entry->info; /* structure assignment */
1166  siglen = blen + 1;
1167  }
1168  /*@switchbreak@*/ break;
1169  case RPMTAG_RSAHEADER:
1170  if (vsflags & RPMVSF_NORSAHEADER)
1171  /*@switchbreak@*/ break;
1172  if (entry->info.type != RPM_BIN_TYPE) {
1173  (void) snprintf(buf, sizeof(buf), _("hdr RSA: BAD, not binary"));
1174  goto exit;
1175  }
1176  *info = entry->info; /* structure assignment */
1177  siglen = info->count;
1178  /*@switchbreak@*/ break;
1179  case RPMTAG_DSAHEADER:
1180  if (vsflags & RPMVSF_NODSAHEADER)
1181  /*@switchbreak@*/ break;
1182  if (entry->info.type != RPM_BIN_TYPE) {
1183  (void) snprintf(buf, sizeof(buf), _("hdr DSA: BAD, not binary"));
1184  goto exit;
1185  }
1186  *info = entry->info; /* structure assignment */
1187  siglen = info->count;
1188  /*@switchbreak@*/ break;
1189  default:
1190  /*@switchbreak@*/ break;
1191  }
1192  }
1193  rc = RPMRC_NOTFOUND;
1194 
1195 exit:
1196  /* Return determined RPMRC_OK/RPMRC_FAIL conditions. */
1197  if (rc != RPMRC_NOTFOUND) {
1198  buf[sizeof(buf)-1] = '\0';
1199  if (msg) *msg = xstrdup(buf);
1200 if (_pkgio_debug)
1201 fprintf(stderr, "<-- headerCheck #1: rc %d \"%s\"\n", rc, (msg ? *msg: ""));
1202  return rc;
1203  }
1204 
1205  /* If no header-only digest/signature, then do simple sanity check. */
1206  if (info->tag == 0) {
1207  xx = (ril > 0 ? headerVerifyInfo(ril-1, dl, pe+1, &entry->info, 0) : -1);
1208  if (xx != -1) {
1209  (void) snprintf(buf, sizeof(buf),
1210  _("tag[%d]: BAD, tag %u type %u offset %d count %u"),
1211  xx+1, (unsigned) entry->info.tag, (unsigned) entry->info.type,
1212  (int)entry->info.offset, (unsigned) entry->info.count);
1213  rc = RPMRC_FAIL;
1214  } else {
1215  (void) snprintf(buf, sizeof(buf), "Header sanity check: OK");
1216  rc = RPMRC_OK;
1217  }
1218  buf[sizeof(buf)-1] = '\0';
1219  if (msg) *msg = xstrdup(buf);
1220 if (_pkgio_debug)
1221 fprintf(stderr, "<-- headerCheck #2: rc %d \"%s\"\n", rc, (msg ? *msg: ""));
1222  return rc;
1223  }
1224 
1225  /* Verify header-only digest/signature. */
1226 assert(dig != NULL);
1227  dig->nbytes = 0;
1228 
1229  sig = memcpy(xmalloc(siglen), dataStart + info->offset, siglen);
1230  {
1231  const void * osig = pgpGetSig(dig);
1232 /*@-modobserver -observertrans -dependenttrans @*/ /* FIX: pgpSetSig() lazy free. */
1233  osig = _free(osig);
1234 /*@=modobserver =observertrans =dependenttrans @*/
1235  (void) pgpSetSig(dig, info->tag, info->type, sig, info->count);
1236  }
1237 
1238  switch (info->tag) {
1239  case RPMTAG_RSAHEADER:
1240  /* Parse the parameters from the OpenPGP packets that will be needed. */
1241  pleft = info->count;
1242  xx = pgpPktLen((const rpmuint8_t *)sig, pleft, pp);
1243  xx = rpmhkpLoadSignature(NULL, dig, pp);
1244  if (dig->signature.version != (rpmuint8_t)3
1245  && dig->signature.version != (rpmuint8_t)4)
1246  {
1248  _("skipping header with unverifiable V%u signature\n"),
1249  (unsigned) dig->signature.version);
1250  rc = RPMRC_FAIL;
1251  goto exit;
1252  }
1253 
1254  ildl[0] = (rpmuint32_t) htonl(ril);
1255  ildl[1] = (rpmuint32_t) (regionEnd - dataStart);
1256  ildl[1] = (rpmuint32_t) htonl(ildl[1]);
1257 
1258  op = (rpmop) pgpStatsAccumulator(dig, 10); /* RPMTS_OP_DIGEST */
1259  (void) rpmswEnter(op, 0);
1260  dig->hdrctx = rpmDigestInit((pgpHashAlgo)dig->signature.hash_algo, RPMDIGEST_NONE);
1261 
1262  b = NULL; nb = 0;
1263  (void) headerGetMagic(NULL, &b, &nb);
1264  if (b && nb > 0) {
1265  (void) rpmDigestUpdate(dig->hdrctx, b, nb);
1266  dig->nbytes += nb;
1267  }
1268 
1269  b = (unsigned char *) ildl;
1270  nb = sizeof(ildl);
1271  (void) rpmDigestUpdate(dig->hdrctx, b, nb);
1272  dig->nbytes += nb;
1273 
1274  b = (unsigned char *) pe;
1275  nb = (size_t) (htonl(ildl[0]) * sizeof(*pe));
1276  (void) rpmDigestUpdate(dig->hdrctx, b, nb);
1277  dig->nbytes += nb;
1278 
1279  b = (unsigned char *) dataStart;
1280  nb = (size_t) htonl(ildl[1]);
1281  (void) rpmDigestUpdate(dig->hdrctx, b, nb);
1282  dig->nbytes += nb;
1283  (void) rpmswExit(op, dig->nbytes);
1284 
1285  break;
1286  case RPMTAG_DSAHEADER:
1287  /* Parse the parameters from the OpenPGP packets that will be needed. */
1288  pleft = info->count;
1289  xx = pgpPktLen((const rpmuint8_t *)sig, pleft, pp);
1290  xx = rpmhkpLoadSignature(NULL, dig, pp);
1291  if (dig->signature.version != (rpmuint8_t)3
1292  && dig->signature.version != (rpmuint8_t)4)
1293  {
1295  _("skipping header with unverifiable V%u signature\n"),
1296  (unsigned) dig->signature.version);
1297  rc = RPMRC_FAIL;
1298  goto exit;
1299  }
1300  /*@fallthrough@*/
1301  case RPMTAG_SHA1HEADER:
1302  ildl[0] = (rpmuint32_t) htonl(ril);
1303  ildl[1] = (rpmuint32_t) (regionEnd - dataStart);
1304  ildl[1] = (rpmuint32_t) htonl(ildl[1]);
1305 
1306  op = (rpmop) pgpStatsAccumulator(dig, 10); /* RPMTS_OP_DIGEST */
1307  (void) rpmswEnter(op, 0);
1308  dig->hdrsha1ctx = rpmDigestInit(PGPHASHALGO_SHA1, RPMDIGEST_NONE);
1309 
1310  b = NULL; nb = 0;
1311  (void) headerGetMagic(NULL, &b, &nb);
1312  if (b && nb > 0) {
1313  (void) rpmDigestUpdate(dig->hdrsha1ctx, b, nb);
1314  dig->nbytes += nb;
1315  }
1316 
1317  b = (unsigned char *) ildl;
1318  nb = sizeof(ildl);
1319  (void) rpmDigestUpdate(dig->hdrsha1ctx, b, nb);
1320  dig->nbytes += nb;
1321 
1322  b = (unsigned char *) pe;
1323  nb = (size_t) (htonl(ildl[0]) * sizeof(*pe));
1324  (void) rpmDigestUpdate(dig->hdrsha1ctx, b, nb);
1325  dig->nbytes += nb;
1326 
1327  b = (unsigned char *) dataStart;
1328  nb = (size_t) htonl(ildl[1]);
1329  (void) rpmDigestUpdate(dig->hdrsha1ctx, b, nb);
1330  dig->nbytes += nb;
1331  (void) rpmswExit(op, dig->nbytes);
1332 
1333  break;
1334  default:
1335  sig = _free(sig);
1336  break;
1337  }
1338 
1339  buf[0] = '\0';
1340  rc = rpmVerifySignature(dig, buf);
1341 
1342  buf[sizeof(buf)-1] = '\0';
1343  if (msg) *msg = xstrdup(buf);
1344 
1345 if (_pkgio_debug)
1346 fprintf(stderr, "<-- headerCheck #3: rc %d \"%s\"\n", rc, (msg ? *msg: ""));
1347  return rc;
1348 }
1349 
1355 static size_t szHeader(/*@null@*/ const void * ptr)
1356  /*@*/
1357 {
1358  rpmuint32_t p[4];
1359 assert(ptr != NULL);
1360  memcpy(p, ptr, sizeof(p));
1361  return (8 + 8 + 16 * ntohl(p[2]) + ntohl(p[3]));
1362 }
1363 
1364 /*@-globuse@*/
1372 static rpmRC ckHeader(/*@unused@*/ FD_t fd, const void * ptr,
1373  /*@unused@*/ /*@null@*/ const char ** msg)
1374  /*@globals fileSystem, internalState @*/
1375  /*@modifies ptr, fileSystem, internalState @*/
1376 {
1377  rpmRC rc = RPMRC_OK;
1378  Header h;
1379 
1380  h = headerLoad((void *)ptr);
1381  if (h == NULL)
1382  rc = RPMRC_FAIL;
1383  (void)headerFree(h);
1384  h = NULL;
1385 
1386  return rc;
1387 }
1388 
1396 static rpmRC rpmReadHeader(FD_t fd, /*@null@*/ Header * hdrp,
1397  /*@null@*/ const char ** msg)
1398  /*@globals fileSystem, internalState @*/
1399  /*@modifies fd, *hdrp, *msg, fileSystem, internalState @*/
1400 {
1401 rpmxar xar = fdGetXAR(fd);
1402  pgpDig dig = pgpDigLink(fdGetDig(fd));
1403  char buf[BUFSIZ];
1404  rpmuint32_t block[4];
1405  rpmuint32_t il;
1406  rpmuint32_t dl;
1407  rpmuint32_t * ei = NULL;
1408  size_t uc = 0;
1409  unsigned char * b;
1410  size_t startoff;
1411  size_t nb;
1412  Header h = NULL;
1413  const char * origin = NULL;
1414  rpmRC rc = RPMRC_FAIL; /* assume failure */
1415  int xx;
1416  static int map = 1;
1417 
1418 if (_pkgio_debug)
1419 fprintf(stderr, "--> rpmReadHeader(%p, %p, %p)\n", fd, hdrp, msg);
1420 
1421  /* Create (if not already) a signature parameters container. */
1422  if (dig == NULL) {
1424  (void) fdSetDig(fd, dig);
1425  }
1426 
1427  buf[0] = '\0';
1428 
1429  if (hdrp)
1430  *hdrp = NULL;
1431 
1432  memset(block, 0, sizeof(block));
1433  if (xar != NULL) {
1434  const char item[] = "Header";
1435  if ((xx = rpmxarNext(xar)) != 0 || (xx = rpmxarPull(xar, item)) != 0) {
1436  (void) snprintf(buf, sizeof(buf),
1437  _("XAR file not found (or no XAR support)"));
1438  rc = RPMRC_NOTFOUND;
1439  goto exit;
1440  }
1441  }
1442 
1443  startoff = fd->stats->ops[FDSTAT_READ].bytes;
1444  if ((xx = (int) timedRead(fd, (char *)block, sizeof(block))) != (int)sizeof(block)) {
1445  /* XXX Handle EOF's as RPMRC_NOTFOUND, not RPMRC_FAIL, returns. */
1446  if (xx == 0)
1447  rc = RPMRC_NOTFOUND;
1448  else
1449  (void) snprintf(buf, sizeof(buf),
1450  _("hdr size(%u): BAD, read returned %d"), (unsigned)sizeof(block), xx);
1451  goto exit;
1452  }
1453 
1454  b = NULL;
1455  nb = 0;
1456  (void) headerGetMagic(NULL, &b, &nb);
1457  if (memcmp(block, b, nb)) {
1458  unsigned char * x = (unsigned char *) block;
1459 /*@+charint@*/
1460  (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]);
1461 /*@=charint@*/
1462  goto exit;
1463  }
1464 
1465  il = (rpmuint32_t)ntohl(block[2]);
1466  if (hdrchkTags(il)) {
1467  (void) snprintf(buf, sizeof(buf),
1468  _("hdr tags: BAD, no. of tags(%u) out of range"), (unsigned) il);
1469 
1470  goto exit;
1471  }
1472  dl = (rpmuint32_t)ntohl(block[3]);
1473  if (hdrchkData(dl)) {
1474  (void) snprintf(buf, sizeof(buf),
1475  _("hdr data: BAD, no. of bytes(%u) out of range\n"), (unsigned) dl);
1476  goto exit;
1477  }
1478 
1479 /*@-sizeoftype@*/
1480  nb = (il * sizeof(struct entryInfo_s)) + dl;
1481 /*@=sizeoftype@*/
1482  uc = sizeof(il) + sizeof(dl) + nb;
1483  if (map) {
1484  static const int prot = PROT_READ | PROT_WRITE;
1485  static const int flags = MAP_PRIVATE| MAP_ANONYMOUS;
1486  static const int fdno = -1;
1487  static const off_t off = 0;
1488 
1489  ei = (rpmuint32_t *) mmap(NULL, uc, prot, flags, fdno, off);
1490  if (ei == NULL || ei == (void *)-1)
1491  fprintf(stderr,
1492  "==> mmap(%p[%u], 0x%x, 0x%x, %d, 0x%x) error(%d): %s\n",
1493  NULL, (unsigned)uc, prot, flags, fdno, (unsigned)off,
1494  errno, strerror(errno));
1495  } else {
1496  ei = (rpmuint32_t *) xmalloc(uc);
1497  }
1498 
1499  if ((xx = (int) timedRead(fd, (char *)&ei[2], nb)) != (int) nb) {
1500  (void) snprintf(buf, sizeof(buf),
1501  _("hdr blob(%u): BAD, read returned %d"), (unsigned)nb, xx);
1502  goto exit;
1503  }
1504  ei[0] = block[2];
1505  ei[1] = block[3];
1506 
1507  if (map) {
1508  if (mprotect(ei, uc, PROT_READ) != 0)
1509  fprintf(stderr, "==> mprotect(%p[%u],0x%x) error(%d): %s\n",
1510  ei, (unsigned)uc, PROT_READ,
1511  errno, strerror(errno));
1512  }
1513 
1514  /* Sanity check header tags */
1515  rc = headerCheck(dig, ei, uc, msg);
1516  if (rc != RPMRC_OK)
1517  goto exit;
1518 
1519  /* OK, blob looks sane, load the header. */
1520  h = headerLoad(ei);
1521  if (h == NULL) {
1522  (void) snprintf(buf, sizeof(buf), _("hdr load: BAD\n"));
1523  goto exit;
1524  }
1525  if (map) {
1526  h->flags |= HEADERFLAG_MAPPED;
1527  h->flags |= HEADERFLAG_RDONLY;
1528  } else
1530  ei = NULL; /* XXX will be freed with header */
1531 
1532  /* Save the opened path as the header origin. */
1533  /* XXX TODO: push the Realpath() underneath fdGetOPath(). */
1534  origin = fdGetOPath(fd);
1535  if (origin != NULL) {
1536  const char * lpath = NULL;
1537  int ut = urlPath(origin, &lpath);
1538  ut = ut; /* XXX keep gcc quiet. */
1539  if (lpath && *lpath != '/') {
1540  char * rpath = Realpath(origin, NULL);
1541  (void) headerSetOrigin(h, (rpath ? rpath : NULL));
1542  rpath = _free(rpath);
1543  } else
1544  (void) headerSetOrigin(h, origin);
1545  }
1546 /*@-mods@*/
1547  { struct stat * st = headerGetStatbuf(h);
1548  int saveno = errno;
1549  (void) Fstat(fd, st);
1550  errno = saveno;
1551  }
1552 /*@=mods@*/
1553  (void) headerSetStartOff(h, (rpmuint32_t)startoff);
1554  (void) headerSetEndOff(h, fd->stats->ops[FDSTAT_READ].bytes);
1555 
1556 exit:
1557  if (hdrp && h && rc == RPMRC_OK)
1558  *hdrp = headerLink(h);
1559  if (ei != NULL && uc > 0) {
1560  if (map) {
1561  if (munmap(ei, uc) != 0)
1562  fprintf(stderr, "==> munmap(%p[%u]) error(%d): %s\n",
1563  ei, (unsigned)uc, errno, strerror(errno));
1564  ei = NULL;
1565  } else
1566  ei = _free(ei);
1567  }
1568  dig = pgpDigFree(dig);
1569  (void)headerFree(h);
1570  h = NULL;
1571 
1572  if (msg != NULL && *msg == NULL && buf[0] != '\0') {
1573  buf[sizeof(buf)-1] = '\0';
1574  *msg = xstrdup(buf);
1575  }
1576 
1577 if (_pkgio_debug)
1578 fprintf(stderr, "<-- rpmReadHeader: rc %d \"%s\"\n", rc, (msg ? *msg: ""));
1579  return rc;
1580 }
1581 
1589 static rpmRC rdHeader(FD_t fd, /*@out@*/ /*@null@*/ void * ptr,
1590  /*@null@*/ const char ** msg)
1591  /*@globals fileSystem, internalState @*/
1592  /*@modifies fd, *ptr, *msg, fileSystem, internalState @*/
1593 {
1594  Header * hdrp = (Header *) ptr;
1595 /*@-compdef@*/
1596  return rpmReadHeader(fd, hdrp, msg);
1597 /*@=compdef@*/
1598 }
1599 
1607 static rpmRC wrHeader(FD_t fd, void * ptr,
1608  /*@unused@*/ /*@null@*/ const char ** msg)
1609  /*@globals fileSystem, internalState @*/
1610  /*@modifies fd, ptr, *msg, fileSystem, internalState @*/
1611 {
1612  Header h = (Header) ptr;
1613  return rpmWriteHeader(fd, h, msg);
1614 }
1615 /*@=globuse@*/
1616 
1617 /*===============================================*/
1618 
1619 size_t rpmpkgSizeof(const char * fn, const void * ptr)
1620 {
1621  size_t len = 0;
1622 
1623  if (!strcmp(fn, "Lead"))
1624  len = 96; /* RPMLEAD_SIZE */
1625  else
1626  if (!strcmp(fn, "Signature")) {
1627  len = szHeader(ptr);
1628  len += ((8 - (len % 8)) % 8); /* padding */
1629  } else
1630  if (!strcmp(fn, "Header"))
1631  len = szHeader(ptr);
1632  return len;
1633 }
1634 
1635 rpmRC rpmpkgCheck(const char * fn, FD_t fd, const void * ptr, const char ** msg)
1636 {
1637  rpmRC rc = RPMRC_FAIL;
1638 
1639  if (msg)
1640  *msg = NULL;
1641 
1642  if (!strcmp(fn, "Header"))
1643  rc = ckHeader(fd, ptr, msg);
1644  return rc;
1645 }
1646 
1647 rpmRC rpmpkgRead(const char * fn, FD_t fd, void * ptr, const char ** msg)
1648 {
1649  rpmRC rc = RPMRC_FAIL;
1650 
1651  if (msg)
1652  *msg = NULL;
1653 
1654  if (!strcmp(fn, "Lead"))
1655  rc = rdLead(fd, ptr, msg);
1656  else
1657  if (!strcmp(fn, "Signature"))
1658  rc = rdSignature(fd, ptr, msg);
1659  else
1660  if (!strcmp(fn, "Header"))
1661  rc = rdHeader(fd, ptr, msg);
1662  return rc;
1663 }
1664 
1665 rpmRC rpmpkgWrite(const char * fn, FD_t fd, void * ptr, const char ** msg)
1666 {
1667  rpmRC rc = RPMRC_FAIL;
1668 
1669  if (msg)
1670  *msg = NULL;
1671 
1672  if (!strcmp(fn, "Lead"))
1673  rc = wrLead(fd, ptr, msg);
1674  else
1675  if (!strcmp(fn, "Signature"))
1676  rc = wrSignature(fd, ptr, msg);
1677  else
1678  if (!strcmp(fn, "Header"))
1679  rc = wrHeader(fd, ptr, msg);
1680  return rc;
1681 }
Structure(s)and methods for a XAR archive wrapper format.
FDSTAT_t stats
rpmTagType t
Definition: rpmtag.h:502
rpmuint32_t siglen
Definition: signature.c:618
int xx
Definition: spec.c:744
rpmTag tag
Definition: rpmtag.h:501
static rpmRC rpmWriteHeader(FD_t fd, Header h, const char **msg)
Write (with unload) header to file handle.
Definition: pkgio.c:75
rpmop rpmtsOp(rpmts ts, rpmtsOpX opx)
Retrieve operation timestamp from a transaction set.
Definition: pkgio.c:133
const void * uh
Definition: rpmts-py.c:977
const char ** argv
Definition: rpmtag.h:75
struct rpmxar_s * rpmxar
Definition: rpmxar.h:14
enum pgpPubkeyAlgo_e pgpPubkeyAlgo
9.1.
int pgpGrabPkts(const rpmuint8_t *pkts, size_t pktlen, rpmuint8_t ***pppkts, int *pnpkts)
Return array of packet pointers.
Definition: rpmpgp.c:1306
#define headerFree(_h)
Definition: rpmtag.h:870
struct entryInfo_s info
struct rpmdb_s * rpmdb
Database of headers and tag value indices.
Definition: rpmtypes.h:43
rpmlog(RPMLOG_ERR,"%s\n", buf)
int headerVerifyInfo(rpmuint32_t il, rpmuint32_t dl, const void *pev, void *iv, int negate)
Perform simple sanity and range checks on header tag(s).
static void fdSetXAR(FD_t fd, rpmxar xar)
rpmuint32_t rpmTagCount
Definition: rpmtag.h:55
static rpmRC wrHeader(FD_t fd, void *ptr, const char **msg)
Write metadata header.
Definition: pkgio.c:1607
struct stat * headerGetStatbuf(Header h)
Return header stat(2) buffer (of origin *.rpm file).
Definition: header.c:1228
rpmtime_t rpmswAdd(rpmop to, rpmop from)
Sum statistic counters.
Definition: rpmsw.c:280
int pgpPktLen(const rpmuint8_t *pkt, size_t pleft, pgpPkt pp)
Definition: rpmpgp.c:939
static size_t szHeader(const void *ptr)
Return size of Header.
Definition: pkgio.c:1355
static rpmRC rdSignature(FD_t fd, void *ptr, const char **msg)
Read (and verify header+payload size) signature header.
Definition: pkgio.c:751
rpmRC rpmpkgWrite(const char *fn, FD_t fd, void *ptr, const char **msg)
Write item onto file descriptor.
Definition: pkgio.c:1665
size_t Fwrite(const void *buf, size_t size, size_t nmemb, FD_t fd)
fwrite(3) clone.
Definition: rpmio.c:2432
#define timedRead
Definition: pkgio.c:63
char * xstrdup(const char *str)
Definition: rpmmalloc.c:321
rpmuint32_t * ui32p
Definition: rpmtag.h:70
unsigned short type
Definition: pkgio.c:484
uint32_t rpmmiInstance(rpmmi mi)
Return header instance for current position of rpmdb iterator.
Definition: rpmdb.c:1743
char * rpmGetPath(const char *path,...)
Return (malloc&#39;ed) expanded, canonicalized, file path.
Definition: macro.c:3371
static char *size_t nb
fgets(3) analogue that reads \ continuations.
Definition: macro.c:409
rpmTagType type
int pgpPubkeyFingerprint(const rpmuint8_t *pkt, size_t pktlen, rpmuint8_t *keyid)
Print/parse an OpenPGP subtype packet.
Definition: rpmpgp.c:969
struct rpmts_s * rpmts
The RPM Transaction Set.
Definition: rpmtypes.h:14
int rc
Definition: poptALL.c:670
rpmdb rpmtsGetRdb(rpmts ts)
Get transaction set database handle.
Definition: pkgio.c:151
The Header data structure.
int rpmxarNext(rpmxar xar)
Iterate a xar archive instance.
Definition: rpmxar.c:128
pgpDig rpmtsDig(rpmts ts)
Get OpenPGP packet parameters, i.e.
Definition: pkgio.c:442
Definition: rpmdb.c:436
rpmRC headerCheck(pgpDig dig, const void *uh, size_t uc, const char **msg)
Check header consistency, performing headerGet() the hard way.
Definition: pkgio.c:1027
enum rpmTag_e rpmTag
Definition: rpmtag.h:468
int errno
#define HEADERFLAG_ALLOCATED
#define HEADERFLAG_RDONLY
Header headerLink(Header h)
Reference a header instance.
Header h
Definition: spec.c:739
pgpDigParams rpmtsPubkey(const rpmts ts)
Return OpenPGP pubkey constants.
Definition: pkgio.c:144
rpmRC rpmpkgCheck(const char *fn, FD_t fd, const void *ptr, const char **msg)
Verify item integrity.
Definition: pkgio.c:1635
struct pgpDig_s * pgpDig
Definition: rpmiotypes.h:86
unsigned char rpmuint8_t
Private int typedefs to avoid C99 portability issues.
Definition: rpmiotypes.h:26
struct rpmiob_s * rpmiob
Definition: rpmiotypes.h:60
#define REGION_TAG_TYPE
sprintf(t," (%u)",(unsigned) dig->nbytes)
exit Fhe p ptr
Definition: db3.c:2119
pgpArmor pgpReadPkts(const char *fn, rpmuint8_t **pkt, size_t *pktlen)
Parse armored OpenPGP packets from a file.
Definition: rpmpgp.c:1569
rpmRC res
Definition: signature.c:584
rpmTagCount count
int Fstat(FD_t fd, struct stat *st)
fstat(2) clone.
Definition: rpmrpc.c:1441
char * alloca()
pgpDig pgpDigNew(pgpVSFlags vsflags, pgpPubkeyAlgo pubkey_algo)
Create a container for parsed OpenPGP packates.
Definition: rpmpgp.c:1205
enum rpmRC_e rpmRC
RPM return codes.
Definition: signature.c:616
goto exit
Definition: db3.c:1903
memset(_r, 0, sizeof(*_r))
int headerSetOrigin(Header h, const char *origin)
Store header origin (e.g path or URL).
Definition: header.c:1189
unsigned int rpmuint32_t
Definition: rpmiotypes.h:28
pgpVSFlags pgpDigVSFlags
Disabler bits(s) for signature/digest checking.
Definition: rpmpgp.c:1104
const char * Fstrerror(FD_t fd)
strerror(3) clone.
Definition: rpmio.c:2399
void * xcalloc(size_t nmemb, size_t size)
Definition: rpmmalloc.c:300
assert(key->size==sizeof(hdrNum))
void * ptr
Definition: rpmtag.h:67
int rpmdbOpen(const char *prefix, rpmdb *dbp, int mode, mode_t perms)
Open rpm database.
Definition: rpmdb.c:1175
int ix
Definition: rpmps-py.c:174
char * p
Definition: macro.c:413
int headerGetMagic(Header h, unsigned char **magicp, size_t *nmagicp)
Return header magic.
Definition: header.c:1162
rpmmi rpmmiFree(rpmmi mi)
Destroy rpm database iterator.
static char * pgpHexStr(const rpmuint8_t *p, size_t plen)
Return hex formatted representation of bytes.
Definition: rpmpgp.h:1164
int rpmxarPull(rpmxar xar, const char *fn)
Definition: rpmxar.c:168
static rpmRC wrLead(FD_t fd, const void *ptr, const char **msg)
Write lead to file handle.
Definition: pkgio.c:509
fprintf(stderr,"--> %s(%p,%p,%p) sig %p sigp %p\n", __FUNCTION__, dig, t, rsactx, sig, sigp)
size_t rpmpkgSizeof(const char *fn, const void *ptr)
Return size of item in bytes.
Definition: pkgio.c:1619
static rpmxar fdGetXAR(FD_t fd)
enum pgpHashAlgo_e pgpHashAlgo
9.4.
The lead data structure.
Definition: pkgio.c:480
enum rpmtsOpX_e rpmtsOpX
Indices for timestamps.
static rpmRC rdHeader(FD_t fd, void *ptr, const char **msg)
Read metadata header.
Definition: pkgio.c:1589
void * pgpStatsAccumulator(pgpDig dig, int opx)
Return pgpDig container accumulator structure.
Definition: rpmpgp.c:1270
rpmTagData p
Definition: rpmtag.h:504
static rpmRC rpmReadHeader(FD_t fd, Header *hdrp, const char **msg)
Return checked and loaded header.
Definition: pkgio.c:1396
unsigned short osnum
Definition: pkgio.c:487
int rpmDigestUpdate(DIGEST_CTX ctx, const void *data, size_t len)
Update context with next plain text buffer.
Definition: digest.c:907
void rpmswExit(op, 0)
pgpDigParams pgpGetSignature(pgpDig dig)
Return OpenPGP signature parameters.
Definition: rpmpgp.c:1226
#define hdrchkTags(_ntags)
Sanity check on no.
Header rpmmiNext(rpmmi mi)
Return next package header from iteration.
Definition: rpmdb.c:2248
struct _HE_s * HE_t
Destroy an extension cache.
Definition: rpmtag.h:59
unsigned short signature_type
Definition: pkgio.c:488
int headerGet(Header h, HE_t he, unsigned int flags)
Retrieve extension or tag value from a header.
Definition: header.c:2230
int rpmint32_t
Definition: rpmiotypes.h:33
Header headerLoad(void *uh)
Convert header to in-memory representation.
Definition: header.c:970
The FD_t File Handle data structure.
pgpDig pgpDigFree(pgpDig dig)
Destroy a container for parsed OpenPGP packates.
struct indexEntry_s * indexEntry
A single tag from a Header.
#define HEADERFLAG_SIGNATURE
struct pgpPkt_s * pgpPkt
Definition: rpmiotypes.h:82
rpmTagCount c
Definition: rpmtag.h:505
Generate and verify rpm package signatures.
DIGEST_CTX rpmDigestInit(pgpHashAlgo hashalgo, rpmDigestFlags flags)
Initialize digest.
Definition: digest.c:244
pgpDig pgpDigLink(pgpDig dig)
Reference a signature parameters instance.
char * rpmExpand(const char *arg,...)
Return (malloc&#39;ed) concatenated macro expansion(s).
Definition: macro.c:3178
#define HEADERFLAG_MAPPED
static rpmRC ckHeader(FD_t fd, const void *ptr, const char **msg)
Check metadata header.
Definition: pkgio.c:1372
static void fdSetDig(FD_t fd, pgpDig dig)
static unsigned
Definition: rpmmtree.c:386
rpmuint32_t headerSetStartOff(Header h, rpmuint32_t startoff)
Store header starting byte offset.
Definition: header.c:1295
* op
Definition: rpmps-py.c:266
rpmuint32_t headerSetEndOff(Header h, rpmuint32_t endoff)
Store header ending byte offset.
Definition: header.c:1307
pgpVSFlags rpmVSFlags
Bit(s) to control digest and signature verification.
Definition: rpmts.h:35
unsigned char major
Definition: pkgio.c:482
node fd
Definition: rpmfd-py.c:124
struct entryInfo_s * entryInfo
Description of tag data.
spectags st
Definition: spec.c:741
Cumulative statistics for an operation.
Definition: rpmsw.h:39
rpmiob rpmiobNew(size_t len)
Create an I/O buffer.
Definition: rpmiob.c:44
int Ferror(FD_t fd)
ferror(3) clone.
Definition: rpmio.c:2942
Definition: rpmtag.h:500
return strcmp(ame->name, bme->name)
rpmRC rpmkuFindPubkey(pgpDigParams sigp, rpmiob *iobp)
Lookup pubkey in keyutils keyring.
Definition: rpmku.c:157
urltype urlPath(const char *url, const char **pathp)
Return path component of URL.
Definition: url.c:430
static unsigned char lead_magic[]
Definition: pkgio.c:495
static int snprintf(char *buf, int nb, const char *fmt,...)
Definition: rpmps.c:220
#define REGION_TAG_COUNT
Methods to handle package elements.
int pgpSetFindPubkey(pgpDig dig, int(*findPubkey)(void *ts, void *dig), void *_ts)
Set find pubkey vector.
Definition: rpmpgp.c:1284
mi
Definition: rpmdb-py.c:159
unsigned char magic[4]
Definition: pkgio.c:481
int rpmxarSwapBuf(rpmxar xar, unsigned char *b, size_t bsize, unsigned char **obp, size_t *obsizep)
Definition: rpmxar.c:203
void * headerUnload(Header h, size_t *lenp)
headerUnload.
Definition: header.c:648
const void * pgpGetSig(pgpDig dig)
Get signature tag data, i.e.
Definition: rpmpgp.c:1246
size_t headerSizeof(Header h)
Return size of on-disk header representation in bytes.
Definition: header.c:266
Structures and prototypes used for an &quot;rpmts&quot; transaction set.
int flags
Definition: fnmatch.c:282
struct headerToken_s * Header
Definition: rpmtag.h:22
const char * msg
Definition: rpmts-py.c:976
static const char * fdGetOPath(FD_t fd)
static rpmRC wrSignature(FD_t fd, void *ptr, const char **msg)
Write signature header.
Definition: pkgio.c:674
struct pgpDigParams_s * pgpDigParams
Definition: rpmiotypes.h:90
rpmuint32_t flags
return NULL
Definition: poptALL.c:613
int Fileno(FD_t fd)
fileno(3) clone.
Definition: rpmio.c:2989
#define hdrchkData(_nbytes)
Sanity check on data size and/or offset and/or count.
static void
Print copy of spec file, filling in Group/Description/Summary from specspo.
Definition: spec.c:737
rpmxar rpmxarFree(rpmxar xar, const char *msg)
Destroy a xar archive instance.
int pgpSetSig(pgpDig dig, rpmuint32_t sigtag, rpmuint32_t sigtype, const void *sig, rpmuint32_t siglen)
Set signature tag info, i.e.
Definition: rpmpgp.c:1256
char * buf
Parse (and execute) macro undefinition.
Definition: macro.c:703
#define _(Text)
Definition: system.h:29
rpmint32_t offset
char * b
Definition: macro.c:746
int
Save source and expand field into target.
Definition: rpmds.c:2709
static unsigned int pgpGrab(const rpmuint8_t *s, size_t nbytes)
Return (native-endian) integer from big-endian representation.
Definition: rpmpgp.h:1076
char name[66]
Definition: pkgio.c:486
#define xmalloc
Definition: system.h:32
rpmRC rpmpkgRead(const char *fn, FD_t fd, void *ptr, const char **msg)
Read item from file descriptor.
Definition: pkgio.c:1647
unsigned char minor
Definition: pkgio.c:483
Access RPM indices using Berkeley DB interface(s).
void rpmswEnter(op, 0)
struct rpmop_s * rpmop
Definition: rpmsw.h:24
#define D_(Text)
Definition: system.h:526
int fdno
Definition: rpmts-py.c:923
int uc
Definition: rpmts-py.c:978
unsigned short archnum
Definition: pkgio.c:485
static rpmRC printSize(FD_t fd, size_t siglen, size_t pad, size_t datalen)
Print package size.
Definition: pkgio.c:711
rpmVSFlags vsflags
Definition: rpmrepo.c:161
static rpmRC rdLead(FD_t fd, void *ptr, const char **msg)
Read lead from file handle.
Definition: pkgio.c:547
static void * _free(const void *p)
Wrapper to free(3), hides const compilation noise, permit NULL, return NULL.
Definition: rpmiotypes.h:647
pgpDigParams sigp
Definition: signature.c:748
char reserved[16]
Definition: pkgio.c:490
rpmxar rpmxarNew(const char *fn, const char *fmode)
Create a xar archive instance.
Definition: rpmxar.c:112
int i
Definition: spec.c:743
int _pkgio_debug
Definition: pkgio.c:55
pgpDig dig
Definition: rpmts-py.c:979
strncpy(sbuf, f, flen)
char * Realpath(const char *path, char *resolved_path)
realpath(3) clone.
Definition: rpmrpc.c:2330
static pgpDig fdGetDig(FD_t fd)
rpmRC rpmVerifySignature(void *_dig, char *result)
Verify a signature from a package.
Definition: signature.c:941
rpmRC rpmkuStorePubkey(pgpDigParams sigp, rpmiob iob)
Store pubkey in keyutils keyring.
Definition: rpmku.c:204
rpmRC rpmtsFindPubkey(rpmts ts, void *_dig)
Retrieve pubkey from rpm database.
Definition: pkgio.c:162
pgpDigParams pgpGetPubkey(pgpDig dig)
Return OpenPGP pubkey parameters.
Definition: rpmpgp.c:1231
size_t fn
Definition: macro.c:1698
int len
Definition: rpmdb-py.c:119
rpmmi rpmmiInit(rpmdb db, rpmTag tag, const void *keyp, size_t keylen)
Return database iterator.
Definition: rpmdb.c:2491
void rpmtsCleanDig(rpmts ts)
Free signature verification data.
Definition: pkgio.c:457