2000-08-28 03:27:03 +08:00
|
|
|
/** \ingroup lead
|
|
|
|
* \file lib/rpmlead.c
|
|
|
|
*/
|
|
|
|
|
1998-07-26 05:00:26 +08:00
|
|
|
#include "system.h"
|
1997-11-18 11:13:56 +08:00
|
|
|
|
2010-01-05 21:25:31 +08:00
|
|
|
#include <errno.h>
|
1996-02-15 01:55:28 +08:00
|
|
|
#include <netinet/in.h>
|
1996-01-05 11:08:34 +08:00
|
|
|
|
2008-01-30 19:53:51 +08:00
|
|
|
#include <rpm/rpmlib.h> /* rpmGetOs/ArchInfo() */
|
2008-01-30 23:05:29 +08:00
|
|
|
#include <rpm/rpmlog.h>
|
2008-04-17 23:15:34 +08:00
|
|
|
#include <rpm/rpmstring.h>
|
2008-01-30 23:05:29 +08:00
|
|
|
|
2007-11-23 18:39:29 +08:00
|
|
|
#include "lib/signature.h"
|
2012-10-30 17:36:56 +08:00
|
|
|
#include "lib/header_internal.h" /* Freadall() */
|
2007-11-23 18:39:29 +08:00
|
|
|
#include "lib/rpmlead.h"
|
2008-01-30 23:05:29 +08:00
|
|
|
|
2000-12-13 04:03:45 +08:00
|
|
|
#include "debug.h"
|
1996-01-05 11:05:34 +08:00
|
|
|
|
2008-03-07 14:15:03 +08:00
|
|
|
static unsigned char const lead_magic[] = {
|
2002-08-06 03:45:52 +08:00
|
|
|
RPMLEAD_MAGIC0, RPMLEAD_MAGIC1, RPMLEAD_MAGIC2, RPMLEAD_MAGIC3
|
|
|
|
};
|
|
|
|
|
2007-12-02 00:31:09 +08:00
|
|
|
/** \ingroup lead
|
|
|
|
* The lead data structure.
|
|
|
|
* The lead needs to be 8 byte aligned.
|
|
|
|
* @deprecated The lead (except for signature_type) is legacy.
|
|
|
|
* @todo Don't use any information from lead.
|
|
|
|
*/
|
|
|
|
struct rpmlead_s {
|
|
|
|
unsigned char magic[4];
|
|
|
|
unsigned char major;
|
|
|
|
unsigned char minor;
|
|
|
|
short type;
|
|
|
|
short archnum;
|
|
|
|
char name[66];
|
|
|
|
short osnum;
|
|
|
|
short signature_type; /*!< Signature header type (RPMSIG_HEADERSIG) */
|
|
|
|
char reserved[16]; /*!< Pad to 96 bytes -- 8 byte aligned! */
|
|
|
|
};
|
|
|
|
|
|
|
|
rpmlead rpmLeadFromHeader(Header h)
|
|
|
|
{
|
2011-07-06 17:09:21 +08:00
|
|
|
rpmlead l = NULL;
|
|
|
|
|
|
|
|
if (h != NULL) {
|
|
|
|
int archnum, osnum;
|
|
|
|
char * nevr = headerGetAsString(h, RPMTAG_NEVR);
|
|
|
|
|
|
|
|
/* FIXME: should grab these from header instead (RhBug:717898) */
|
|
|
|
rpmGetArchInfo(NULL, &archnum);
|
|
|
|
rpmGetOsInfo(NULL, &osnum);
|
|
|
|
|
2011-07-06 17:42:56 +08:00
|
|
|
l = xcalloc(1, sizeof(*l));
|
2011-07-06 17:09:21 +08:00
|
|
|
l->major = 3;
|
|
|
|
l->minor = 0;
|
|
|
|
l->archnum = archnum;
|
|
|
|
l->osnum = osnum;
|
|
|
|
l->signature_type = RPMSIGTYPE_HEADERSIG;
|
|
|
|
l->type = (headerIsSource(h) ? 1 : 0);
|
|
|
|
|
|
|
|
memcpy(l->magic, lead_magic, sizeof(l->magic));
|
|
|
|
rstrlcpy(l->name, nevr, sizeof(l->name));
|
2007-12-02 00:31:09 +08:00
|
|
|
|
2011-07-06 17:09:21 +08:00
|
|
|
free(nevr);
|
|
|
|
}
|
1996-06-20 02:18:04 +08:00
|
|
|
|
2007-12-02 00:31:09 +08:00
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
rpmlead rpmLeadFree(rpmlead lead)
|
1996-01-05 11:05:34 +08:00
|
|
|
{
|
2007-12-02 00:31:09 +08:00
|
|
|
free(lead);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The lead needs to be 8 byte aligned */
|
|
|
|
rpmRC rpmLeadWrite(FD_t fd, rpmlead lead)
|
|
|
|
{
|
2011-07-06 17:53:59 +08:00
|
|
|
rpmRC rc = RPMRC_FAIL;
|
1996-01-05 11:05:34 +08:00
|
|
|
|
2011-07-06 17:53:59 +08:00
|
|
|
if (lead != NULL) {
|
|
|
|
struct rpmlead_s l;
|
|
|
|
memcpy(&l, lead, sizeof(l));
|
|
|
|
|
|
|
|
l.type = htons(lead->type);
|
|
|
|
l.archnum = htons(lead->archnum);
|
|
|
|
l.osnum = htons(lead->osnum);
|
|
|
|
l.signature_type = htons(lead->signature_type);
|
|
|
|
|
|
|
|
if (Fwrite(&l, 1, sizeof(l), fd) == sizeof(l))
|
|
|
|
rc = RPMRC_OK;
|
|
|
|
}
|
|
|
|
return rc;
|
1996-01-05 11:05:34 +08:00
|
|
|
}
|
|
|
|
|
2011-07-07 16:55:28 +08:00
|
|
|
static rpmRC rpmLeadCheck(rpmlead lead, char **msg)
|
1996-01-05 11:05:34 +08:00
|
|
|
{
|
2007-12-02 00:31:09 +08:00
|
|
|
if (memcmp(lead->magic, lead_magic, sizeof(lead_magic))) {
|
2011-07-07 16:55:28 +08:00
|
|
|
*msg = xstrdup(_("not an rpm package"));
|
2007-12-02 00:31:09 +08:00
|
|
|
return RPMRC_NOTFOUND;
|
|
|
|
}
|
|
|
|
if (lead->signature_type != RPMSIGTYPE_HEADERSIG) {
|
2011-07-07 16:55:28 +08:00
|
|
|
*msg = xstrdup(_("illegal signature type"));
|
2007-12-02 00:31:09 +08:00
|
|
|
return RPMRC_FAIL;
|
|
|
|
}
|
|
|
|
if (lead->major < 3 || lead->major > 4) {
|
2011-07-07 16:55:28 +08:00
|
|
|
*msg = xstrdup(_("unsupported RPM package version"));
|
2007-12-02 00:31:09 +08:00
|
|
|
return RPMRC_FAIL;
|
|
|
|
}
|
|
|
|
return RPMRC_OK;
|
|
|
|
}
|
|
|
|
|
2011-07-07 16:55:28 +08:00
|
|
|
rpmRC rpmLeadRead(FD_t fd, rpmlead *lead, int *type, char **emsg)
|
2007-12-02 00:31:09 +08:00
|
|
|
{
|
2011-07-06 17:42:56 +08:00
|
|
|
rpmRC rc = RPMRC_OK;
|
2011-07-07 16:17:29 +08:00
|
|
|
struct rpmlead_s l;
|
2011-07-07 16:55:28 +08:00
|
|
|
char *err = NULL;
|
2011-07-06 17:42:56 +08:00
|
|
|
|
2011-07-07 16:17:29 +08:00
|
|
|
memset(&l, 0, sizeof(l));
|
2012-10-30 17:36:56 +08:00
|
|
|
if (Freadall(fd, &l, sizeof(l)) != sizeof(l)) {
|
2003-04-17 01:48:04 +08:00
|
|
|
if (Ferror(fd)) {
|
2011-07-07 16:55:28 +08:00
|
|
|
rasprintf(&err, _("read failed: %s (%d)\n"), Fstrerror(fd), errno);
|
2011-07-06 17:42:56 +08:00
|
|
|
rc = RPMRC_FAIL;
|
2009-07-14 19:25:58 +08:00
|
|
|
} else {
|
2011-07-07 16:55:28 +08:00
|
|
|
err = xstrdup(_("not an rpm package\n"));
|
2011-07-06 17:42:56 +08:00
|
|
|
rc = RPMRC_NOTFOUND;
|
2003-04-17 01:48:04 +08:00
|
|
|
}
|
2011-07-07 16:17:29 +08:00
|
|
|
} else {
|
|
|
|
l.type = ntohs(l.type);
|
|
|
|
l.archnum = ntohs(l.archnum);
|
|
|
|
l.osnum = ntohs(l.osnum);
|
|
|
|
l.signature_type = ntohs(l.signature_type);
|
2011-07-07 16:55:28 +08:00
|
|
|
rc = rpmLeadCheck(&l, &err);
|
1996-02-15 01:55:28 +08:00
|
|
|
}
|
1996-01-05 11:05:34 +08:00
|
|
|
|
2011-07-07 16:55:28 +08:00
|
|
|
if (rc == RPMRC_OK) {
|
|
|
|
if (lead != NULL) {
|
|
|
|
*lead = xmalloc(sizeof(l));
|
|
|
|
memcpy(*lead, &l, sizeof(l));
|
|
|
|
}
|
|
|
|
if (type != NULL)
|
|
|
|
*type = l.type;
|
|
|
|
} else {
|
|
|
|
if (emsg != NULL)
|
|
|
|
*emsg = err;
|
|
|
|
else
|
|
|
|
free(err);
|
2011-07-06 17:42:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
1996-01-05 11:05:34 +08:00
|
|
|
}
|