2013-03-05 13:31:06 +00:00
|
|
|
/*-
|
2014-02-23 22:04:03 +00:00
|
|
|
* Copyright (c) 2014 Baptiste Daroussin <bapt@FreeBSD.org>
|
2013-10-26 03:43:02 +00:00
|
|
|
* Copyright (c) 2013 Bryan Drewery <bdrewery@FreeBSD.org>
|
2013-03-05 13:31:06 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
2014-01-20 22:54:11 +00:00
|
|
|
#include <sys/queue.h>
|
2013-03-05 13:31:06 +00:00
|
|
|
#include <sys/sbuf.h>
|
|
|
|
#include <sys/elf_common.h>
|
|
|
|
#include <sys/endian.h>
|
2013-12-12 17:59:09 +00:00
|
|
|
#include <sys/types.h>
|
2013-03-05 13:31:06 +00:00
|
|
|
|
2013-09-10 20:56:01 +00:00
|
|
|
#include <assert.h>
|
2013-12-12 17:59:09 +00:00
|
|
|
#include <dirent.h>
|
2014-02-23 21:55:07 +00:00
|
|
|
#include <ucl.h>
|
2013-03-05 13:31:06 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <gelf.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <paths.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "elf_tables.h"
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#define roundup2(x, y) (((x)+((y)-1))&(~((y)-1))) /* if y is powers of two */
|
|
|
|
|
2013-12-12 17:59:09 +00:00
|
|
|
struct config_value {
|
|
|
|
char *value;
|
|
|
|
STAILQ_ENTRY(config_value) next;
|
|
|
|
};
|
|
|
|
|
2013-03-05 13:31:06 +00:00
|
|
|
struct config_entry {
|
|
|
|
uint8_t type;
|
|
|
|
const char *key;
|
|
|
|
const char *val;
|
|
|
|
char *value;
|
2013-12-12 17:59:09 +00:00
|
|
|
STAILQ_HEAD(, config_value) *list;
|
2013-03-05 13:31:06 +00:00
|
|
|
bool envset;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct config_entry c[] = {
|
|
|
|
[PACKAGESITE] = {
|
|
|
|
PKG_CONFIG_STRING,
|
|
|
|
"PACKAGESITE",
|
2013-10-24 10:49:55 +00:00
|
|
|
URL_SCHEME_PREFIX "http://pkg.FreeBSD.org/${ABI}/latest",
|
2013-03-05 13:31:06 +00:00
|
|
|
NULL,
|
2013-12-12 17:59:09 +00:00
|
|
|
NULL,
|
2013-03-05 13:31:06 +00:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
[ABI] = {
|
|
|
|
PKG_CONFIG_STRING,
|
|
|
|
"ABI",
|
|
|
|
NULL,
|
|
|
|
NULL,
|
2013-12-12 17:59:09 +00:00
|
|
|
NULL,
|
2013-03-05 13:31:06 +00:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
[MIRROR_TYPE] = {
|
|
|
|
PKG_CONFIG_STRING,
|
|
|
|
"MIRROR_TYPE",
|
|
|
|
"SRV",
|
|
|
|
NULL,
|
2013-12-12 17:59:09 +00:00
|
|
|
NULL,
|
2013-03-05 13:31:06 +00:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
[ASSUME_ALWAYS_YES] = {
|
|
|
|
PKG_CONFIG_BOOL,
|
|
|
|
"ASSUME_ALWAYS_YES",
|
|
|
|
"NO",
|
|
|
|
NULL,
|
2013-12-12 17:59:09 +00:00
|
|
|
NULL,
|
2013-03-05 13:31:06 +00:00
|
|
|
false,
|
2013-10-26 03:43:02 +00:00
|
|
|
},
|
|
|
|
[SIGNATURE_TYPE] = {
|
|
|
|
PKG_CONFIG_STRING,
|
|
|
|
"SIGNATURE_TYPE",
|
|
|
|
NULL,
|
|
|
|
NULL,
|
2013-12-12 17:59:09 +00:00
|
|
|
NULL,
|
2013-10-26 03:43:02 +00:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
[FINGERPRINTS] = {
|
|
|
|
PKG_CONFIG_STRING,
|
|
|
|
"FINGERPRINTS",
|
|
|
|
NULL,
|
|
|
|
NULL,
|
2013-12-12 17:59:09 +00:00
|
|
|
NULL,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
[REPOS_DIR] = {
|
|
|
|
PKG_CONFIG_LIST,
|
|
|
|
"REPOS_DIR",
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
2013-10-26 03:43:02 +00:00
|
|
|
false,
|
|
|
|
},
|
2013-03-05 13:31:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
elf_corres_to_string(struct _elf_corres *m, int e)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; m[i].string != NULL; i++)
|
|
|
|
if (m[i].elf_nb == e)
|
|
|
|
return (m[i].string);
|
|
|
|
|
|
|
|
return ("unknown");
|
|
|
|
}
|
|
|
|
|
2013-09-10 20:56:01 +00:00
|
|
|
static const char *
|
|
|
|
aeabi_parse_arm_attributes(void *data, size_t length)
|
|
|
|
{
|
|
|
|
uint32_t sect_len;
|
|
|
|
uint8_t *section = data;
|
|
|
|
|
|
|
|
#define MOVE(len) do { \
|
|
|
|
assert(length >= (len)); \
|
|
|
|
section += (len); \
|
|
|
|
length -= (len); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
if (length == 0 || *section != 'A')
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
MOVE(1);
|
|
|
|
|
|
|
|
/* Read the section length */
|
|
|
|
if (length < sizeof(sect_len))
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
memcpy(§_len, section, sizeof(sect_len));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The section length should be no longer than the section it is within
|
|
|
|
*/
|
|
|
|
if (sect_len > length)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
MOVE(sizeof(sect_len));
|
|
|
|
|
|
|
|
/* Skip the vendor name */
|
|
|
|
while (length != 0) {
|
|
|
|
if (*section == '\0')
|
|
|
|
break;
|
|
|
|
MOVE(1);
|
|
|
|
}
|
|
|
|
if (length == 0)
|
|
|
|
return (NULL);
|
|
|
|
MOVE(1);
|
|
|
|
|
|
|
|
while (length != 0) {
|
|
|
|
uint32_t tag_length;
|
|
|
|
|
|
|
|
switch(*section) {
|
|
|
|
case 1: /* Tag_File */
|
|
|
|
MOVE(1);
|
|
|
|
if (length < sizeof(tag_length))
|
|
|
|
return (NULL);
|
|
|
|
memcpy(&tag_length, section, sizeof(tag_length));
|
|
|
|
break;
|
|
|
|
case 2: /* Tag_Section */
|
|
|
|
case 3: /* Tag_Symbol */
|
|
|
|
default:
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
/* At least space for the tag and size */
|
|
|
|
if (tag_length <= 5)
|
|
|
|
return (NULL);
|
|
|
|
tag_length--;
|
|
|
|
/* Check the tag fits */
|
|
|
|
if (tag_length > length)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
#define MOVE_TAG(len) do { \
|
|
|
|
assert(tag_length >= (len)); \
|
|
|
|
MOVE(len); \
|
|
|
|
tag_length -= (len); \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
MOVE(sizeof(tag_length));
|
|
|
|
tag_length -= sizeof(tag_length);
|
|
|
|
|
|
|
|
while (tag_length != 0) {
|
|
|
|
uint8_t tag;
|
|
|
|
|
|
|
|
assert(tag_length >= length);
|
|
|
|
|
|
|
|
tag = *section;
|
|
|
|
MOVE_TAG(1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* These tag values come from:
|
|
|
|
*
|
|
|
|
* Addenda to, and Errata in, the ABI for the
|
|
|
|
* ARM Architecture. Release 2.08, section 2.3.
|
|
|
|
*/
|
|
|
|
if (tag == 6) { /* == Tag_CPU_arch */
|
|
|
|
uint8_t val;
|
|
|
|
|
|
|
|
val = *section;
|
|
|
|
/*
|
|
|
|
* We don't support values that require
|
|
|
|
* more than one byte.
|
|
|
|
*/
|
|
|
|
if (val & (1 << 7))
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
/* We have an ARMv4 or ARMv5 */
|
|
|
|
if (val <= 5)
|
|
|
|
return ("arm");
|
|
|
|
else /* We have an ARMv6+ */
|
|
|
|
return ("armv6");
|
|
|
|
} else if (tag == 4 || tag == 5 || tag == 32 ||
|
|
|
|
tag == 65 || tag == 67) {
|
|
|
|
while (*section != '\0' && length != 0)
|
|
|
|
MOVE_TAG(1);
|
|
|
|
if (tag_length == 0)
|
|
|
|
return (NULL);
|
|
|
|
/* Skip the last byte */
|
|
|
|
MOVE_TAG(1);
|
|
|
|
} else if ((tag >= 7 && tag <= 31) || tag == 34 ||
|
|
|
|
tag == 36 || tag == 38 || tag == 42 || tag == 44 ||
|
|
|
|
tag == 64 || tag == 66 || tag == 68 || tag == 70) {
|
|
|
|
/* Skip the uleb128 data */
|
|
|
|
while (*section & (1 << 7) && length != 0)
|
|
|
|
MOVE_TAG(1);
|
|
|
|
if (tag_length == 0)
|
|
|
|
return (NULL);
|
|
|
|
/* Skip the last byte */
|
|
|
|
MOVE_TAG(1);
|
|
|
|
} else
|
|
|
|
return (NULL);
|
|
|
|
#undef MOVE_TAG
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
#undef MOVE
|
|
|
|
}
|
|
|
|
|
2013-03-05 13:31:06 +00:00
|
|
|
static int
|
|
|
|
pkg_get_myabi(char *dest, size_t sz)
|
|
|
|
{
|
|
|
|
Elf *elf;
|
|
|
|
Elf_Data *data;
|
|
|
|
Elf_Note note;
|
|
|
|
Elf_Scn *scn;
|
|
|
|
char *src, *osname;
|
2013-09-10 20:56:01 +00:00
|
|
|
const char *arch, *abi, *fpu, *endian_corres_str;
|
|
|
|
const char *wordsize_corres_str;
|
2013-03-05 13:31:06 +00:00
|
|
|
GElf_Ehdr elfhdr;
|
|
|
|
GElf_Shdr shdr;
|
|
|
|
int fd, i, ret;
|
|
|
|
uint32_t version;
|
|
|
|
|
|
|
|
version = 0;
|
|
|
|
ret = -1;
|
|
|
|
scn = NULL;
|
|
|
|
abi = NULL;
|
|
|
|
|
|
|
|
if (elf_version(EV_CURRENT) == EV_NONE) {
|
|
|
|
warnx("ELF library initialization failed: %s",
|
|
|
|
elf_errmsg(-1));
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((fd = open(_PATH_BSHELL, O_RDONLY)) < 0) {
|
|
|
|
warn("open()");
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
|
|
|
|
ret = -1;
|
|
|
|
warnx("elf_begin() failed: %s.", elf_errmsg(-1));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gelf_getehdr(elf, &elfhdr) == NULL) {
|
|
|
|
ret = -1;
|
|
|
|
warn("getehdr() failed: %s.", elf_errmsg(-1));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
while ((scn = elf_nextscn(elf, scn)) != NULL) {
|
|
|
|
if (gelf_getshdr(scn, &shdr) != &shdr) {
|
|
|
|
ret = -1;
|
|
|
|
warn("getshdr() failed: %s.", elf_errmsg(-1));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shdr.sh_type == SHT_NOTE)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (scn == NULL) {
|
|
|
|
ret = -1;
|
|
|
|
warn("failed to get the note section");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
data = elf_getdata(scn, NULL);
|
|
|
|
src = data->d_buf;
|
|
|
|
for (;;) {
|
|
|
|
memcpy(¬e, src, sizeof(Elf_Note));
|
|
|
|
src += sizeof(Elf_Note);
|
|
|
|
if (note.n_type == NT_VERSION)
|
|
|
|
break;
|
|
|
|
src += note.n_namesz + note.n_descsz;
|
|
|
|
}
|
|
|
|
osname = src;
|
|
|
|
src += roundup2(note.n_namesz, 4);
|
|
|
|
if (elfhdr.e_ident[EI_DATA] == ELFDATA2MSB)
|
|
|
|
version = be32dec(src);
|
|
|
|
else
|
|
|
|
version = le32dec(src);
|
|
|
|
|
|
|
|
for (i = 0; osname[i] != '\0'; i++)
|
|
|
|
osname[i] = (char)tolower(osname[i]);
|
|
|
|
|
2013-09-10 20:56:01 +00:00
|
|
|
wordsize_corres_str = elf_corres_to_string(wordsize_corres,
|
|
|
|
(int)elfhdr.e_ident[EI_CLASS]);
|
|
|
|
|
|
|
|
arch = elf_corres_to_string(mach_corres, (int) elfhdr.e_machine);
|
|
|
|
|
|
|
|
snprintf(dest, sz, "%s:%d",
|
|
|
|
osname, version / 100000);
|
2013-03-05 13:31:06 +00:00
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
switch (elfhdr.e_machine) {
|
|
|
|
case EM_ARM:
|
2013-09-10 20:56:01 +00:00
|
|
|
endian_corres_str = elf_corres_to_string(endian_corres,
|
|
|
|
(int)elfhdr.e_ident[EI_DATA]);
|
|
|
|
|
2013-07-28 20:11:31 +00:00
|
|
|
/* FreeBSD doesn't support the hard-float ABI yet */
|
|
|
|
fpu = "softfp";
|
|
|
|
if ((elfhdr.e_flags & 0xFF000000) != 0) {
|
2013-09-10 20:56:01 +00:00
|
|
|
const char *sh_name = NULL;
|
|
|
|
size_t shstrndx;
|
|
|
|
|
2013-07-28 20:11:31 +00:00
|
|
|
/* This is an EABI file, the conformance level is set */
|
|
|
|
abi = "eabi";
|
2013-09-10 20:56:01 +00:00
|
|
|
/* Find which TARGET_ARCH we are building for. */
|
|
|
|
elf_getshdrstrndx(elf, &shstrndx);
|
|
|
|
while ((scn = elf_nextscn(elf, scn)) != NULL) {
|
|
|
|
sh_name = NULL;
|
|
|
|
if (gelf_getshdr(scn, &shdr) != &shdr) {
|
|
|
|
scn = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
sh_name = elf_strptr(elf, shstrndx,
|
|
|
|
shdr.sh_name);
|
|
|
|
if (sh_name == NULL)
|
|
|
|
continue;
|
|
|
|
if (strcmp(".ARM.attributes", sh_name) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (scn != NULL && sh_name != NULL) {
|
|
|
|
data = elf_getdata(scn, NULL);
|
|
|
|
/*
|
|
|
|
* Prior to FreeBSD 10.0 libelf would return
|
|
|
|
* NULL from elf_getdata on the .ARM.attributes
|
|
|
|
* section. As this was the first release to
|
|
|
|
* get armv6 support assume a NULL value means
|
|
|
|
* arm.
|
|
|
|
*
|
|
|
|
* This assumption can be removed when 9.x
|
|
|
|
* is unsupported.
|
|
|
|
*/
|
|
|
|
if (data != NULL) {
|
|
|
|
arch = aeabi_parse_arm_attributes(
|
|
|
|
data->d_buf, data->d_size);
|
|
|
|
if (arch == NULL) {
|
|
|
|
ret = 1;
|
|
|
|
warn("unknown ARM ARCH");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ret = 1;
|
|
|
|
warn("Unable to find the .ARM.attributes "
|
|
|
|
"section");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2013-07-28 20:11:31 +00:00
|
|
|
} else if (elfhdr.e_ident[EI_OSABI] != ELFOSABI_NONE) {
|
|
|
|
/*
|
|
|
|
* EABI executables all have this field set to
|
|
|
|
* ELFOSABI_NONE, therefore it must be an oabi file.
|
|
|
|
*/
|
|
|
|
abi = "oabi";
|
|
|
|
} else {
|
|
|
|
ret = 1;
|
2013-09-10 20:56:01 +00:00
|
|
|
warn("unknown ARM ABI");
|
2013-07-28 20:11:31 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
2013-03-05 13:31:06 +00:00
|
|
|
snprintf(dest + strlen(dest), sz - strlen(dest),
|
2013-09-10 20:56:01 +00:00
|
|
|
":%s:%s:%s:%s:%s", arch, wordsize_corres_str,
|
|
|
|
endian_corres_str, abi, fpu);
|
2013-03-05 13:31:06 +00:00
|
|
|
break;
|
|
|
|
case EM_MIPS:
|
|
|
|
/*
|
|
|
|
* this is taken from binutils sources:
|
|
|
|
* include/elf/mips.h
|
|
|
|
* mapping is figured out from binutils:
|
|
|
|
* gas/config/tc-mips.c
|
|
|
|
*/
|
|
|
|
switch (elfhdr.e_flags & EF_MIPS_ABI) {
|
|
|
|
case E_MIPS_ABI_O32:
|
|
|
|
abi = "o32";
|
|
|
|
break;
|
|
|
|
case E_MIPS_ABI_N32:
|
|
|
|
abi = "n32";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (elfhdr.e_ident[EI_DATA] ==
|
|
|
|
ELFCLASS32)
|
|
|
|
abi = "o32";
|
|
|
|
else if (elfhdr.e_ident[EI_DATA] ==
|
|
|
|
ELFCLASS64)
|
|
|
|
abi = "n64";
|
|
|
|
break;
|
|
|
|
}
|
2013-09-10 20:56:01 +00:00
|
|
|
endian_corres_str = elf_corres_to_string(endian_corres,
|
|
|
|
(int)elfhdr.e_ident[EI_DATA]);
|
|
|
|
|
|
|
|
snprintf(dest + strlen(dest), sz - strlen(dest), ":%s:%s:%s:%s",
|
|
|
|
arch, wordsize_corres_str, endian_corres_str, abi);
|
2013-03-05 13:31:06 +00:00
|
|
|
break;
|
2013-09-10 20:56:01 +00:00
|
|
|
default:
|
|
|
|
snprintf(dest + strlen(dest), sz - strlen(dest), ":%s:%s",
|
|
|
|
arch, wordsize_corres_str);
|
2013-03-05 13:31:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
if (elf != NULL)
|
|
|
|
elf_end(elf);
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
subst_packagesite(const char *abi)
|
|
|
|
{
|
|
|
|
struct sbuf *newval;
|
|
|
|
const char *variable_string;
|
|
|
|
const char *oldval;
|
|
|
|
|
|
|
|
if (c[PACKAGESITE].value != NULL)
|
|
|
|
oldval = c[PACKAGESITE].value;
|
|
|
|
else
|
|
|
|
oldval = c[PACKAGESITE].val;
|
|
|
|
|
|
|
|
if ((variable_string = strstr(oldval, "${ABI}")) == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
newval = sbuf_new_auto();
|
|
|
|
sbuf_bcat(newval, oldval, variable_string - oldval);
|
|
|
|
sbuf_cat(newval, abi);
|
|
|
|
sbuf_cat(newval, variable_string + strlen("${ABI}"));
|
|
|
|
sbuf_finish(newval);
|
|
|
|
|
|
|
|
free(c[PACKAGESITE].value);
|
|
|
|
c[PACKAGESITE].value = strdup(sbuf_data(newval));
|
|
|
|
}
|
|
|
|
|
2013-12-12 17:59:09 +00:00
|
|
|
static int
|
|
|
|
boolstr_to_bool(const char *str)
|
|
|
|
{
|
|
|
|
if (str != NULL && (strcasecmp(str, "true") == 0 ||
|
|
|
|
strcasecmp(str, "yes") == 0 || strcasecmp(str, "on") == 0 ||
|
|
|
|
str[0] == '1'))
|
|
|
|
return (true);
|
|
|
|
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
|
2013-03-05 13:31:06 +00:00
|
|
|
static void
|
2014-02-23 21:55:07 +00:00
|
|
|
config_parse(ucl_object_t *obj, pkg_conf_file_t conftype)
|
2013-03-05 13:31:06 +00:00
|
|
|
{
|
|
|
|
struct sbuf *buf = sbuf_new_auto();
|
2014-02-23 21:55:07 +00:00
|
|
|
ucl_object_t *cur, *seq;
|
|
|
|
ucl_object_iter_t it = NULL, itseq = NULL;
|
2013-12-12 17:59:09 +00:00
|
|
|
struct config_entry *temp_config;
|
|
|
|
struct config_value *cv;
|
2014-02-23 21:55:07 +00:00
|
|
|
const char *key;
|
2013-03-05 13:31:06 +00:00
|
|
|
int i;
|
|
|
|
size_t j;
|
|
|
|
|
2013-12-12 17:59:09 +00:00
|
|
|
/* Temporary config for configs that may be disabled. */
|
|
|
|
temp_config = calloc(CONFIG_SIZE, sizeof(struct config_entry));
|
|
|
|
|
2014-02-23 21:55:07 +00:00
|
|
|
while ((cur = ucl_iterate_object(obj, &it, true))) {
|
|
|
|
key = ucl_object_key(cur);
|
|
|
|
if (key == NULL)
|
2013-03-05 13:31:06 +00:00
|
|
|
continue;
|
|
|
|
sbuf_clear(buf);
|
|
|
|
|
2013-10-26 03:31:05 +00:00
|
|
|
if (conftype == CONFFILE_PKG) {
|
2014-02-23 21:55:07 +00:00
|
|
|
for (j = 0; j < strlen(key); ++j)
|
|
|
|
sbuf_putc(buf, key[j]);
|
2013-10-26 03:31:05 +00:00
|
|
|
sbuf_finish(buf);
|
|
|
|
} else if (conftype == CONFFILE_REPO) {
|
2014-02-23 21:55:07 +00:00
|
|
|
if (strcasecmp(key, "url") == 0)
|
2013-10-26 03:31:05 +00:00
|
|
|
sbuf_cpy(buf, "PACKAGESITE");
|
2014-02-23 21:55:07 +00:00
|
|
|
else if (strcasecmp(key, "mirror_type") == 0)
|
2013-10-26 03:31:05 +00:00
|
|
|
sbuf_cpy(buf, "MIRROR_TYPE");
|
2014-02-23 21:55:07 +00:00
|
|
|
else if (strcasecmp(key, "signature_type") == 0)
|
2013-10-26 03:43:02 +00:00
|
|
|
sbuf_cpy(buf, "SIGNATURE_TYPE");
|
2014-02-23 21:55:07 +00:00
|
|
|
else if (strcasecmp(key, "fingerprints") == 0)
|
2013-10-26 03:43:02 +00:00
|
|
|
sbuf_cpy(buf, "FINGERPRINTS");
|
2014-02-23 21:55:07 +00:00
|
|
|
else if (strcasecmp(key, "enabled") == 0) {
|
|
|
|
if ((cur->type != UCL_BOOLEAN) ||
|
|
|
|
!ucl_object_toboolean(cur))
|
2013-12-12 17:59:09 +00:00
|
|
|
goto cleanup;
|
2014-02-23 21:55:07 +00:00
|
|
|
} else
|
2013-10-26 03:31:05 +00:00
|
|
|
continue;
|
|
|
|
sbuf_finish(buf);
|
|
|
|
}
|
|
|
|
|
2013-03-05 13:31:06 +00:00
|
|
|
for (i = 0; i < CONFIG_SIZE; i++) {
|
|
|
|
if (strcmp(sbuf_data(buf), c[i].key) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-10-26 03:31:05 +00:00
|
|
|
/* Silently skip unknown keys to be future compatible. */
|
2014-02-23 21:55:07 +00:00
|
|
|
if (i == CONFIG_SIZE)
|
2013-03-05 13:31:06 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* env has priority over config file */
|
2014-02-23 21:55:07 +00:00
|
|
|
if (c[i].envset)
|
2013-03-05 13:31:06 +00:00
|
|
|
continue;
|
|
|
|
|
2013-12-12 17:59:09 +00:00
|
|
|
/* Parse sequence value ["item1", "item2"] */
|
|
|
|
switch (c[i].type) {
|
|
|
|
case PKG_CONFIG_LIST:
|
2014-02-23 21:55:07 +00:00
|
|
|
if (cur->type != UCL_ARRAY) {
|
|
|
|
warnx("Skipping invalid array "
|
2013-12-12 17:59:09 +00:00
|
|
|
"value for %s.\n", c[i].key);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
temp_config[i].list =
|
|
|
|
malloc(sizeof(*temp_config[i].list));
|
|
|
|
STAILQ_INIT(temp_config[i].list);
|
|
|
|
|
2014-02-23 21:55:07 +00:00
|
|
|
while ((seq = ucl_iterate_object(cur, &itseq, true))) {
|
|
|
|
if (seq->type != UCL_STRING)
|
2013-12-12 17:59:09 +00:00
|
|
|
continue;
|
|
|
|
cv = malloc(sizeof(struct config_value));
|
|
|
|
cv->value =
|
2014-02-23 21:55:07 +00:00
|
|
|
strdup(ucl_object_tostring(seq));
|
2013-12-12 17:59:09 +00:00
|
|
|
STAILQ_INSERT_TAIL(temp_config[i].list, cv,
|
|
|
|
next);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* Normal string value. */
|
2014-02-23 21:55:07 +00:00
|
|
|
temp_config[i].value = strdup(ucl_object_tostring(cur));
|
2013-12-12 17:59:09 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-03-05 13:31:06 +00:00
|
|
|
}
|
|
|
|
|
2013-12-12 17:59:09 +00:00
|
|
|
/* Repo is enabled, copy over all settings from temp_config. */
|
|
|
|
for (i = 0; i < CONFIG_SIZE; i++) {
|
|
|
|
if (c[i].envset)
|
|
|
|
continue;
|
|
|
|
switch (c[i].type) {
|
|
|
|
case PKG_CONFIG_LIST:
|
|
|
|
c[i].list = temp_config[i].list;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
c[i].value = temp_config[i].value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
free(temp_config);
|
2013-03-05 13:31:06 +00:00
|
|
|
sbuf_delete(buf);
|
|
|
|
}
|
|
|
|
|
2013-10-26 03:31:05 +00:00
|
|
|
/*-
|
|
|
|
* Parse new repo style configs in style:
|
|
|
|
* Name:
|
|
|
|
* URL:
|
|
|
|
* MIRROR_TYPE:
|
|
|
|
* etc...
|
|
|
|
*/
|
|
|
|
static void
|
2014-02-23 21:55:07 +00:00
|
|
|
parse_repo_file(ucl_object_t *obj)
|
2013-03-05 13:31:06 +00:00
|
|
|
{
|
2014-02-23 21:55:07 +00:00
|
|
|
ucl_object_iter_t it = NULL;
|
|
|
|
ucl_object_t *cur;
|
|
|
|
const char *key;
|
2013-03-05 13:31:06 +00:00
|
|
|
|
2014-02-23 21:55:07 +00:00
|
|
|
while ((cur = ucl_iterate_object(obj, &it, true))) {
|
|
|
|
key = ucl_object_key(cur);
|
2013-10-26 03:31:05 +00:00
|
|
|
|
2014-02-23 21:55:07 +00:00
|
|
|
if (key == NULL)
|
2013-10-26 03:31:05 +00:00
|
|
|
continue;
|
|
|
|
|
2014-02-23 21:55:07 +00:00
|
|
|
if (cur->type != UCL_OBJECT)
|
2013-10-26 03:31:05 +00:00
|
|
|
continue;
|
|
|
|
|
2014-02-23 21:55:07 +00:00
|
|
|
config_parse(cur, CONFFILE_REPO);
|
2013-03-05 13:31:06 +00:00
|
|
|
}
|
2013-10-26 03:31:05 +00:00
|
|
|
}
|
2013-03-05 13:31:06 +00:00
|
|
|
|
2013-10-26 03:31:05 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
read_conf_file(const char *confpath, pkg_conf_file_t conftype)
|
|
|
|
{
|
2014-02-23 21:55:07 +00:00
|
|
|
struct ucl_parser *p;
|
|
|
|
ucl_object_t *obj = NULL;
|
2013-03-05 13:31:06 +00:00
|
|
|
|
2014-02-23 21:55:07 +00:00
|
|
|
p = ucl_parser_new(0);
|
|
|
|
|
|
|
|
if (!ucl_parser_add_file(p, confpath)) {
|
2013-03-05 13:31:06 +00:00
|
|
|
if (errno != ENOENT)
|
2014-02-23 21:55:07 +00:00
|
|
|
errx(EXIT_FAILURE, "Unable to parse configuration "
|
|
|
|
"file %s: %s", confpath, ucl_parser_get_error(p));
|
|
|
|
ucl_parser_free(p);
|
2013-03-05 13:31:06 +00:00
|
|
|
/* no configuration present */
|
2013-10-26 03:31:05 +00:00
|
|
|
return (1);
|
2013-03-05 13:31:06 +00:00
|
|
|
}
|
|
|
|
|
2014-02-23 21:55:07 +00:00
|
|
|
obj = ucl_parser_get_object(p);
|
|
|
|
if (obj->type != UCL_OBJECT)
|
2013-10-26 03:21:08 +00:00
|
|
|
warnx("Invalid configuration format, ignoring the "
|
2013-10-26 03:31:05 +00:00
|
|
|
"configuration file %s", confpath);
|
|
|
|
else {
|
|
|
|
if (conftype == CONFFILE_PKG)
|
2014-02-23 21:55:07 +00:00
|
|
|
config_parse(obj, conftype);
|
2013-10-26 03:31:05 +00:00
|
|
|
else if (conftype == CONFFILE_REPO)
|
2014-02-23 21:55:07 +00:00
|
|
|
parse_repo_file(obj);
|
2013-03-05 13:31:06 +00:00
|
|
|
}
|
|
|
|
|
2014-02-23 21:55:07 +00:00
|
|
|
ucl_object_free(obj);
|
|
|
|
ucl_parser_free(p);
|
2013-03-05 13:31:06 +00:00
|
|
|
|
2013-10-26 03:31:05 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2013-12-12 17:59:09 +00:00
|
|
|
static int
|
|
|
|
load_repositories(const char *repodir)
|
|
|
|
{
|
|
|
|
struct dirent *ent;
|
|
|
|
DIR *d;
|
|
|
|
char *p;
|
|
|
|
size_t n;
|
|
|
|
char path[MAXPATHLEN];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
if ((d = opendir(repodir)) == NULL)
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
while ((ent = readdir(d))) {
|
|
|
|
/* Trim out 'repos'. */
|
|
|
|
if ((n = strlen(ent->d_name)) <= 5)
|
|
|
|
continue;
|
|
|
|
p = &ent->d_name[n - 5];
|
|
|
|
if (strcmp(p, ".conf") == 0) {
|
|
|
|
snprintf(path, sizeof(path), "%s%s%s",
|
|
|
|
repodir,
|
|
|
|
repodir[strlen(repodir) - 1] == '/' ? "" : "/",
|
|
|
|
ent->d_name);
|
|
|
|
if (access(path, F_OK) == 0 &&
|
|
|
|
read_conf_file(path, CONFFILE_REPO)) {
|
|
|
|
ret = 1;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
closedir(d);
|
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2013-10-26 03:31:05 +00:00
|
|
|
int
|
|
|
|
config_init(void)
|
|
|
|
{
|
2013-12-12 17:59:09 +00:00
|
|
|
char *val;
|
2013-10-26 03:31:05 +00:00
|
|
|
int i;
|
|
|
|
const char *localbase;
|
2013-12-12 17:59:09 +00:00
|
|
|
char *env_list_item;
|
2013-10-26 03:31:05 +00:00
|
|
|
char confpath[MAXPATHLEN];
|
2013-12-12 17:59:09 +00:00
|
|
|
struct config_value *cv;
|
2013-10-26 03:31:05 +00:00
|
|
|
char abi[BUFSIZ];
|
|
|
|
|
|
|
|
for (i = 0; i < CONFIG_SIZE; i++) {
|
|
|
|
val = getenv(c[i].key);
|
|
|
|
if (val != NULL) {
|
|
|
|
c[i].envset = true;
|
2013-12-12 17:59:09 +00:00
|
|
|
switch (c[i].type) {
|
|
|
|
case PKG_CONFIG_LIST:
|
|
|
|
/* Split up comma-separated items from env. */
|
|
|
|
c[i].list = malloc(sizeof(*c[i].list));
|
|
|
|
STAILQ_INIT(c[i].list);
|
|
|
|
for (env_list_item = strtok(val, ",");
|
|
|
|
env_list_item != NULL;
|
|
|
|
env_list_item = strtok(NULL, ",")) {
|
|
|
|
cv =
|
|
|
|
malloc(sizeof(struct config_value));
|
|
|
|
cv->value =
|
|
|
|
strdup(env_list_item);
|
|
|
|
STAILQ_INSERT_TAIL(c[i].list, cv,
|
|
|
|
next);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
c[i].val = val;
|
|
|
|
break;
|
|
|
|
}
|
2013-10-26 03:31:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-12 17:59:09 +00:00
|
|
|
/* Read LOCALBASE/etc/pkg.conf first. */
|
2013-10-26 03:31:05 +00:00
|
|
|
localbase = getenv("LOCALBASE") ? getenv("LOCALBASE") : _LOCALBASE;
|
|
|
|
snprintf(confpath, sizeof(confpath), "%s/etc/pkg.conf",
|
|
|
|
localbase);
|
|
|
|
|
|
|
|
if (access(confpath, F_OK) == 0 && read_conf_file(confpath,
|
|
|
|
CONFFILE_PKG))
|
|
|
|
goto finalize;
|
|
|
|
|
2013-12-12 17:59:09 +00:00
|
|
|
/* Then read in all repos from REPOS_DIR list of directories. */
|
|
|
|
if (c[REPOS_DIR].list == NULL) {
|
|
|
|
c[REPOS_DIR].list = malloc(sizeof(*c[REPOS_DIR].list));
|
|
|
|
STAILQ_INIT(c[REPOS_DIR].list);
|
|
|
|
cv = malloc(sizeof(struct config_value));
|
|
|
|
cv->value = strdup("/etc/pkg");
|
|
|
|
STAILQ_INSERT_TAIL(c[REPOS_DIR].list, cv, next);
|
|
|
|
cv = malloc(sizeof(struct config_value));
|
|
|
|
if (asprintf(&cv->value, "%s/etc/pkg/repos", localbase) < 0)
|
|
|
|
goto finalize;
|
|
|
|
STAILQ_INSERT_TAIL(c[REPOS_DIR].list, cv, next);
|
|
|
|
}
|
|
|
|
|
|
|
|
STAILQ_FOREACH(cv, c[REPOS_DIR].list, next)
|
|
|
|
if (load_repositories(cv->value))
|
|
|
|
goto finalize;
|
2013-10-26 03:31:05 +00:00
|
|
|
|
2013-03-05 13:31:06 +00:00
|
|
|
finalize:
|
|
|
|
if (c[ABI].val == NULL && c[ABI].value == NULL) {
|
|
|
|
if (pkg_get_myabi(abi, BUFSIZ) != 0)
|
2013-10-26 03:21:08 +00:00
|
|
|
errx(EXIT_FAILURE, "Failed to determine the system "
|
|
|
|
"ABI");
|
2013-03-05 13:31:06 +00:00
|
|
|
c[ABI].val = abi;
|
|
|
|
}
|
|
|
|
|
2013-03-05 14:08:36 +00:00
|
|
|
subst_packagesite(c[ABI].value != NULL ? c[ABI].value : c[ABI].val);
|
2013-03-05 13:31:06 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
config_string(pkg_config_key k, const char **val)
|
|
|
|
{
|
|
|
|
if (c[k].type != PKG_CONFIG_STRING)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
if (c[k].value != NULL)
|
|
|
|
*val = c[k].value;
|
|
|
|
else
|
|
|
|
*val = c[k].val;
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
config_bool(pkg_config_key k, bool *val)
|
|
|
|
{
|
|
|
|
const char *value;
|
|
|
|
|
|
|
|
if (c[k].type != PKG_CONFIG_BOOL)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
*val = false;
|
|
|
|
|
|
|
|
if (c[k].value != NULL)
|
|
|
|
value = c[k].value;
|
|
|
|
else
|
|
|
|
value = c[k].val;
|
|
|
|
|
2013-12-12 17:59:09 +00:00
|
|
|
if (boolstr_to_bool(value))
|
2013-03-05 13:31:06 +00:00
|
|
|
*val = true;
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
config_finish(void) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < CONFIG_SIZE; i++)
|
|
|
|
free(c[i].value);
|
|
|
|
}
|