config.c revision 262400
1247841Sbapt/*-
2247841Sbapt * Copyright (c) 2013 Baptiste Daroussin <bapt@FreeBSD.org>
3257147Sbdrewery * Copyright (c) 2013 Bryan Drewery <bdrewery@FreeBSD.org>
4247841Sbapt * All rights reserved.
5247841Sbapt *
6247841Sbapt * Redistribution and use in source and binary forms, with or without
7247841Sbapt * modification, are permitted provided that the following conditions
8247841Sbapt * are met:
9247841Sbapt * 1. Redistributions of source code must retain the above copyright
10247841Sbapt *    notice, this list of conditions and the following disclaimer.
11247841Sbapt * 2. Redistributions in binary form must reproduce the above copyright
12247841Sbapt *    notice, this list of conditions and the following disclaimer in the
13247841Sbapt *    documentation and/or other materials provided with the distribution.
14247841Sbapt *
15247841Sbapt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16247841Sbapt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17247841Sbapt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18247841Sbapt * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19247841Sbapt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20247841Sbapt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21247841Sbapt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22247841Sbapt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23247841Sbapt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24247841Sbapt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25247841Sbapt * SUCH DAMAGE.
26247841Sbapt */
27247841Sbapt
28247841Sbapt#include <sys/cdefs.h>
29247841Sbapt__FBSDID("$FreeBSD: head/usr.sbin/pkg/config.c 262400 2014-02-23 21:55:07Z bapt $");
30247841Sbapt
31247841Sbapt#include <sys/param.h>
32260942Sbapt#include <sys/queue.h>
33247841Sbapt#include <sys/sbuf.h>
34247841Sbapt#include <sys/elf_common.h>
35247841Sbapt#include <sys/endian.h>
36259266Sbdrewery#include <sys/types.h>
37247841Sbapt
38255457Sbapt#include <assert.h>
39259266Sbdrewery#include <dirent.h>
40262400Sbapt#include <ucl.h>
41247841Sbapt#include <ctype.h>
42247841Sbapt#include <err.h>
43247841Sbapt#include <errno.h>
44247841Sbapt#include <fcntl.h>
45247841Sbapt#include <gelf.h>
46247841Sbapt#include <inttypes.h>
47247841Sbapt#include <paths.h>
48247841Sbapt#include <stdbool.h>
49247841Sbapt#include <string.h>
50247841Sbapt#include <unistd.h>
51247841Sbapt
52247841Sbapt#include "elf_tables.h"
53247841Sbapt#include "config.h"
54247841Sbapt
55247841Sbapt#define roundup2(x, y)	(((x)+((y)-1))&(~((y)-1))) /* if y is powers of two */
56247841Sbapt
57259266Sbdrewerystruct config_value {
58259266Sbdrewery       char *value;
59259266Sbdrewery       STAILQ_ENTRY(config_value) next;
60259266Sbdrewery};
61259266Sbdrewery
62247841Sbaptstruct config_entry {
63247841Sbapt	uint8_t type;
64247841Sbapt	const char *key;
65247841Sbapt	const char *val;
66247841Sbapt	char *value;
67259266Sbdrewery	STAILQ_HEAD(, config_value) *list;
68247841Sbapt	bool envset;
69247841Sbapt};
70247841Sbapt
71247841Sbaptstatic struct config_entry c[] = {
72247841Sbapt	[PACKAGESITE] = {
73247841Sbapt		PKG_CONFIG_STRING,
74247841Sbapt		"PACKAGESITE",
75257051Sbdrewery		URL_SCHEME_PREFIX "http://pkg.FreeBSD.org/${ABI}/latest",
76247841Sbapt		NULL,
77259266Sbdrewery		NULL,
78247841Sbapt		false,
79247841Sbapt	},
80247841Sbapt	[ABI] = {
81247841Sbapt		PKG_CONFIG_STRING,
82247841Sbapt		"ABI",
83247841Sbapt		NULL,
84247841Sbapt		NULL,
85259266Sbdrewery		NULL,
86247841Sbapt		false,
87247841Sbapt	},
88247841Sbapt	[MIRROR_TYPE] = {
89247841Sbapt		PKG_CONFIG_STRING,
90247841Sbapt		"MIRROR_TYPE",
91247841Sbapt		"SRV",
92247841Sbapt		NULL,
93259266Sbdrewery		NULL,
94247841Sbapt		false,
95247841Sbapt	},
96247841Sbapt	[ASSUME_ALWAYS_YES] = {
97247841Sbapt		PKG_CONFIG_BOOL,
98247841Sbapt		"ASSUME_ALWAYS_YES",
99247841Sbapt		"NO",
100247841Sbapt		NULL,
101259266Sbdrewery		NULL,
102247841Sbapt		false,
103257147Sbdrewery	},
104257147Sbdrewery	[SIGNATURE_TYPE] = {
105257147Sbdrewery		PKG_CONFIG_STRING,
106257147Sbdrewery		"SIGNATURE_TYPE",
107257147Sbdrewery		NULL,
108257147Sbdrewery		NULL,
109259266Sbdrewery		NULL,
110257147Sbdrewery		false,
111257147Sbdrewery	},
112257147Sbdrewery	[FINGERPRINTS] = {
113257147Sbdrewery		PKG_CONFIG_STRING,
114257147Sbdrewery		"FINGERPRINTS",
115257147Sbdrewery		NULL,
116257147Sbdrewery		NULL,
117259266Sbdrewery		NULL,
118257147Sbdrewery		false,
119257147Sbdrewery	},
120259266Sbdrewery	[REPOS_DIR] = {
121259266Sbdrewery		PKG_CONFIG_LIST,
122259266Sbdrewery		"REPOS_DIR",
123259266Sbdrewery		NULL,
124259266Sbdrewery		NULL,
125259266Sbdrewery		NULL,
126259266Sbdrewery		false,
127259266Sbdrewery	},
128247841Sbapt};
129247841Sbapt
130247841Sbaptstatic const char *
131247841Sbaptelf_corres_to_string(struct _elf_corres *m, int e)
132247841Sbapt{
133247841Sbapt	int i;
134247841Sbapt
135247841Sbapt	for (i = 0; m[i].string != NULL; i++)
136247841Sbapt		if (m[i].elf_nb == e)
137247841Sbapt			return (m[i].string);
138247841Sbapt
139247841Sbapt	return ("unknown");
140247841Sbapt}
141247841Sbapt
142255457Sbaptstatic const char *
143255457Sbaptaeabi_parse_arm_attributes(void *data, size_t length)
144255457Sbapt{
145255457Sbapt	uint32_t sect_len;
146255457Sbapt	uint8_t *section = data;
147255457Sbapt
148255457Sbapt#define	MOVE(len) do {            \
149255457Sbapt	assert(length >= (len));  \
150255457Sbapt	section += (len);         \
151255457Sbapt	length -= (len);          \
152255457Sbapt} while (0)
153255457Sbapt
154255457Sbapt	if (length == 0 || *section != 'A')
155255457Sbapt		return (NULL);
156255457Sbapt
157255457Sbapt	MOVE(1);
158255457Sbapt
159255457Sbapt	/* Read the section length */
160255457Sbapt	if (length < sizeof(sect_len))
161255457Sbapt		return (NULL);
162255457Sbapt
163255457Sbapt	memcpy(&sect_len, section, sizeof(sect_len));
164255457Sbapt
165255457Sbapt	/*
166255457Sbapt	 * The section length should be no longer than the section it is within
167255457Sbapt	 */
168255457Sbapt	if (sect_len > length)
169255457Sbapt		return (NULL);
170255457Sbapt
171255457Sbapt	MOVE(sizeof(sect_len));
172255457Sbapt
173255457Sbapt	/* Skip the vendor name */
174255457Sbapt	while (length != 0) {
175255457Sbapt		if (*section == '\0')
176255457Sbapt			break;
177255457Sbapt		MOVE(1);
178255457Sbapt	}
179255457Sbapt	if (length == 0)
180255457Sbapt		return (NULL);
181255457Sbapt	MOVE(1);
182255457Sbapt
183255457Sbapt	while (length != 0) {
184255457Sbapt		uint32_t tag_length;
185255457Sbapt
186255457Sbapt	switch(*section) {
187255457Sbapt	case 1: /* Tag_File */
188255457Sbapt		MOVE(1);
189255457Sbapt		if (length < sizeof(tag_length))
190255457Sbapt			return (NULL);
191255457Sbapt		memcpy(&tag_length, section, sizeof(tag_length));
192255457Sbapt		break;
193255457Sbapt	case 2: /* Tag_Section */
194255457Sbapt	case 3: /* Tag_Symbol */
195255457Sbapt	default:
196255457Sbapt		return (NULL);
197255457Sbapt	}
198255457Sbapt	/* At least space for the tag and size */
199255457Sbapt	if (tag_length <= 5)
200255457Sbapt		return (NULL);
201255457Sbapt	tag_length--;
202255457Sbapt	/* Check the tag fits */
203255457Sbapt	if (tag_length > length)
204255457Sbapt		return (NULL);
205255457Sbapt
206255457Sbapt#define  MOVE_TAG(len) do {           \
207255457Sbapt	assert(tag_length >= (len));  \
208255457Sbapt	MOVE(len);                    \
209255457Sbapt	tag_length -= (len);          \
210255457Sbapt} while(0)
211255457Sbapt
212255457Sbapt		MOVE(sizeof(tag_length));
213255457Sbapt		tag_length -= sizeof(tag_length);
214255457Sbapt
215255457Sbapt		while (tag_length != 0) {
216255457Sbapt			uint8_t tag;
217255457Sbapt
218255457Sbapt			assert(tag_length >= length);
219255457Sbapt
220255457Sbapt			tag = *section;
221255457Sbapt			MOVE_TAG(1);
222255457Sbapt
223255457Sbapt			/*
224255457Sbapt			 * These tag values come from:
225255457Sbapt			 *
226255457Sbapt			 * Addenda to, and Errata in, the ABI for the
227255457Sbapt			 * ARM Architecture. Release 2.08, section 2.3.
228255457Sbapt			 */
229255457Sbapt			if (tag == 6) { /* == Tag_CPU_arch */
230255457Sbapt				uint8_t val;
231255457Sbapt
232255457Sbapt				val = *section;
233255457Sbapt				/*
234255457Sbapt				 * We don't support values that require
235255457Sbapt				 * more than one byte.
236255457Sbapt				 */
237255457Sbapt				if (val & (1 << 7))
238255457Sbapt					return (NULL);
239255457Sbapt
240255457Sbapt				/* We have an ARMv4 or ARMv5 */
241255457Sbapt				if (val <= 5)
242255457Sbapt					return ("arm");
243255457Sbapt				else /* We have an ARMv6+ */
244255457Sbapt					return ("armv6");
245255457Sbapt			} else if (tag == 4 || tag == 5 || tag == 32 ||
246255457Sbapt			    tag == 65 || tag == 67) {
247255457Sbapt				while (*section != '\0' && length != 0)
248255457Sbapt					MOVE_TAG(1);
249255457Sbapt				if (tag_length == 0)
250255457Sbapt					return (NULL);
251255457Sbapt				/* Skip the last byte */
252255457Sbapt				MOVE_TAG(1);
253255457Sbapt			} else if ((tag >= 7 && tag <= 31) || tag == 34 ||
254255457Sbapt			    tag == 36 || tag == 38 || tag == 42 || tag == 44 ||
255255457Sbapt			    tag == 64 || tag == 66 || tag == 68 || tag == 70) {
256255457Sbapt				/* Skip the uleb128 data */
257255457Sbapt				while (*section & (1 << 7) && length != 0)
258255457Sbapt					MOVE_TAG(1);
259255457Sbapt				if (tag_length == 0)
260255457Sbapt					return (NULL);
261255457Sbapt				/* Skip the last byte */
262255457Sbapt				MOVE_TAG(1);
263255457Sbapt			} else
264255457Sbapt				return (NULL);
265255457Sbapt#undef MOVE_TAG
266255457Sbapt		}
267255457Sbapt
268255457Sbapt		break;
269255457Sbapt	}
270255457Sbapt	return (NULL);
271255457Sbapt#undef MOVE
272255457Sbapt}
273255457Sbapt
274247841Sbaptstatic int
275247841Sbaptpkg_get_myabi(char *dest, size_t sz)
276247841Sbapt{
277247841Sbapt	Elf *elf;
278247841Sbapt	Elf_Data *data;
279247841Sbapt	Elf_Note note;
280247841Sbapt	Elf_Scn *scn;
281247841Sbapt	char *src, *osname;
282255457Sbapt	const char *arch, *abi, *fpu, *endian_corres_str;
283255457Sbapt	const char *wordsize_corres_str;
284247841Sbapt	GElf_Ehdr elfhdr;
285247841Sbapt	GElf_Shdr shdr;
286247841Sbapt	int fd, i, ret;
287247841Sbapt	uint32_t version;
288247841Sbapt
289247841Sbapt	version = 0;
290247841Sbapt	ret = -1;
291247841Sbapt	scn = NULL;
292247841Sbapt	abi = NULL;
293247841Sbapt
294247841Sbapt	if (elf_version(EV_CURRENT) == EV_NONE) {
295247841Sbapt		warnx("ELF library initialization failed: %s",
296247841Sbapt		    elf_errmsg(-1));
297247841Sbapt		return (-1);
298247841Sbapt	}
299247841Sbapt
300247841Sbapt	if ((fd = open(_PATH_BSHELL, O_RDONLY)) < 0) {
301247841Sbapt		warn("open()");
302247841Sbapt		return (-1);
303247841Sbapt	}
304247841Sbapt
305247841Sbapt	if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
306247841Sbapt		ret = -1;
307247841Sbapt		warnx("elf_begin() failed: %s.", elf_errmsg(-1));
308247841Sbapt		goto cleanup;
309247841Sbapt	}
310247841Sbapt
311247841Sbapt	if (gelf_getehdr(elf, &elfhdr) == NULL) {
312247841Sbapt		ret = -1;
313247841Sbapt		warn("getehdr() failed: %s.", elf_errmsg(-1));
314247841Sbapt		goto cleanup;
315247841Sbapt	}
316247841Sbapt	while ((scn = elf_nextscn(elf, scn)) != NULL) {
317247841Sbapt		if (gelf_getshdr(scn, &shdr) != &shdr) {
318247841Sbapt			ret = -1;
319247841Sbapt			warn("getshdr() failed: %s.", elf_errmsg(-1));
320247841Sbapt			goto cleanup;
321247841Sbapt		}
322247841Sbapt
323247841Sbapt		if (shdr.sh_type == SHT_NOTE)
324247841Sbapt			break;
325247841Sbapt	}
326247841Sbapt
327247841Sbapt	if (scn == NULL) {
328247841Sbapt		ret = -1;
329247841Sbapt		warn("failed to get the note section");
330247841Sbapt		goto cleanup;
331247841Sbapt	}
332247841Sbapt
333247841Sbapt	data = elf_getdata(scn, NULL);
334247841Sbapt	src = data->d_buf;
335247841Sbapt	for (;;) {
336247841Sbapt		memcpy(&note, src, sizeof(Elf_Note));
337247841Sbapt		src += sizeof(Elf_Note);
338247841Sbapt		if (note.n_type == NT_VERSION)
339247841Sbapt			break;
340247841Sbapt		src += note.n_namesz + note.n_descsz;
341247841Sbapt	}
342247841Sbapt	osname = src;
343247841Sbapt	src += roundup2(note.n_namesz, 4);
344247841Sbapt	if (elfhdr.e_ident[EI_DATA] == ELFDATA2MSB)
345247841Sbapt		version = be32dec(src);
346247841Sbapt	else
347247841Sbapt		version = le32dec(src);
348247841Sbapt
349247841Sbapt	for (i = 0; osname[i] != '\0'; i++)
350247841Sbapt		osname[i] = (char)tolower(osname[i]);
351247841Sbapt
352255457Sbapt	wordsize_corres_str = elf_corres_to_string(wordsize_corres,
353255457Sbapt	    (int)elfhdr.e_ident[EI_CLASS]);
354247841Sbapt
355255457Sbapt	arch = elf_corres_to_string(mach_corres, (int) elfhdr.e_machine);
356255457Sbapt
357255457Sbapt	snprintf(dest, sz, "%s:%d",
358255457Sbapt	    osname, version / 100000);
359255457Sbapt
360247841Sbapt	ret = 0;
361247841Sbapt
362247841Sbapt	switch (elfhdr.e_machine) {
363247841Sbapt	case EM_ARM:
364255457Sbapt		endian_corres_str = elf_corres_to_string(endian_corres,
365255457Sbapt		    (int)elfhdr.e_ident[EI_DATA]);
366255457Sbapt
367253755Sbapt		/* FreeBSD doesn't support the hard-float ABI yet */
368253755Sbapt		fpu = "softfp";
369253755Sbapt		if ((elfhdr.e_flags & 0xFF000000) != 0) {
370255457Sbapt			const char *sh_name = NULL;
371255457Sbapt			size_t shstrndx;
372255457Sbapt
373253755Sbapt			/* This is an EABI file, the conformance level is set */
374253755Sbapt			abi = "eabi";
375255457Sbapt			/* Find which TARGET_ARCH we are building for. */
376255457Sbapt			elf_getshdrstrndx(elf, &shstrndx);
377255457Sbapt			while ((scn = elf_nextscn(elf, scn)) != NULL) {
378255457Sbapt				sh_name = NULL;
379255457Sbapt				if (gelf_getshdr(scn, &shdr) != &shdr) {
380255457Sbapt					scn = NULL;
381255457Sbapt					break;
382255457Sbapt				}
383255457Sbapt
384255457Sbapt				sh_name = elf_strptr(elf, shstrndx,
385255457Sbapt				    shdr.sh_name);
386255457Sbapt				if (sh_name == NULL)
387255457Sbapt					continue;
388255457Sbapt				if (strcmp(".ARM.attributes", sh_name) == 0)
389255457Sbapt					break;
390255457Sbapt			}
391255457Sbapt			if (scn != NULL && sh_name != NULL) {
392255457Sbapt				data = elf_getdata(scn, NULL);
393255457Sbapt				/*
394255457Sbapt				 * Prior to FreeBSD 10.0 libelf would return
395255457Sbapt				 * NULL from elf_getdata on the .ARM.attributes
396255457Sbapt				 * section. As this was the first release to
397255457Sbapt				 * get armv6 support assume a NULL value means
398255457Sbapt				 * arm.
399255457Sbapt				 *
400255457Sbapt				 * This assumption can be removed when 9.x
401255457Sbapt				 * is unsupported.
402255457Sbapt				 */
403255457Sbapt				if (data != NULL) {
404255457Sbapt					arch = aeabi_parse_arm_attributes(
405255457Sbapt					    data->d_buf, data->d_size);
406255457Sbapt					if (arch == NULL) {
407255457Sbapt						ret = 1;
408255457Sbapt						warn("unknown ARM ARCH");
409255457Sbapt						goto cleanup;
410255457Sbapt					}
411255457Sbapt				}
412255457Sbapt			} else {
413255457Sbapt				ret = 1;
414255457Sbapt				warn("Unable to find the .ARM.attributes "
415255457Sbapt				    "section");
416255457Sbapt				goto cleanup;
417255457Sbapt			}
418253755Sbapt		} else if (elfhdr.e_ident[EI_OSABI] != ELFOSABI_NONE) {
419253755Sbapt			/*
420253755Sbapt			 * EABI executables all have this field set to
421253755Sbapt			 * ELFOSABI_NONE, therefore it must be an oabi file.
422253755Sbapt			 */
423253755Sbapt			abi = "oabi";
424253755Sbapt		} else {
425253755Sbapt			ret = 1;
426255457Sbapt			warn("unknown ARM ABI");
427253755Sbapt			goto cleanup;
428253755Sbapt		}
429247841Sbapt		snprintf(dest + strlen(dest), sz - strlen(dest),
430255457Sbapt		    ":%s:%s:%s:%s:%s", arch, wordsize_corres_str,
431255457Sbapt		    endian_corres_str, abi, fpu);
432247841Sbapt		break;
433247841Sbapt	case EM_MIPS:
434247841Sbapt		/*
435247841Sbapt		 * this is taken from binutils sources:
436247841Sbapt		 * include/elf/mips.h
437247841Sbapt		 * mapping is figured out from binutils:
438247841Sbapt		 * gas/config/tc-mips.c
439247841Sbapt		 */
440247841Sbapt		switch (elfhdr.e_flags & EF_MIPS_ABI) {
441247841Sbapt		case E_MIPS_ABI_O32:
442247841Sbapt			abi = "o32";
443247841Sbapt			break;
444247841Sbapt		case E_MIPS_ABI_N32:
445247841Sbapt			abi = "n32";
446247841Sbapt			break;
447247841Sbapt		default:
448247841Sbapt			if (elfhdr.e_ident[EI_DATA] ==
449247841Sbapt			    ELFCLASS32)
450247841Sbapt				abi = "o32";
451247841Sbapt			else if (elfhdr.e_ident[EI_DATA] ==
452247841Sbapt			    ELFCLASS64)
453247841Sbapt				abi = "n64";
454247841Sbapt			break;
455247841Sbapt		}
456255457Sbapt		endian_corres_str = elf_corres_to_string(endian_corres,
457255457Sbapt		    (int)elfhdr.e_ident[EI_DATA]);
458255457Sbapt
459255457Sbapt		snprintf(dest + strlen(dest), sz - strlen(dest), ":%s:%s:%s:%s",
460255457Sbapt		    arch, wordsize_corres_str, endian_corres_str, abi);
461247841Sbapt		break;
462255457Sbapt	default:
463255457Sbapt		snprintf(dest + strlen(dest), sz - strlen(dest), ":%s:%s",
464255457Sbapt		    arch, wordsize_corres_str);
465247841Sbapt	}
466247841Sbapt
467247841Sbaptcleanup:
468247841Sbapt	if (elf != NULL)
469247841Sbapt		elf_end(elf);
470247841Sbapt
471247841Sbapt	close(fd);
472247841Sbapt	return (ret);
473247841Sbapt}
474247841Sbapt
475247841Sbaptstatic void
476247841Sbaptsubst_packagesite(const char *abi)
477247841Sbapt{
478247841Sbapt	struct sbuf *newval;
479247841Sbapt	const char *variable_string;
480247841Sbapt	const char *oldval;
481247841Sbapt
482247841Sbapt	if (c[PACKAGESITE].value != NULL)
483247841Sbapt		oldval = c[PACKAGESITE].value;
484247841Sbapt	else
485247841Sbapt		oldval = c[PACKAGESITE].val;
486247841Sbapt
487247841Sbapt	if ((variable_string = strstr(oldval, "${ABI}")) == NULL)
488247841Sbapt		return;
489247841Sbapt
490247841Sbapt	newval = sbuf_new_auto();
491247841Sbapt	sbuf_bcat(newval, oldval, variable_string - oldval);
492247841Sbapt	sbuf_cat(newval, abi);
493247841Sbapt	sbuf_cat(newval, variable_string + strlen("${ABI}"));
494247841Sbapt	sbuf_finish(newval);
495247841Sbapt
496247841Sbapt	free(c[PACKAGESITE].value);
497247841Sbapt	c[PACKAGESITE].value = strdup(sbuf_data(newval));
498247841Sbapt}
499247841Sbapt
500259266Sbdrewerystatic int
501259266Sbdreweryboolstr_to_bool(const char *str)
502259266Sbdrewery{
503259266Sbdrewery	if (str != NULL && (strcasecmp(str, "true") == 0 ||
504259266Sbdrewery	    strcasecmp(str, "yes") == 0 || strcasecmp(str, "on") == 0 ||
505259266Sbdrewery	    str[0] == '1'))
506259266Sbdrewery		return (true);
507259266Sbdrewery
508259266Sbdrewery	return (false);
509259266Sbdrewery}
510259266Sbdrewery
511247841Sbaptstatic void
512262400Sbaptconfig_parse(ucl_object_t *obj, pkg_conf_file_t conftype)
513247841Sbapt{
514247841Sbapt	struct sbuf *buf = sbuf_new_auto();
515262400Sbapt	ucl_object_t *cur, *seq;
516262400Sbapt	ucl_object_iter_t it = NULL, itseq = NULL;
517259266Sbdrewery	struct config_entry *temp_config;
518259266Sbdrewery	struct config_value *cv;
519262400Sbapt	const char *key;
520247841Sbapt	int i;
521247841Sbapt	size_t j;
522247841Sbapt
523259266Sbdrewery	/* Temporary config for configs that may be disabled. */
524259266Sbdrewery	temp_config = calloc(CONFIG_SIZE, sizeof(struct config_entry));
525259266Sbdrewery
526262400Sbapt	while ((cur = ucl_iterate_object(obj, &it, true))) {
527262400Sbapt		key = ucl_object_key(cur);
528262400Sbapt		if (key == NULL)
529247841Sbapt			continue;
530247841Sbapt		sbuf_clear(buf);
531247841Sbapt
532257145Sbdrewery		if (conftype == CONFFILE_PKG) {
533262400Sbapt			for (j = 0; j < strlen(key); ++j)
534262400Sbapt				sbuf_putc(buf, key[j]);
535257145Sbdrewery			sbuf_finish(buf);
536257145Sbdrewery		} else if (conftype == CONFFILE_REPO) {
537262400Sbapt			if (strcasecmp(key, "url") == 0)
538257145Sbdrewery				sbuf_cpy(buf, "PACKAGESITE");
539262400Sbapt			else if (strcasecmp(key, "mirror_type") == 0)
540257145Sbdrewery				sbuf_cpy(buf, "MIRROR_TYPE");
541262400Sbapt			else if (strcasecmp(key, "signature_type") == 0)
542257147Sbdrewery				sbuf_cpy(buf, "SIGNATURE_TYPE");
543262400Sbapt			else if (strcasecmp(key, "fingerprints") == 0)
544257147Sbdrewery				sbuf_cpy(buf, "FINGERPRINTS");
545262400Sbapt			else if (strcasecmp(key, "enabled") == 0) {
546262400Sbapt				if ((cur->type != UCL_BOOLEAN) ||
547262400Sbapt				    !ucl_object_toboolean(cur))
548259266Sbdrewery					goto cleanup;
549262400Sbapt			} else
550257145Sbdrewery				continue;
551257145Sbdrewery			sbuf_finish(buf);
552257145Sbdrewery		}
553257145Sbdrewery
554247841Sbapt		for (i = 0; i < CONFIG_SIZE; i++) {
555247841Sbapt			if (strcmp(sbuf_data(buf), c[i].key) == 0)
556247841Sbapt				break;
557247841Sbapt		}
558247841Sbapt
559257145Sbdrewery		/* Silently skip unknown keys to be future compatible. */
560262400Sbapt		if (i == CONFIG_SIZE)
561247841Sbapt			continue;
562247841Sbapt
563247841Sbapt		/* env has priority over config file */
564262400Sbapt		if (c[i].envset)
565247841Sbapt			continue;
566247841Sbapt
567259266Sbdrewery		/* Parse sequence value ["item1", "item2"] */
568259266Sbdrewery		switch (c[i].type) {
569259266Sbdrewery		case PKG_CONFIG_LIST:
570262400Sbapt			if (cur->type != UCL_ARRAY) {
571262400Sbapt				warnx("Skipping invalid array "
572259266Sbdrewery				    "value for %s.\n", c[i].key);
573259266Sbdrewery				continue;
574259266Sbdrewery			}
575259266Sbdrewery			temp_config[i].list =
576259266Sbdrewery			    malloc(sizeof(*temp_config[i].list));
577259266Sbdrewery			STAILQ_INIT(temp_config[i].list);
578259266Sbdrewery
579262400Sbapt			while ((seq = ucl_iterate_object(cur, &itseq, true))) {
580262400Sbapt				if (seq->type != UCL_STRING)
581259266Sbdrewery					continue;
582259266Sbdrewery				cv = malloc(sizeof(struct config_value));
583259266Sbdrewery				cv->value =
584262400Sbapt				    strdup(ucl_object_tostring(seq));
585259266Sbdrewery				STAILQ_INSERT_TAIL(temp_config[i].list, cv,
586259266Sbdrewery				    next);
587259266Sbdrewery			}
588259266Sbdrewery			break;
589259266Sbdrewery		default:
590259266Sbdrewery			/* Normal string value. */
591262400Sbapt			temp_config[i].value = strdup(ucl_object_tostring(cur));
592259266Sbdrewery			break;
593259266Sbdrewery		}
594247841Sbapt	}
595247841Sbapt
596259266Sbdrewery	/* Repo is enabled, copy over all settings from temp_config. */
597259266Sbdrewery	for (i = 0; i < CONFIG_SIZE; i++) {
598259266Sbdrewery		if (c[i].envset)
599259266Sbdrewery			continue;
600259266Sbdrewery		switch (c[i].type) {
601259266Sbdrewery		case PKG_CONFIG_LIST:
602259266Sbdrewery			c[i].list = temp_config[i].list;
603259266Sbdrewery			break;
604259266Sbdrewery		default:
605259266Sbdrewery			c[i].value = temp_config[i].value;
606259266Sbdrewery			break;
607259266Sbdrewery		}
608259266Sbdrewery	}
609259266Sbdrewery
610259266Sbdrewerycleanup:
611259266Sbdrewery	free(temp_config);
612247841Sbapt	sbuf_delete(buf);
613247841Sbapt}
614247841Sbapt
615257145Sbdrewery/*-
616257145Sbdrewery * Parse new repo style configs in style:
617257145Sbdrewery * Name:
618257145Sbdrewery *   URL:
619257145Sbdrewery *   MIRROR_TYPE:
620257145Sbdrewery * etc...
621257145Sbdrewery */
622257145Sbdrewerystatic void
623262400Sbaptparse_repo_file(ucl_object_t *obj)
624247841Sbapt{
625262400Sbapt	ucl_object_iter_t it = NULL;
626262400Sbapt	ucl_object_t *cur;
627262400Sbapt	const char *key;
628257145Sbdrewery
629262400Sbapt	while ((cur = ucl_iterate_object(obj, &it, true))) {
630262400Sbapt		key = ucl_object_key(cur);
631257145Sbdrewery
632262400Sbapt		if (key == NULL)
633257145Sbdrewery			continue;
634257145Sbdrewery
635262400Sbapt		if (cur->type != UCL_OBJECT)
636257145Sbdrewery			continue;
637257145Sbdrewery
638262400Sbapt		config_parse(cur, CONFFILE_REPO);
639257145Sbdrewery	}
640257145Sbdrewery}
641257145Sbdrewery
642257145Sbdrewery
643257145Sbdrewerystatic int
644257145Sbdreweryread_conf_file(const char *confpath, pkg_conf_file_t conftype)
645257145Sbdrewery{
646262400Sbapt	struct ucl_parser *p;
647262400Sbapt	ucl_object_t *obj = NULL;
648247841Sbapt
649262400Sbapt	p = ucl_parser_new(0);
650262400Sbapt
651262400Sbapt	if (!ucl_parser_add_file(p, confpath)) {
652247841Sbapt		if (errno != ENOENT)
653262400Sbapt			errx(EXIT_FAILURE, "Unable to parse configuration "
654262400Sbapt			    "file %s: %s", confpath, ucl_parser_get_error(p));
655262400Sbapt		ucl_parser_free(p);
656247841Sbapt		/* no configuration present */
657257145Sbdrewery		return (1);
658247841Sbapt	}
659247841Sbapt
660262400Sbapt	obj = ucl_parser_get_object(p);
661262400Sbapt	if (obj->type != UCL_OBJECT)
662257142Sbdrewery		warnx("Invalid configuration format, ignoring the "
663257145Sbdrewery		    "configuration file %s", confpath);
664257145Sbdrewery	else {
665257145Sbdrewery		if (conftype == CONFFILE_PKG)
666262400Sbapt			config_parse(obj, conftype);
667257145Sbdrewery		else if (conftype == CONFFILE_REPO)
668262400Sbapt			parse_repo_file(obj);
669247841Sbapt	}
670247841Sbapt
671262400Sbapt	ucl_object_free(obj);
672262400Sbapt	ucl_parser_free(p);
673247841Sbapt
674257145Sbdrewery	return (0);
675257145Sbdrewery}
676257145Sbdrewery
677259266Sbdrewerystatic int
678259266Sbdreweryload_repositories(const char *repodir)
679259266Sbdrewery{
680259266Sbdrewery	struct dirent *ent;
681259266Sbdrewery	DIR *d;
682259266Sbdrewery	char *p;
683259266Sbdrewery	size_t n;
684259266Sbdrewery	char path[MAXPATHLEN];
685259266Sbdrewery	int ret;
686259266Sbdrewery
687259266Sbdrewery	ret = 0;
688259266Sbdrewery
689259266Sbdrewery	if ((d = opendir(repodir)) == NULL)
690259266Sbdrewery		return (1);
691259266Sbdrewery
692259266Sbdrewery	while ((ent = readdir(d))) {
693259266Sbdrewery		/* Trim out 'repos'. */
694259266Sbdrewery		if ((n = strlen(ent->d_name)) <= 5)
695259266Sbdrewery			continue;
696259266Sbdrewery		p = &ent->d_name[n - 5];
697259266Sbdrewery		if (strcmp(p, ".conf") == 0) {
698259266Sbdrewery			snprintf(path, sizeof(path), "%s%s%s",
699259266Sbdrewery			    repodir,
700259266Sbdrewery			    repodir[strlen(repodir) - 1] == '/' ? "" : "/",
701259266Sbdrewery			    ent->d_name);
702259266Sbdrewery			if (access(path, F_OK) == 0 &&
703259266Sbdrewery			    read_conf_file(path, CONFFILE_REPO)) {
704259266Sbdrewery				ret = 1;
705259266Sbdrewery				goto cleanup;
706259266Sbdrewery			}
707259266Sbdrewery		}
708259266Sbdrewery	}
709259266Sbdrewery
710259266Sbdrewerycleanup:
711259266Sbdrewery	closedir(d);
712259266Sbdrewery
713259266Sbdrewery	return (ret);
714259266Sbdrewery}
715259266Sbdrewery
716257145Sbdreweryint
717257145Sbdreweryconfig_init(void)
718257145Sbdrewery{
719259266Sbdrewery	char *val;
720257145Sbdrewery	int i;
721257145Sbdrewery	const char *localbase;
722259266Sbdrewery	char *env_list_item;
723257145Sbdrewery	char confpath[MAXPATHLEN];
724259266Sbdrewery	struct config_value *cv;
725257145Sbdrewery	char abi[BUFSIZ];
726257145Sbdrewery
727257145Sbdrewery	for (i = 0; i < CONFIG_SIZE; i++) {
728257145Sbdrewery		val = getenv(c[i].key);
729257145Sbdrewery		if (val != NULL) {
730257145Sbdrewery			c[i].envset = true;
731259266Sbdrewery			switch (c[i].type) {
732259266Sbdrewery			case PKG_CONFIG_LIST:
733259266Sbdrewery				/* Split up comma-separated items from env. */
734259266Sbdrewery				c[i].list = malloc(sizeof(*c[i].list));
735259266Sbdrewery				STAILQ_INIT(c[i].list);
736259266Sbdrewery				for (env_list_item = strtok(val, ",");
737259266Sbdrewery				    env_list_item != NULL;
738259266Sbdrewery				    env_list_item = strtok(NULL, ",")) {
739259266Sbdrewery					cv =
740259266Sbdrewery					    malloc(sizeof(struct config_value));
741259266Sbdrewery					cv->value =
742259266Sbdrewery					    strdup(env_list_item);
743259266Sbdrewery					STAILQ_INSERT_TAIL(c[i].list, cv,
744259266Sbdrewery					    next);
745259266Sbdrewery				}
746259266Sbdrewery				break;
747259266Sbdrewery			default:
748259266Sbdrewery				c[i].val = val;
749259266Sbdrewery				break;
750259266Sbdrewery			}
751257145Sbdrewery		}
752257145Sbdrewery	}
753257145Sbdrewery
754259266Sbdrewery	/* Read LOCALBASE/etc/pkg.conf first. */
755257145Sbdrewery	localbase = getenv("LOCALBASE") ? getenv("LOCALBASE") : _LOCALBASE;
756257145Sbdrewery	snprintf(confpath, sizeof(confpath), "%s/etc/pkg.conf",
757257145Sbdrewery	    localbase);
758257145Sbdrewery
759257145Sbdrewery	if (access(confpath, F_OK) == 0 && read_conf_file(confpath,
760257145Sbdrewery	    CONFFILE_PKG))
761257145Sbdrewery		goto finalize;
762257145Sbdrewery
763259266Sbdrewery	/* Then read in all repos from REPOS_DIR list of directories. */
764259266Sbdrewery	if (c[REPOS_DIR].list == NULL) {
765259266Sbdrewery		c[REPOS_DIR].list = malloc(sizeof(*c[REPOS_DIR].list));
766259266Sbdrewery		STAILQ_INIT(c[REPOS_DIR].list);
767259266Sbdrewery		cv = malloc(sizeof(struct config_value));
768259266Sbdrewery		cv->value = strdup("/etc/pkg");
769259266Sbdrewery		STAILQ_INSERT_TAIL(c[REPOS_DIR].list, cv, next);
770259266Sbdrewery		cv = malloc(sizeof(struct config_value));
771259266Sbdrewery		if (asprintf(&cv->value, "%s/etc/pkg/repos", localbase) < 0)
772259266Sbdrewery			goto finalize;
773259266Sbdrewery		STAILQ_INSERT_TAIL(c[REPOS_DIR].list, cv, next);
774259266Sbdrewery	}
775257145Sbdrewery
776259266Sbdrewery	STAILQ_FOREACH(cv, c[REPOS_DIR].list, next)
777259266Sbdrewery		if (load_repositories(cv->value))
778259266Sbdrewery			goto finalize;
779259266Sbdrewery
780247841Sbaptfinalize:
781247841Sbapt	if (c[ABI].val == NULL && c[ABI].value == NULL) {
782247841Sbapt		if (pkg_get_myabi(abi, BUFSIZ) != 0)
783257142Sbdrewery			errx(EXIT_FAILURE, "Failed to determine the system "
784257142Sbdrewery			    "ABI");
785247841Sbapt		c[ABI].val = abi;
786247841Sbapt	}
787247841Sbapt
788247843Sbapt	subst_packagesite(c[ABI].value != NULL ? c[ABI].value : c[ABI].val);
789247841Sbapt
790247841Sbapt	return (0);
791247841Sbapt}
792247841Sbapt
793247841Sbaptint
794247841Sbaptconfig_string(pkg_config_key k, const char **val)
795247841Sbapt{
796247841Sbapt	if (c[k].type != PKG_CONFIG_STRING)
797247841Sbapt		return (-1);
798247841Sbapt
799247841Sbapt	if (c[k].value != NULL)
800247841Sbapt		*val = c[k].value;
801247841Sbapt	else
802247841Sbapt		*val = c[k].val;
803247841Sbapt
804247841Sbapt	return (0);
805247841Sbapt}
806247841Sbapt
807247841Sbaptint
808247841Sbaptconfig_bool(pkg_config_key k, bool *val)
809247841Sbapt{
810247841Sbapt	const char *value;
811247841Sbapt
812247841Sbapt	if (c[k].type != PKG_CONFIG_BOOL)
813247841Sbapt		return (-1);
814247841Sbapt
815247841Sbapt	*val = false;
816247841Sbapt
817247841Sbapt	if (c[k].value != NULL)
818247841Sbapt		value = c[k].value;
819247841Sbapt	else
820247841Sbapt		value = c[k].val;
821247841Sbapt
822259266Sbdrewery	if (boolstr_to_bool(value))
823247841Sbapt		*val = true;
824247841Sbapt
825247841Sbapt	return (0);
826247841Sbapt}
827247841Sbapt
828247841Sbaptvoid
829247841Sbaptconfig_finish(void) {
830247841Sbapt	int i;
831247841Sbapt
832247841Sbapt	for (i = 0; i < CONFIG_SIZE; i++)
833247841Sbapt		free(c[i].value);
834247841Sbapt}
835