pkg_install-20091015:
[netbsd-mini2440.git] / external / bsd / pkg_install / dist / lib / parse-config.c
blobb6fe359cd4f1cd5724064046d104d5c2757a4170
1 /* $NetBSD: parse-config.c,v 1.11 2009/10/15 12:41:41 joerg Exp $ */
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.11 2009/10/15 12:41:41 joerg Exp $");
12 /*-
13 * Copyright (c) 2008, 2009 Joerg Sonnenberger <joerg@NetBSD.org>.
14 * All rights reserved.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
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.
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.
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
49 #include "lib.h"
51 const char *config_file = SYSCONFDIR"/pkg_install.conf";
53 char fetch_flags[10] = ""; /* Workaround Mac OS X linker issues with BSS */
54 static const char *active_ftp;
55 static const char *verbose_netio;
56 static const char *ignore_proxy;
57 const char *cache_index = "yes";
58 const char *cert_chain_file;
59 const char *certs_packages;
60 const char *certs_pkg_vulnerabilities;
61 const char *check_vulnerabilities;
62 const char *config_pkg_path;
63 const char *do_license_check;
64 const char *verified_installation;
65 const char *gpg_cmd;
66 const char *gpg_keyring_pkgvuln;
67 const char *gpg_keyring_sign;
68 const char *gpg_keyring_verify;
69 const char *gpg_sign_as;
70 const char *pkg_vulnerabilities_dir;
71 const char *pkg_vulnerabilities_file;
72 const char *pkg_vulnerabilities_url;
73 const char *ignore_advisories = NULL;
74 const char tnf_vulnerability_base[] = "http://ftp.NetBSD.org/pub/NetBSD/packages/vulns";
75 const char *acceptable_licenses = NULL;
77 static struct config_variable {
78 const char *name;
79 const char **var;
80 } config_variables[] = {
81 { "ACCEPTABLE_LICENSES", &acceptable_licenses },
82 { "ACTIVE_FTP", &active_ftp },
83 { "CACHE_INDEX", &cache_index },
84 { "CERTIFICATE_ANCHOR_PKGS", &certs_packages },
85 { "CERTIFICATE_ANCHOR_PKGVULN", &certs_pkg_vulnerabilities },
86 { "CERTIFICATE_CHAIN", &cert_chain_file },
87 { "CHECK_LICENSE", &do_license_check },
88 { "CHECK_VULNERABILITIES", &check_vulnerabilities },
89 { "DEFAULT_ACCEPTABLE_LICENSES", &default_acceptable_licenses },
90 { "GPG", &gpg_cmd },
91 { "GPG_KEYRING_PKGVULN", &gpg_keyring_pkgvuln },
92 { "GPG_KEYRING_SIGN", &gpg_keyring_sign },
93 { "GPG_KEYRING_VERIFY", &gpg_keyring_verify },
94 { "GPG_SIGN_AS", &gpg_sign_as },
95 { "IGNORE_PROXY", &ignore_proxy },
96 { "IGNORE_URL", &ignore_advisories },
97 { "PKG_PATH", &config_pkg_path },
98 { "PKGVULNDIR", &pkg_vulnerabilities_dir },
99 { "PKGVULNURL", &pkg_vulnerabilities_url },
100 { "VERBOSE_NETIO", &verbose_netio },
101 { "VERIFIED_INSTALLATION", &verified_installation },
102 { NULL, NULL }
105 char *config_tmp_variables[sizeof config_variables/sizeof config_variables[0]];
107 static void
108 parse_pkg_install_conf(void)
110 struct config_variable *var;
111 FILE *fp;
112 char *line, *value;
113 size_t len, var_len, i;
115 fp = fopen(config_file, "r");
116 if (!fp) {
117 if (errno != ENOENT)
118 warn("Can't open '%s' for reading", config_file);
119 return;
122 while ((line = fgetln(fp, &len)) != (char *) NULL) {
123 if (line[len - 1] == '\n')
124 --len;
125 for (i = 0; (var = &config_variables[i])->name != NULL; ++i) {
126 var_len = strlen(var->name);
127 if (strncmp(var->name, line, var_len) != 0)
128 continue;
129 if (line[var_len] != '=')
130 continue;
131 line += var_len + 1;
132 len -= var_len + 1;
133 if (config_tmp_variables[i])
134 value = xasprintf("%s\n%.*s",
135 config_tmp_variables[i], (int)len, line);
136 else
137 value = xasprintf("%.*s", (int)len, line);
138 free(config_tmp_variables[i]);
139 config_tmp_variables[i] = value;
140 break;
144 for (i = 0; (var = &config_variables[i])->name != NULL; ++i) {
145 if (config_tmp_variables[i] == NULL)
146 continue;
147 *var->var = config_tmp_variables[i];
148 config_tmp_variables[i] = NULL;
151 fclose(fp);
154 void
155 pkg_install_config(void)
157 int do_cache_index;
158 char *value;
159 parse_pkg_install_conf();
161 if (pkg_vulnerabilities_dir == NULL)
162 pkg_vulnerabilities_dir = _pkgdb_getPKGDB_DIR();
163 pkg_vulnerabilities_file = xasprintf("%s/pkg-vulnerabilities",
164 pkg_vulnerabilities_dir);
165 if (pkg_vulnerabilities_url == NULL) {
166 pkg_vulnerabilities_url = xasprintf("%s/pkg-vulnerabilities.gz",
167 tnf_vulnerability_base);
169 if (verified_installation == NULL)
170 verified_installation = "never";
172 if (check_vulnerabilities == NULL)
173 check_vulnerabilities = "never";
175 if (do_license_check == NULL)
176 do_license_check = "no";
178 if ((value = getenv("PKG_PATH")) != NULL)
179 config_pkg_path = value;
181 if (strcasecmp(cache_index, "yes") == 0)
182 do_cache_index = 1;
183 else {
184 if (strcasecmp(cache_index, "no"))
185 warnx("Invalid value for configuration option "
186 "CACHE_INDEX");
187 do_cache_index = 0;
190 snprintf(fetch_flags, sizeof(fetch_flags), "%s%s%s%s",
191 (do_cache_index) ? "c" : "",
192 (verbose_netio && *verbose_netio) ? "v" : "",
193 (active_ftp && *active_ftp) ? "a" : "",
194 (ignore_proxy && *ignore_proxy) ? "d" : "");
197 void
198 pkg_install_show_variable(const char *var_name)
200 struct config_variable *var;
202 for (var = config_variables; var->name != NULL; ++var) {
203 if (strcmp(var->name, var_name) != 0)
204 continue;
205 if (*var->var != NULL)
206 puts(*var->var);