1 /*	$NetBSD: parse-config.c,v 1.1.1.11 2010/06/26 00:14:31 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.11 2010/06/26 00:14:31 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_eol = "yes";
69 const char *check_vulnerabilities;
70 static const char *config_cache_connections;
71 static const char *config_cache_connections_host;
72 const char *config_pkg_dbdir;
73 const char *config_pkg_path;
74 const char *config_pkg_refcount_dbdir;
75 const char *do_license_check;
76 const char *verified_installation;
77 const char *gpg_cmd;
78 const char *gpg_keyring_pkgvuln;
79 const char *gpg_keyring_sign;
80 const char *gpg_keyring_verify;
81 const char *gpg_sign_as;
82 const char *pkg_vulnerabilities_dir;
83 const char *pkg_vulnerabilities_file;
84 const char *pkg_vulnerabilities_url;
85 const char *ignore_advisories = NULL;
86 const char tnf_vulnerability_base[] = "http://ftp.NetBSD.org/pub/NetBSD/packages/vulns";
87 const char *acceptable_licenses = NULL;
88 
89 static struct config_variable {
90 	const char *name;
91 	const char **var;
92 } config_variables[] = {
93 	{ "ACCEPTABLE_LICENSES", &acceptable_licenses },
94 	{ "ACTIVE_FTP", &active_ftp },
95 	{ "CACHE_INDEX", &cache_index },
96 	{ "CACHE_CONNECTIONS", &config_cache_connections },
97 	{ "CACHE_CONNECTIONS_HOST", &config_cache_connections_host },
98 	{ "CERTIFICATE_ANCHOR_PKGS", &certs_packages },
99 	{ "CERTIFICATE_ANCHOR_PKGVULN", &certs_pkg_vulnerabilities },
100 	{ "CERTIFICATE_CHAIN", &cert_chain_file },
101 	{ "CHECK_LICENSE", &do_license_check },
102 	{ "CHECK_END_OF_LIFE", &check_eol },
103 	{ "CHECK_VULNERABILITIES", &check_vulnerabilities },
104 	{ "DEFAULT_ACCEPTABLE_LICENSES", &default_acceptable_licenses },
105 	{ "GPG", &gpg_cmd },
106 	{ "GPG_KEYRING_PKGVULN", &gpg_keyring_pkgvuln },
107 	{ "GPG_KEYRING_SIGN", &gpg_keyring_sign },
108 	{ "GPG_KEYRING_VERIFY", &gpg_keyring_verify },
109 	{ "GPG_SIGN_AS", &gpg_sign_as },
110 	{ "IGNORE_PROXY", &ignore_proxy },
111 	{ "IGNORE_URL", &ignore_advisories },
112 	{ "PKG_DBDIR", &config_pkg_dbdir },
113 	{ "PKG_PATH", &config_pkg_path },
114 	{ "PKG_REFCOUNT_DBDIR", &config_pkg_refcount_dbdir },
115 	{ "PKGVULNDIR", &pkg_vulnerabilities_dir },
116 	{ "PKGVULNURL", &pkg_vulnerabilities_url },
117 	{ "VERBOSE_NETIO", &verbose_netio },
118 	{ "VERIFIED_INSTALLATION", &verified_installation },
119 	{ NULL, NULL }, /* For use by pkg_install_show_variable */
120 	{ NULL, NULL }
121 };
122 
123 char *config_tmp_variables[sizeof config_variables/sizeof config_variables[0]];
124 
125 static void
126 parse_pkg_install_conf(void)
127 {
128 	struct config_variable *var;
129 	FILE *fp;
130 	char *line, *value;
131 	size_t len, var_len, i;
132 
133 	fp = fopen(config_file, "r");
134 	if (!fp) {
135 		if (errno != ENOENT)
136 			warn("Can't open '%s' for reading", config_file);
137 		return;
138 	}
139 
140 	while ((line = fgetln(fp, &len)) != (char *) NULL) {
141 		if (line[len - 1] == '\n')
142 			--len;
143 		for (i = 0; (var = &config_variables[i])->name != NULL; ++i) {
144 			var_len = strlen(var->name);
145 			if (strncmp(var->name, line, var_len) != 0)
146 				continue;
147 			if (line[var_len] != '=')
148 				continue;
149 			line += var_len + 1;
150 			len -= var_len + 1;
151 			if (config_tmp_variables[i])
152 				value = xasprintf("%s\n%.*s",
153 				    config_tmp_variables[i], (int)len, line);
154 			else
155 				value = xasprintf("%.*s", (int)len, line);
156 			free(config_tmp_variables[i]);
157 			config_tmp_variables[i] = value;
158 			break;
159 		}
160 	}
161 
162 	for (i = 0; (var = &config_variables[i])->name != NULL; ++i) {
163 		if (config_tmp_variables[i] == NULL)
164 			continue;
165 		*var->var = config_tmp_variables[i];
166 		config_tmp_variables[i] = NULL;
167 	}
168 
169 	fclose(fp);
170 }
171 
172 void
173 pkg_install_config(void)
174 {
175 	int do_cache_index;
176 	char *value;
177 
178 	parse_pkg_install_conf();
179 
180 	if ((value = getenv("PKG_DBDIR")) != NULL)
181 		pkgdb_set_dir(value, 2);
182 	else if (config_pkg_dbdir != NULL)
183 		pkgdb_set_dir(config_pkg_dbdir, 1);
184 	config_pkg_dbdir = xstrdup(pkgdb_get_dir());
185 
186 	if ((value = getenv("PKG_REFCOUNT_DBDIR")) != NULL)
187 		config_pkg_refcount_dbdir = value;
188 	else if (config_pkg_refcount_dbdir == NULL)
189 		config_pkg_refcount_dbdir = xasprintf("%s.refcount",
190 		    pkgdb_get_dir());
191 
192 	if (pkg_vulnerabilities_dir == NULL)
193 		pkg_vulnerabilities_dir = pkgdb_get_dir();
194 	pkg_vulnerabilities_file = xasprintf("%s/pkg-vulnerabilities",
195 	    pkg_vulnerabilities_dir);
196 	if (pkg_vulnerabilities_url == NULL) {
197 		pkg_vulnerabilities_url = xasprintf("%s/pkg-vulnerabilities.gz",
198 		    tnf_vulnerability_base);
199 	}
200 	if (verified_installation == NULL)
201 		verified_installation = "never";
202 
203 	if (check_vulnerabilities == NULL)
204 		check_vulnerabilities = "never";
205 
206 	if (do_license_check == NULL)
207 		do_license_check = "no";
208 
209 	if ((value = getenv("PKG_PATH")) != NULL)
210 		config_pkg_path = value;
211 
212 	if (strcasecmp(cache_index, "yes") == 0)
213 		do_cache_index = 1;
214 	else {
215 		if (strcasecmp(cache_index, "no"))
216 			warnx("Invalid value for configuration option "
217 			    "CACHE_INDEX");
218 		do_cache_index = 0;
219 	}
220 
221 	if (config_cache_connections && *config_cache_connections) {
222 		long v = strtol(config_cache_connections, &value, 10);
223 		if (*value == '\0') {
224 			if (v >= INT_MAX || v < 0)
225 				v = -1;
226 			cache_connections = v;
227 		}
228 	}
229 	config_cache_connections = xasprintf("%d", cache_connections);
230 
231 	if (config_cache_connections_host) {
232 		long v = strtol(config_cache_connections_host, &value, 10);
233 		if (*value == '\0') {
234 			if (v >= INT_MAX || v < 0)
235 				v = -1;
236 			cache_connections_host = v;
237 		}
238 	}
239 	config_cache_connections_host = xasprintf("%d", cache_connections_host);
240 
241 #ifndef BOOTSTRAP
242 	fetchConnectionCacheInit(cache_connections, cache_connections_host);
243 #endif
244 
245 	snprintf(fetch_flags, sizeof(fetch_flags), "%s%s%s%s",
246 	    (do_cache_index) ? "c" : "",
247 	    (verbose_netio && *verbose_netio) ? "v" : "",
248 	    (active_ftp && *active_ftp) ? "a" : "",
249 	    (ignore_proxy && *ignore_proxy) ? "d" : "");
250 }
251 
252 void
253 pkg_install_show_variable(const char *var_name)
254 {
255 	struct config_variable *var;
256 	const char *tmp_value = NULL;
257 
258 	for (var = config_variables; var->name != NULL; ++var) {
259 		if (strcmp(var->name, var_name) == 0)
260 			break;
261 	}
262 	if (var->name == NULL) {
263 		var->name = var_name;
264 		var->var = &tmp_value;
265 	}
266 
267 	pkg_install_config();
268 
269 	if (*var->var != NULL)
270 		puts(*var->var);
271 }
272