Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
HardenedBSD-pkg src info.c
/*-
 * Copyright (c) 2011-2014 Baptiste Daroussin <bapt@FreeBSD.org>
 * Copyright (c) 2011-2012 Julien Laffaye <jlaffaye@FreeBSD.org>
 * Copyright (c) 2011 Philippe Pepiot <phil@philpep.org>
 * Copyright (c) 2011-2012 Marin Atanasov Nikolov <dnaeon@gmail.com>
 * Copyright (c) 2013-2014 Matthew Seaman <matthew@FreeBSD.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer
 *    in this position and unchanged.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#if __has_include(<sys/capsicum.h>)
#include <sys/capsicum.h>
#define HAVE_CAPSICUM 1
#endif

#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <pkg.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "pkgcli.h"

enum sign {
	LT,
	LE,
	GT,
	GE,
	EQ
};

static bool
version_match(const char *version, const char *constraint, int sign)
{
	switch (pkg_version_cmp(version, constraint)) {
	case -1:
		return (sign == LT || sign == LE);
	case 0:
		return (sign == LE || sign == GE || sign == EQ);
	case 1:
		return (sign == GT || sign == GE);
	}
	return (false);
}

static void
parse_version_op(char *pkgname, int j, char **verp, int *signp)
{
	int base_sign;

	switch (pkgname[j]) {
	case '<': base_sign = LT; break;
	case '>': base_sign = GT; break;
	case '=': base_sign = EQ; break;
	default: return;
	}

	*verp = pkgname + j;
	*signp = base_sign;
	(*verp)[0] = '\0';
	(*verp)++;
	if ((*verp)[0] == '=' && base_sign != EQ) {
		(*verp)++;
		*signp = (base_sign == LT) ? LE : GE;
	} else if ((*verp)[0] == '=') {
		(*verp)++;
	}
}

void
usage_info(void)
{
	fprintf(stderr, "Usage: pkg info <pkg-name>\n");
	fprintf(stderr, "       pkg info -a\n");
	fprintf(stderr, "       pkg info [-AbBDdefIklOqRrs] [-Cgix] <pkg-name>\n");
	fprintf(stderr, "       pkg info [-AbBDdfIlqRrs] -F <pkg-file>\n\n");
	fprintf(stderr, "For more information see 'pkg help info'.\n");
}

/*
 * list of options
 * -S <type> : show scripts, type can be pre-install etc: TODO
 */

