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