1/* $NetBSD: license.c,v 1.12 2024/06/11 09:26:57 wiz Exp $ */ 2 3/*- 4 * Copyright (c) 2009 Joerg Sonnenberger <joerg@NetBSD.org>. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 22 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 28 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32#if HAVE_CONFIG_H 33#include "config.h" 34#endif 35 36#include <nbcompat.h> 37 38#if HAVE_ERR_H 39#include <err.h> 40#endif 41#include <stdlib.h> 42#include <string.h> 43 44#include "lib.h" 45 46#define HASH_SIZE 521 47 48const char *default_acceptable_licenses = 49 "afl-3.0 " 50 "apache-1.1 apache-2.0 " 51 "arphic-public " 52 "artistic artistic-2.0 " 53 "beer-ware " 54 "boost-license " 55 "cc-by-sa-v3.0 " 56 "cc-by-sa-v4.0 " 57 "cc-by-v4.0 " 58 "cc0-1.0-universal " 59 "cddl-1.0 " 60 "cecill-2.1 " 61 "cecill-b-v1 " 62 "cecill-c-v1 " 63 "cpl-1.0 " 64 "epl-v1.0 " 65 "eupl-v1.1 " 66 "eupl-v1.2 " 67 "gfsl " 68 "gnu-fdl-v1.1 gnu-fdl-v1.2 gnu-fdl-v1.3 " 69 "gnu-gpl-v1 " 70 "gnu-gpl-v2 gnu-lgpl-v2 gnu-lgpl-v2.1 " 71 "gnu-gpl-v3 gnu-lgpl-v3 " 72 "happy " 73 "hpnd " 74 "icu " 75 "ijg " 76 "info-zip " 77 "ipafont " 78 "ipl-1.0 " 79 "isc " 80 "lppl-1.0 lppl-1.2 lppl-1.3c " 81 "lucent " 82 "miros " 83 "mit " 84 "mpl-1.0 mpl-1.1 mpl-2.0 " 85 "mplusfont " 86 "ms-pl " 87 "odbl-v1 " 88 "ofl-v1.0 ofl-v1.1 " 89 "openssl " 90 "original-bsd modified-bsd 2-clause-bsd 0-clause-bsd " 91 "osl " 92 "paratype " 93 "php " 94 "png-license " 95 "postgresql-license " 96 "public-domain " 97 "python-software-foundation " 98 "qpl-v1.0 " 99 "sgi-free-software-b-v2.0 " 100 "sissl-1.1 " 101 "sleepycat-public " 102 "unicode " 103 "unicode-v3 " 104 "unlicense " 105 "vera-ttf-license " 106 "w3c " 107 "x11 " 108 "zlib " 109 "zpl-2.0 zpl-2.1 " 110 "zsh"; 111 112#ifdef DEBUG 113static size_t hash_collisions; 114#endif 115 116static char **license_hash[HASH_SIZE]; 117static const char license_spaces[] = " \t\n"; 118static const char license_chars[] = 119 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-."; 120 121static size_t 122hash_license(const char *license, size_t len) 123{ 124 size_t hash; 125 126 for (hash = 0; *license && len; ++license, --len) 127 hash = *license + hash * 32; 128 return hash % HASH_SIZE; 129} 130 131static void 132add_license_internal(const char *license, size_t len) 133{ 134 char *new_license; 135 size_t slot, i; 136 137 slot = hash_license(license, len); 138 139 new_license = malloc(len + 1); 140 memcpy(new_license, license, len); 141 new_license[len] = '\0'; 142 143 if (license_hash[slot] == NULL) { 144 license_hash[slot] = calloc(sizeof(char *), 2); 145 license_hash[slot][0] = new_license; 146 } else { 147 for (i = 0; license_hash[slot][i]; ++i) { 148 if (!memcmp(license_hash[slot][i], license, len) && 149 license_hash[slot][i][len] == '\0') { 150 free(new_license); 151 return; 152 } 153 } 154 155#ifdef DEBUG 156 ++hash_collisions; 157#endif 158 159 license_hash[slot] = realloc(license_hash[slot], 160 sizeof(char *) * (i + 2)); 161 license_hash[slot][i] = new_license; 162 license_hash[slot][i + 1] = NULL; 163 } 164} 165 166int 167add_licenses(const char *line) 168{ 169 const char *next; 170 171 if (line == NULL) 172 return 0; 173 174 for (line += strspn(line, license_spaces); line; ) { 175 next = line + strspn(line, license_chars); 176 if (next == line) 177 return *line ? -1 : 0; 178 add_license_internal(line, next - line); 179 line = next + strspn(next, license_spaces); 180 if (next == line) 181 return *line ? -1 : 0; 182 } 183 return 0; 184} 185 186static int 187acceptable_license_internal(const char *license, size_t len) 188{ 189 size_t slot, i; 190 191 slot = hash_license(license, len); 192 193 if (license_hash[slot] == NULL) 194 return 0; 195 196 for (i = 0; license_hash[slot][i]; ++i) { 197 if (strncmp(license_hash[slot][i], license, len) == 0 && 198 license_hash[slot][i][len] == '\0') 199 return 1; 200 } 201 202 return 0; 203} 204 205int 206acceptable_license(const char *license) 207{ 208 size_t len; 209 210 len = strlen(license); 211 if (strspn(license, license_chars) != len) { 212 warnx("Invalid character in license name at position %" PRIzu, len); 213 return -1; 214 } 215 216 return acceptable_license_internal(license, len); 217} 218 219static int 220acceptable_pkg_license_internal(const char **licensep, int toplevel, const char *start) 221{ 222 const char *license = *licensep; 223 int need_parenthesis, is_true = 0; 224 int expr_type = 0; /* 0: unset, 1: or, 2: and */ 225 size_t len; 226 227 license += strspn(license, license_spaces); 228 229 if (*license == '(' && !toplevel) { 230 need_parenthesis = 1; 231 ++license; 232 license += strspn(license, license_spaces); 233 } else { 234 need_parenthesis = 0; 235 } 236 237 for (;;) { 238 if (*license == '(') { 239 switch (acceptable_pkg_license_internal(&license, 0, start)) { 240 case -1: 241 return -1; 242 case 0: 243 if (expr_type == 2) 244 is_true = 0; 245 break; 246 case 1: 247 is_true = 1; 248 break; 249 } 250 license += strspn(license, license_spaces); 251 } else { 252 len = strspn(license, license_chars); 253 if (len == 0) { 254 warnx("Invalid character in license name at position %" PRIzu, license - start + 1); 255 return -1; 256 } 257 258 if (acceptable_license_internal(license, len)) { 259 if (expr_type != 2) 260 is_true = 1; 261 } else if (expr_type == 2) { 262 is_true = 0; 263 } 264 265 license += len; 266 267 len = strspn(license, license_spaces); 268 if (len == 0 && *license && *license != ')') { 269 warnx("Missing space at position %" PRIzu, license - start + 1); 270 return -1; 271 } 272 license += len; 273 } 274 275 if (*license == ')') { 276 if (!need_parenthesis) { 277 warnx("Missing open parenthesis at position %" PRIzu, license - start + 1); 278 return -1; 279 } 280 *licensep = license + 1; 281 return is_true; 282 } 283 if (*license == '\0') { 284 if (need_parenthesis) { 285 warnx("Unbalanced parenthesis at position %" PRIzu, license - start + 1); 286 return -1; 287 } 288 *licensep = license; 289 return is_true; 290 } 291 292 if (strncmp(license, "AND", 3) == 0) { 293 if (expr_type == 1) { 294 warnx("Invalid operator in OR expression at position %" PRIzu, license - start + 1); 295 return -1; 296 } 297 expr_type = 2; 298 license += 3; 299 } else if (strncmp(license, "OR", 2) == 0) { 300 if (expr_type == 2) { 301 warnx("Invalid operator in AND expression at position %" PRIzu, license - start + 1); 302 return -1; 303 } 304 expr_type = 1; 305 license += 2; 306 } else { 307 warnx("Invalid operator at position %" PRIzu, license - start + 1); 308 return -1; 309 } 310 len = strspn(license, license_spaces); 311 if (len == 0 && *license != '(') { 312 warnx("Missing space at position %" PRIzu, license - start + 1); 313 return -1; 314 } 315 license += len; 316 } 317} 318 319int 320acceptable_pkg_license(const char *license) 321{ 322 int ret; 323 324 ret = acceptable_pkg_license_internal(&license, 1, license); 325 if (ret == -1) 326 return -1; 327 license += strspn(license, license_spaces); 328 if (*license) { 329 warnx("Trailing garbage in license specification"); 330 return -1; 331 } 332 return ret; 333} 334 335void 336load_license_lists(void) 337{ 338 if (add_licenses(getenv("PKGSRC_ACCEPTABLE_LICENSES"))) 339 errx(EXIT_FAILURE, "syntax error in PKGSRC_ACCEPTABLE_LICENSES"); 340 if (add_licenses(acceptable_licenses)) 341 errx(EXIT_FAILURE, "syntax error in ACCEPTABLE_LICENSES"); 342 if (add_licenses(getenv("PKGSRC_DEFAULT_ACCEPTABLE_LICENSES"))) 343 errx(EXIT_FAILURE, "syntax error in PKGSRC_DEFAULT_ACCEPTABLE_LICENSES"); 344 if (add_licenses(default_acceptable_licenses)) 345 errx(EXIT_FAILURE, "syntax error in DEFAULT_ACCEPTABLE_LICENSES"); 346} 347