1*a824f5a1SJean-Baptiste Boric /* $NetBSD: parse-config.c,v 1.1.1.11 2010/06/26 00:14:31 joerg Exp $ */
2*a824f5a1SJean-Baptiste Boric
3*a824f5a1SJean-Baptiste Boric #if HAVE_CONFIG_H
4*a824f5a1SJean-Baptiste Boric #include "config.h"
5*a824f5a1SJean-Baptiste Boric #endif
6*a824f5a1SJean-Baptiste Boric #include <nbcompat.h>
7*a824f5a1SJean-Baptiste Boric #if HAVE_SYS_CDEFS_H
8*a824f5a1SJean-Baptiste Boric #include <sys/cdefs.h>
9*a824f5a1SJean-Baptiste Boric #endif
10*a824f5a1SJean-Baptiste Boric __RCSID("$NetBSD: parse-config.c,v 1.1.1.11 2010/06/26 00:14:31 joerg Exp $");
11*a824f5a1SJean-Baptiste Boric
12*a824f5a1SJean-Baptiste Boric /*-
13*a824f5a1SJean-Baptiste Boric * Copyright (c) 2008, 2009 Joerg Sonnenberger <joerg@NetBSD.org>.
14*a824f5a1SJean-Baptiste Boric * All rights reserved.
15*a824f5a1SJean-Baptiste Boric *
16*a824f5a1SJean-Baptiste Boric * Redistribution and use in source and binary forms, with or without
17*a824f5a1SJean-Baptiste Boric * modification, are permitted provided that the following conditions
18*a824f5a1SJean-Baptiste Boric * are met:
19*a824f5a1SJean-Baptiste Boric *
20*a824f5a1SJean-Baptiste Boric * 1. Redistributions of source code must retain the above copyright
21*a824f5a1SJean-Baptiste Boric * notice, this list of conditions and the following disclaimer.
22*a824f5a1SJean-Baptiste Boric * 2. Redistributions in binary form must reproduce the above copyright
23*a824f5a1SJean-Baptiste Boric * notice, this list of conditions and the following disclaimer in
24*a824f5a1SJean-Baptiste Boric * the documentation and/or other materials provided with the
25*a824f5a1SJean-Baptiste Boric * distribution.
26*a824f5a1SJean-Baptiste Boric *
27*a824f5a1SJean-Baptiste Boric * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28*a824f5a1SJean-Baptiste Boric * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29*a824f5a1SJean-Baptiste Boric * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30*a824f5a1SJean-Baptiste Boric * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31*a824f5a1SJean-Baptiste Boric * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32*a824f5a1SJean-Baptiste Boric * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
33*a824f5a1SJean-Baptiste Boric * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
34*a824f5a1SJean-Baptiste Boric * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
35*a824f5a1SJean-Baptiste Boric * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
36*a824f5a1SJean-Baptiste Boric * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
37*a824f5a1SJean-Baptiste Boric * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38*a824f5a1SJean-Baptiste Boric * SUCH DAMAGE.
39*a824f5a1SJean-Baptiste Boric */
40*a824f5a1SJean-Baptiste Boric
41*a824f5a1SJean-Baptiste Boric #if HAVE_ERR_H
42*a824f5a1SJean-Baptiste Boric #include <err.h>
43*a824f5a1SJean-Baptiste Boric #endif
44*a824f5a1SJean-Baptiste Boric #include <errno.h>
45*a824f5a1SJean-Baptiste Boric #if HAVE_STRING_H
46*a824f5a1SJean-Baptiste Boric #include <string.h>
47*a824f5a1SJean-Baptiste Boric #endif
48*a824f5a1SJean-Baptiste Boric
49*a824f5a1SJean-Baptiste Boric #ifndef BOOTSTRAP
50*a824f5a1SJean-Baptiste Boric #include <fetch.h>
51*a824f5a1SJean-Baptiste Boric #endif
52*a824f5a1SJean-Baptiste Boric
53*a824f5a1SJean-Baptiste Boric #include "lib.h"
54*a824f5a1SJean-Baptiste Boric
55*a824f5a1SJean-Baptiste Boric static int cache_connections = 16;
56*a824f5a1SJean-Baptiste Boric static int cache_connections_host = 4;
57*a824f5a1SJean-Baptiste Boric
58*a824f5a1SJean-Baptiste Boric const char *config_file = SYSCONFDIR"/pkg_install.conf";
59*a824f5a1SJean-Baptiste Boric
60*a824f5a1SJean-Baptiste Boric char fetch_flags[10] = ""; /* Workaround Mac OS X linker issues with BSS */
61*a824f5a1SJean-Baptiste Boric static const char *active_ftp;
62*a824f5a1SJean-Baptiste Boric static const char *verbose_netio;
63*a824f5a1SJean-Baptiste Boric static const char *ignore_proxy;
64*a824f5a1SJean-Baptiste Boric const char *cache_index = "yes";
65*a824f5a1SJean-Baptiste Boric const char *cert_chain_file;
66*a824f5a1SJean-Baptiste Boric const char *certs_packages;
67*a824f5a1SJean-Baptiste Boric const char *certs_pkg_vulnerabilities;
68*a824f5a1SJean-Baptiste Boric const char *check_eol = "yes";
69*a824f5a1SJean-Baptiste Boric const char *check_vulnerabilities;
70*a824f5a1SJean-Baptiste Boric static const char *config_cache_connections;
71*a824f5a1SJean-Baptiste Boric static const char *config_cache_connections_host;
72*a824f5a1SJean-Baptiste Boric const char *config_pkg_dbdir;
73*a824f5a1SJean-Baptiste Boric const char *config_pkg_path;
74*a824f5a1SJean-Baptiste Boric const char *config_pkg_refcount_dbdir;
75*a824f5a1SJean-Baptiste Boric const char *do_license_check;
76*a824f5a1SJean-Baptiste Boric const char *verified_installation;
77*a824f5a1SJean-Baptiste Boric const char *gpg_cmd;
78*a824f5a1SJean-Baptiste Boric const char *gpg_keyring_pkgvuln;
79*a824f5a1SJean-Baptiste Boric const char *gpg_keyring_sign;
80*a824f5a1SJean-Baptiste Boric const char *gpg_keyring_verify;
81*a824f5a1SJean-Baptiste Boric const char *gpg_sign_as;
82*a824f5a1SJean-Baptiste Boric const char *pkg_vulnerabilities_dir;
83*a824f5a1SJean-Baptiste Boric const char *pkg_vulnerabilities_file;
84*a824f5a1SJean-Baptiste Boric const char *pkg_vulnerabilities_url;
85*a824f5a1SJean-Baptiste Boric const char *ignore_advisories = NULL;
86*a824f5a1SJean-Baptiste Boric const char tnf_vulnerability_base[] = "http://ftp.NetBSD.org/pub/NetBSD/packages/vulns";
87*a824f5a1SJean-Baptiste Boric const char *acceptable_licenses = NULL;
88*a824f5a1SJean-Baptiste Boric
89*a824f5a1SJean-Baptiste Boric static struct config_variable {
90*a824f5a1SJean-Baptiste Boric const char *name;
91*a824f5a1SJean-Baptiste Boric const char **var;
92*a824f5a1SJean-Baptiste Boric } config_variables[] = {
93*a824f5a1SJean-Baptiste Boric { "ACCEPTABLE_LICENSES", &acceptable_licenses },
94*a824f5a1SJean-Baptiste Boric { "ACTIVE_FTP", &active_ftp },
95*a824f5a1SJean-Baptiste Boric { "CACHE_INDEX", &cache_index },
96*a824f5a1SJean-Baptiste Boric { "CACHE_CONNECTIONS", &config_cache_connections },
97*a824f5a1SJean-Baptiste Boric { "CACHE_CONNECTIONS_HOST", &config_cache_connections_host },
98*a824f5a1SJean-Baptiste Boric { "CERTIFICATE_ANCHOR_PKGS", &certs_packages },
99*a824f5a1SJean-Baptiste Boric { "CERTIFICATE_ANCHOR_PKGVULN", &certs_pkg_vulnerabilities },
100*a824f5a1SJean-Baptiste Boric { "CERTIFICATE_CHAIN", &cert_chain_file },
101*a824f5a1SJean-Baptiste Boric { "CHECK_LICENSE", &do_license_check },
102*a824f5a1SJean-Baptiste Boric { "CHECK_END_OF_LIFE", &check_eol },
103*a824f5a1SJean-Baptiste Boric { "CHECK_VULNERABILITIES", &check_vulnerabilities },
104*a824f5a1SJean-Baptiste Boric { "DEFAULT_ACCEPTABLE_LICENSES", &default_acceptable_licenses },
105*a824f5a1SJean-Baptiste Boric { "GPG", &gpg_cmd },
106*a824f5a1SJean-Baptiste Boric { "GPG_KEYRING_PKGVULN", &gpg_keyring_pkgvuln },
107*a824f5a1SJean-Baptiste Boric { "GPG_KEYRING_SIGN", &gpg_keyring_sign },
108*a824f5a1SJean-Baptiste Boric { "GPG_KEYRING_VERIFY", &gpg_keyring_verify },
109*a824f5a1SJean-Baptiste Boric { "GPG_SIGN_AS", &gpg_sign_as },
110*a824f5a1SJean-Baptiste Boric { "IGNORE_PROXY", &ignore_proxy },
111*a824f5a1SJean-Baptiste Boric { "IGNORE_URL", &ignore_advisories },
112*a824f5a1SJean-Baptiste Boric { "PKG_DBDIR", &config_pkg_dbdir },
113*a824f5a1SJean-Baptiste Boric { "PKG_PATH", &config_pkg_path },
114*a824f5a1SJean-Baptiste Boric { "PKG_REFCOUNT_DBDIR", &config_pkg_refcount_dbdir },
115*a824f5a1SJean-Baptiste Boric { "PKGVULNDIR", &pkg_vulnerabilities_dir },
116*a824f5a1SJean-Baptiste Boric { "PKGVULNURL", &pkg_vulnerabilities_url },
117*a824f5a1SJean-Baptiste Boric { "VERBOSE_NETIO", &verbose_netio },
118*a824f5a1SJean-Baptiste Boric { "VERIFIED_INSTALLATION", &verified_installation },
119*a824f5a1SJean-Baptiste Boric { NULL, NULL }, /* For use by pkg_install_show_variable */
120*a824f5a1SJean-Baptiste Boric { NULL, NULL }
121*a824f5a1SJean-Baptiste Boric };
122*a824f5a1SJean-Baptiste Boric
123*a824f5a1SJean-Baptiste Boric char *config_tmp_variables[sizeof config_variables/sizeof config_variables[0]];
124*a824f5a1SJean-Baptiste Boric
125*a824f5a1SJean-Baptiste Boric static void
parse_pkg_install_conf(void)126*a824f5a1SJean-Baptiste Boric parse_pkg_install_conf(void)
127*a824f5a1SJean-Baptiste Boric {
128*a824f5a1SJean-Baptiste Boric struct config_variable *var;
129*a824f5a1SJean-Baptiste Boric FILE *fp;
130*a824f5a1SJean-Baptiste Boric char *line, *value;
131*a824f5a1SJean-Baptiste Boric size_t len, var_len, i;
132*a824f5a1SJean-Baptiste Boric
133*a824f5a1SJean-Baptiste Boric fp = fopen(config_file, "r");
134*a824f5a1SJean-Baptiste Boric if (!fp) {
135*a824f5a1SJean-Baptiste Boric if (errno != ENOENT)
136*a824f5a1SJean-Baptiste Boric warn("Can't open '%s' for reading", config_file);
137*a824f5a1SJean-Baptiste Boric return;
138*a824f5a1SJean-Baptiste Boric }
139*a824f5a1SJean-Baptiste Boric
140*a824f5a1SJean-Baptiste Boric while ((line = fgetln(fp, &len)) != (char *) NULL) {
141*a824f5a1SJean-Baptiste Boric if (line[len - 1] == '\n')
142*a824f5a1SJean-Baptiste Boric --len;
143*a824f5a1SJean-Baptiste Boric for (i = 0; (var = &config_variables[i])->name != NULL; ++i) {
144*a824f5a1SJean-Baptiste Boric var_len = strlen(var->name);
145*a824f5a1SJean-Baptiste Boric if (strncmp(var->name, line, var_len) != 0)
146*a824f5a1SJean-Baptiste Boric continue;
147*a824f5a1SJean-Baptiste Boric if (line[var_len] != '=')
148*a824f5a1SJean-Baptiste Boric continue;
149*a824f5a1SJean-Baptiste Boric line += var_len + 1;
150*a824f5a1SJean-Baptiste Boric len -= var_len + 1;
151*a824f5a1SJean-Baptiste Boric if (config_tmp_variables[i])
152*a824f5a1SJean-Baptiste Boric value = xasprintf("%s\n%.*s",
153*a824f5a1SJean-Baptiste Boric config_tmp_variables[i], (int)len, line);
154*a824f5a1SJean-Baptiste Boric else
155*a824f5a1SJean-Baptiste Boric value = xasprintf("%.*s", (int)len, line);
156*a824f5a1SJean-Baptiste Boric free(config_tmp_variables[i]);
157*a824f5a1SJean-Baptiste Boric config_tmp_variables[i] = value;
158*a824f5a1SJean-Baptiste Boric break;
159*a824f5a1SJean-Baptiste Boric }
160*a824f5a1SJean-Baptiste Boric }
161*a824f5a1SJean-Baptiste Boric
162*a824f5a1SJean-Baptiste Boric for (i = 0; (var = &config_variables[i])->name != NULL; ++i) {
163*a824f5a1SJean-Baptiste Boric if (config_tmp_variables[i] == NULL)
164*a824f5a1SJean-Baptiste Boric continue;
165*a824f5a1SJean-Baptiste Boric *var->var = config_tmp_variables[i];
166*a824f5a1SJean-Baptiste Boric config_tmp_variables[i] = NULL;
167*a824f5a1SJean-Baptiste Boric }
168*a824f5a1SJean-Baptiste Boric
169*a824f5a1SJean-Baptiste Boric fclose(fp);
170*a824f5a1SJean-Baptiste Boric }
171*a824f5a1SJean-Baptiste Boric
172*a824f5a1SJean-Baptiste Boric void
pkg_install_config(void)173*a824f5a1SJean-Baptiste Boric pkg_install_config(void)
174*a824f5a1SJean-Baptiste Boric {
175*a824f5a1SJean-Baptiste Boric int do_cache_index;
176*a824f5a1SJean-Baptiste Boric char *value;
177*a824f5a1SJean-Baptiste Boric
178*a824f5a1SJean-Baptiste Boric parse_pkg_install_conf();
179*a824f5a1SJean-Baptiste Boric
180*a824f5a1SJean-Baptiste Boric if ((value = getenv("PKG_DBDIR")) != NULL)
181*a824f5a1SJean-Baptiste Boric pkgdb_set_dir(value, 2);
182*a824f5a1SJean-Baptiste Boric else if (config_pkg_dbdir != NULL)
183*a824f5a1SJean-Baptiste Boric pkgdb_set_dir(config_pkg_dbdir, 1);
184*a824f5a1SJean-Baptiste Boric config_pkg_dbdir = xstrdup(pkgdb_get_dir());
185*a824f5a1SJean-Baptiste Boric
186*a824f5a1SJean-Baptiste Boric if ((value = getenv("PKG_REFCOUNT_DBDIR")) != NULL)
187*a824f5a1SJean-Baptiste Boric config_pkg_refcount_dbdir = value;
188*a824f5a1SJean-Baptiste Boric else if (config_pkg_refcount_dbdir == NULL)
189*a824f5a1SJean-Baptiste Boric config_pkg_refcount_dbdir = xasprintf("%s.refcount",
190*a824f5a1SJean-Baptiste Boric pkgdb_get_dir());
191*a824f5a1SJean-Baptiste Boric
192*a824f5a1SJean-Baptiste Boric if (pkg_vulnerabilities_dir == NULL)
193*a824f5a1SJean-Baptiste Boric pkg_vulnerabilities_dir = pkgdb_get_dir();
194*a824f5a1SJean-Baptiste Boric pkg_vulnerabilities_file = xasprintf("%s/pkg-vulnerabilities",
195*a824f5a1SJean-Baptiste Boric pkg_vulnerabilities_dir);
196*a824f5a1SJean-Baptiste Boric if (pkg_vulnerabilities_url == NULL) {
197*a824f5a1SJean-Baptiste Boric pkg_vulnerabilities_url = xasprintf("%s/pkg-vulnerabilities.gz",
198*a824f5a1SJean-Baptiste Boric tnf_vulnerability_base);
199*a824f5a1SJean-Baptiste Boric }
200*a824f5a1SJean-Baptiste Boric if (verified_installation == NULL)
201*a824f5a1SJean-Baptiste Boric verified_installation = "never";
202*a824f5a1SJean-Baptiste Boric
203*a824f5a1SJean-Baptiste Boric if (check_vulnerabilities == NULL)
204*a824f5a1SJean-Baptiste Boric check_vulnerabilities = "never";
205*a824f5a1SJean-Baptiste Boric
206*a824f5a1SJean-Baptiste Boric if (do_license_check == NULL)
207*a824f5a1SJean-Baptiste Boric do_license_check = "no";
208*a824f5a1SJean-Baptiste Boric
209*a824f5a1SJean-Baptiste Boric if ((value = getenv("PKG_PATH")) != NULL)
210*a824f5a1SJean-Baptiste Boric config_pkg_path = value;
211*a824f5a1SJean-Baptiste Boric
212*a824f5a1SJean-Baptiste Boric if (strcasecmp(cache_index, "yes") == 0)
213*a824f5a1SJean-Baptiste Boric do_cache_index = 1;
214*a824f5a1SJean-Baptiste Boric else {
215*a824f5a1SJean-Baptiste Boric if (strcasecmp(cache_index, "no"))
216*a824f5a1SJean-Baptiste Boric warnx("Invalid value for configuration option "
217*a824f5a1SJean-Baptiste Boric "CACHE_INDEX");
218*a824f5a1SJean-Baptiste Boric do_cache_index = 0;
219*a824f5a1SJean-Baptiste Boric }
220*a824f5a1SJean-Baptiste Boric
221*a824f5a1SJean-Baptiste Boric if (config_cache_connections && *config_cache_connections) {
222*a824f5a1SJean-Baptiste Boric long v = strtol(config_cache_connections, &value, 10);
223*a824f5a1SJean-Baptiste Boric if (*value == '\0') {
224*a824f5a1SJean-Baptiste Boric if (v >= INT_MAX || v < 0)
225*a824f5a1SJean-Baptiste Boric v = -1;
226*a824f5a1SJean-Baptiste Boric cache_connections = v;
227*a824f5a1SJean-Baptiste Boric }
228*a824f5a1SJean-Baptiste Boric }
229*a824f5a1SJean-Baptiste Boric config_cache_connections = xasprintf("%d", cache_connections);
230*a824f5a1SJean-Baptiste Boric
231*a824f5a1SJean-Baptiste Boric if (config_cache_connections_host) {
232*a824f5a1SJean-Baptiste Boric long v = strtol(config_cache_connections_host, &value, 10);
233*a824f5a1SJean-Baptiste Boric if (*value == '\0') {
234*a824f5a1SJean-Baptiste Boric if (v >= INT_MAX || v < 0)
235*a824f5a1SJean-Baptiste Boric v = -1;
236*a824f5a1SJean-Baptiste Boric cache_connections_host = v;
237*a824f5a1SJean-Baptiste Boric }
238*a824f5a1SJean-Baptiste Boric }
239*a824f5a1SJean-Baptiste Boric config_cache_connections_host = xasprintf("%d", cache_connections_host);
240*a824f5a1SJean-Baptiste Boric
241*a824f5a1SJean-Baptiste Boric #ifndef BOOTSTRAP
242*a824f5a1SJean-Baptiste Boric fetchConnectionCacheInit(cache_connections, cache_connections_host);
243*a824f5a1SJean-Baptiste Boric #endif
244*a824f5a1SJean-Baptiste Boric
245*a824f5a1SJean-Baptiste Boric snprintf(fetch_flags, sizeof(fetch_flags), "%s%s%s%s",
246*a824f5a1SJean-Baptiste Boric (do_cache_index) ? "c" : "",
247*a824f5a1SJean-Baptiste Boric (verbose_netio && *verbose_netio) ? "v" : "",
248*a824f5a1SJean-Baptiste Boric (active_ftp && *active_ftp) ? "a" : "",
249*a824f5a1SJean-Baptiste Boric (ignore_proxy && *ignore_proxy) ? "d" : "");
250*a824f5a1SJean-Baptiste Boric }
251*a824f5a1SJean-Baptiste Boric
252*a824f5a1SJean-Baptiste Boric void
pkg_install_show_variable(const char * var_name)253*a824f5a1SJean-Baptiste Boric pkg_install_show_variable(const char *var_name)
254*a824f5a1SJean-Baptiste Boric {
255*a824f5a1SJean-Baptiste Boric struct config_variable *var;
256*a824f5a1SJean-Baptiste Boric const char *tmp_value = NULL;
257*a824f5a1SJean-Baptiste Boric
258*a824f5a1SJean-Baptiste Boric for (var = config_variables; var->name != NULL; ++var) {
259*a824f5a1SJean-Baptiste Boric if (strcmp(var->name, var_name) == 0)
260*a824f5a1SJean-Baptiste Boric break;
261*a824f5a1SJean-Baptiste Boric }
262*a824f5a1SJean-Baptiste Boric if (var->name == NULL) {
263*a824f5a1SJean-Baptiste Boric var->name = var_name;
264*a824f5a1SJean-Baptiste Boric var->var = &tmp_value;
265*a824f5a1SJean-Baptiste Boric }
266*a824f5a1SJean-Baptiste Boric
267*a824f5a1SJean-Baptiste Boric pkg_install_config();
268*a824f5a1SJean-Baptiste Boric
269*a824f5a1SJean-Baptiste Boric if (*var->var != NULL)
270*a824f5a1SJean-Baptiste Boric puts(*var->var);
271*a824f5a1SJean-Baptiste Boric }
272