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