int
exec_info(int argc, char **argv)
{
	struct pkgdb *db = NULL;
	struct pkgdb_it *it = NULL;
	int query_flags;
	struct pkg *pkg = NULL;
	uint64_t opt = INFO_TAG_NAMEVER;
	match_t match = MATCH_GLOB;
	char *pkgname;
	char *pkgversion = NULL, *pkgversion2 = NULL;
	const char *file = NULL;
	int ch, fd;
	int ret = EPKG_OK;
	int retcode = 0;
	bool gotone = false;
	int i, j;
	int sign = 0;
	int sign2 = 0;
	int open_flags = 0;
	bool pkg_exists = false;
	bool origin_search = false;
	bool e_flag = false;
	bool json_array = false;
	bool json_first = true;
#ifdef HAVE_CAPSICUM
	cap_rights_t rights;
#endif

	struct option longopts[] = {
		{ "all",		no_argument,		NULL,	'a' },
		{ "annotations",	no_argument,		NULL,	'A' },
		{ "provided-shlibs",	no_argument,		NULL,	'b' },
		{ "required-shlibs",	no_argument,		NULL,	'B' },
		{ "case-sensitive",	no_argument,		NULL,	'C' },
		{ "dependencies",	no_argument,		NULL,	'd' },
		{ "pkg-message",	no_argument,		NULL,	'D' },
		{ "exists",		no_argument,		NULL,	'e' },
		{ "show-name-only",	no_argument,		NULL,	'E' },
		{ "full",		no_argument,		NULL,	'f' },
		{ "file",		required_argument,	NULL,	'F' },
		{ "glob",		no_argument,		NULL,	'g' },
		{ "case-insensitive",	no_argument,		NULL,	'i' },
		{ "comment",		no_argument,		NULL,	'I' },
		{ "locked",		no_argument,		NULL,	'k' },
		{ "list-files",		no_argument,		NULL,	'l' },
		{ "origin",		no_argument,		NULL,	'o' },
		{ "by-origin",		no_argument,		NULL,	'O' },
		{ "prefix",		no_argument,		NULL,	'p' },
		{ "quiet",		no_argument,		NULL,	'q' },
		{ "required-by",	no_argument,		NULL,	'r' },
		{ "raw",		no_argument,		NULL,	'R' },
		{ "size",		no_argument,		NULL,	's' },
		{ "regex",		no_argument,		NULL,	'x' },
		{ "raw-format",		required_argument,	NULL, 	1   },
		{ NULL,			0,			NULL,	0   },
	};

	/* TODO: exclusive opts ? */
	while ((ch = getopt_long(argc, argv, "+aAbBCdDeEfF:giIkloOpqrRsx", longopts, NULL)) != -1) {
		switch (ch) {
		case 'a':
			match = MATCH_ALL;
			break;
		case 'A':
			opt |= INFO_ANNOTATIONS;
			break;
		case 'b':
			opt |= INFO_SHLIBS_PROVIDED;
			break;
		case 'B':
			opt |= INFO_SHLIBS_REQUIRED;
			break;
		case 'C':
			pkgdb_set_case_sensitivity(true);
			break;
		case 'd':
			opt |= INFO_DEPS;
			break;
		case 'D':
			opt |= INFO_MESSAGE;
			break;
		case 'e':
			pkg_exists = true;
			break;
		case 'E': /* ports compatibility */
			e_flag = true;
			break;
		case 'f':
			opt |= INFO_FULL;
			break;
		case 'F':
			file = optarg;
			break;
		case 'g':
			match = MATCH_GLOB;
			break;
		case 'i':
			pkgdb_set_case_sensitivity(false);
			break;
		case 'I':
			opt |= INFO_COMMENT;
			break;
		case 'k':
			opt |= INFO_LOCKED;
			break;
		case 'l':
			opt |= INFO_FILES;
			break;
		case 'o':
			opt |= INFO_ORIGIN;
			break;
		case 'O':
			origin_search = true;  /* only for ports compat */
			break;
		case 'p':
			opt |= INFO_PREFIX;
			break;
		case 'q':
			quiet = true;
			break;
		case 'r':
			opt |= INFO_RDEPS;
			break;
		case 'R':
			opt |= INFO_RAW;
			break;
		case 's':
			opt |= INFO_FLATSIZE;
			break;
		case 'x':
			match = MATCH_REGEX;
			break;
		case 1:
			if (STRIEQ(optarg, "json"))
				opt |= INFO_RAW_JSON;
			else if (STRIEQ(optarg, "json-compact"))
				opt |= INFO_RAW_JSON_COMPACT;
			else if (STRIEQ(optarg, "yaml"))
				opt |= INFO_RAW_YAML;
			else if (STRIEQ(optarg, "ucl"))
				opt |= INFO_RAW_UCL;
			else
				errx(EXIT_FAILURE, "Invalid format '%s' for the "
				    "raw output, expecting json, json-compact "
				    "or yaml", optarg);
			break;
		default:
			usage_info();
			return(EXIT_FAILURE);
		}
	}

	if (argc == 1 || (argc == 2 && quiet))
		match = MATCH_ALL;

	argc -= optind;
	argv += optind;

	if (argc == 0 && file == NULL && match != MATCH_ALL) {
		/* which -O bsd.*.mk always expect clean output */
		if (origin_search)
			return (EXIT_SUCCESS);
		usage_info();
		return (EXIT_FAILURE);
	}

	/* When no other data is requested, default is to print
	 * 'name-ver comment' For -O, just print name-ver */
	if (!origin_search && (opt & INFO_ALL) == 0 && match == MATCH_ALL &&
	    !quiet)
		opt |= INFO_COMMENT;

	/* Special compatibility: handle -O and -q -O */
	if (origin_search) {
		if (quiet) {
			opt = INFO_TAG_NAMEVER;
			quiet = false;
		} else {
			opt = INFO_TAG_NAMEVER|INFO_COMMENT;
		}
	}

	if (match == MATCH_ALL && opt == INFO_TAG_NAMEVER)
		quiet = false;

	if (opt & INFO_RAW) {
		if ((opt & (INFO_RAW_JSON|INFO_RAW_JSON_COMPACT|INFO_RAW_UCL)) == 0)
			opt |= INFO_RAW_YAML;
	}

	if (file != NULL) {
		if ((fd = open(file, O_RDONLY)) == -1) {
			warn("Unable to open %s", file);
			return (EXIT_FAILURE);
		}

		pkg_drop_privileges();
#ifdef HAVE_CAPSICUM
		cap_rights_init(&rights, CAP_READ, CAP_FSTAT);
		if (cap_rights_limit(fd, &rights) < 0 && errno != ENOSYS ) {
			warn("cap_rights_limit() failed");
			close(fd);
			return (EXIT_FAILURE);
		}

#ifndef COVERAGE
		if (cap_enter() < 0 && errno != ENOSYS) {
			warn("cap_enter() failed");
			close(fd);
			return (EXIT_FAILURE);
		}
#endif
#endif
		if (opt == INFO_TAG_NAMEVER)
			opt |= INFO_FULL;

		if ((opt & (INFO_RAW | INFO_FILES |
				INFO_DIRS)) == 0)
			open_flags = PKG_OPEN_MANIFEST_COMPACT;

		if (pkg_open_fd(&pkg, fd, open_flags) != EPKG_OK) {
			close(fd);
			return (1);
		}
		print_info(NULL, pkg, opt);
		close(fd);
		pkg_free(pkg);
		return (EXIT_SUCCESS);
	}

	ret = pkgdb_access(PKGDB_MODE_READ, PKGDB_DB_LOCAL);
	if (ret == EPKG_ENOACCESS) {
		warnx("Insufficient privileges to query the package database");
		return (EXIT_FAILURE);
	} else if (ret == EPKG_ENODB) {
		if (match == MATCH_ALL)
			return (EXIT_SUCCESS);
		if (origin_search)
			return (EXIT_SUCCESS);
		if (!quiet)
			warnx("No packages installed");
		return (EXIT_FAILURE);
	} else if (ret != EPKG_OK)
		return (EXIT_FAILURE);
	ret = pkgdb_open(&db, PKGDB_DEFAULT_READONLY);
	if (ret != EPKG_OK)
		return (EXIT_FAILURE);

	pkg_drop_privileges();
	if (!pkgdb_lock_or_fail(db, PKGDB_LOCK_READONLY))
		return (EXIT_FAILURE);

	if ((opt & INFO_RAW) &&
	    (opt & (INFO_RAW_JSON | INFO_RAW_JSON_COMPACT))) {
		json_array = true;
		printf("[");
	}

	i = 0;
	do {
		gotone = false;
		pkgname = argv[i];

		/*
		 * allow to search for origin with a trailing /
		 * likes audio/linux-vsound depending on ${PORTSDIR}/audio/sox/
		 */
		if (argc > 0 && pkgname[strlen(pkgname) -1] == '/')
			pkgname[strlen(pkgname) -1] = '\0';

		if (argc > 0) {
			for (j = 0; pkgname[j] != '\0'; j++) {
				if (pkgname[j] == '<' || pkgname[j] == '>' || pkgname[j] == '=') {
					if (pkgversion) {
						parse_version_op(pkgname, j, &pkgversion2, &sign2);
						j = pkgversion2 - pkgname - 1;
					} else {
						parse_version_op(pkgname, j, &pkgversion, &sign);
						j = pkgversion - pkgname - 1;
					}
				}
			}
		}

		if (match != MATCH_ALL && pkgname[0] == '\0') {
			fprintf(stderr, "Pattern must not be empty.\n");
			i++;
			continue;
		}

		if ((it = pkgdb_query(db, pkgname, match)) == NULL) {
			goto cleanup;
		}

		/* this is place for compatibility hacks */

		/* ports infrastructure expects pkg info -q -O to
		 * always return 0 even if the ports doesn't exists */

		if (origin_search)
			gotone = true;

		/* end of compatibility hacks */

		/*
		 * only show full version in case of match glob with a
		 * single argument specified which does not contains
		 * any glob pattern
		 */
		if (argc == 1 && !origin_search && !quiet && !e_flag &&
		    match == MATCH_GLOB &&
		    strcspn(pkgname, "*[]{}()") == strlen(pkgname) &&
		    opt == INFO_TAG_NAMEVER)
			opt |= INFO_FULL;

		query_flags = info_flags(opt, false);
		while ((ret = pkgdb_it_next(it, &pkg, query_flags)) == EPKG_OK) {
			gotone = true;
			const char *version;

			pkg_get(pkg, PKG_ATTR_VERSION, &version);
			if (pkgversion != NULL && !version_match(version, pkgversion, sign)) {
				gotone = false;
				continue;
			}
			if (pkgversion2 != NULL && !version_match(version, pkgversion2, sign2)) {
				gotone = false;
				continue;
			}
			if (pkg_exists) {
				if (retcode != EXIT_FAILURE)
					retcode = EXIT_SUCCESS;
			} else {
				if (json_array) {
					if (!json_first)
						printf(",");
					json_first = false;
				}
				print_info(db, pkg, opt);
			}
		}
		if (ret != EPKG_END) {
			retcode = EXIT_FAILURE;
		}

		if (!gotone && match != MATCH_ALL) {
			if (!quiet && !pkg_exists)
				warnx("No package(s) matching %s", argv[i]);
			retcode = EXIT_FAILURE;
		}

		pkgdb_it_free(it);

		i++;
	} while (i < argc);

	if (json_array)
		printf("]\n");

cleanup:
	pkg_free(pkg);

	pkgdb_release_lock(db, PKGDB_LOCK_READONLY);
	pkgdb_close(db);

	return (retcode);
}