2013-06-10 13:58:39 +05:30
|
|
|
/*-
|
2015-01-10 11:56:23 +05:30
|
|
|
* Copyright (c) 2012-2015 Juan Romero Pardines.
|
2013-06-10 13:58:39 +05:30
|
|
|
* 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.
|
|
|
|
* 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 ``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 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2013-12-24 15:13:55 +05:30
|
|
|
#include <libgen.h>
|
2013-08-29 18:00:14 +05:30
|
|
|
#include <fcntl.h>
|
2013-06-10 13:58:39 +05:30
|
|
|
|
2013-12-24 15:13:55 +05:30
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/sha.h>
|
|
|
|
#include <openssl/rsa.h>
|
|
|
|
#include <openssl/ssl.h>
|
|
|
|
#include <openssl/pem.h>
|
|
|
|
|
2013-06-10 13:58:39 +05:30
|
|
|
#include "xbps_api_impl.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file lib/repo.c
|
|
|
|
* @brief Repository functions
|
|
|
|
* @defgroup repo Repository functions
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
xbps_repo_path(struct xbps_handle *xhp, const char *url)
|
|
|
|
{
|
|
|
|
assert(xhp);
|
|
|
|
assert(url);
|
|
|
|
|
|
|
|
return xbps_xasprintf("%s/%s-repodata",
|
|
|
|
url, xhp->target_arch ? xhp->target_arch : xhp->native_arch);
|
|
|
|
}
|
|
|
|
|
2013-10-07 13:49:04 +05:30
|
|
|
static xbps_dictionary_t
|
2013-12-12 04:48:08 +05:30
|
|
|
repo_get_dict(struct xbps_repo *repo)
|
2013-10-07 13:49:04 +05:30
|
|
|
{
|
|
|
|
struct archive_entry *entry;
|
|
|
|
int rv;
|
|
|
|
|
2014-01-22 16:23:08 +05:30
|
|
|
if (repo->ar == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2013-12-12 04:48:08 +05:30
|
|
|
rv = archive_read_next_header(repo->ar, &entry);
|
|
|
|
if (rv != ARCHIVE_OK) {
|
|
|
|
xbps_dbg_printf(repo->xhp,
|
|
|
|
"%s: read_next_header %s\n", repo->uri,
|
|
|
|
archive_error_string(repo->ar));
|
2013-10-07 13:49:04 +05:30
|
|
|
return NULL;
|
2013-12-12 04:48:08 +05:30
|
|
|
}
|
2015-01-10 23:41:31 +05:30
|
|
|
return xbps_archive_get_dictionary(repo->ar, entry);
|
2013-10-07 13:49:04 +05:30
|
|
|
}
|
|
|
|
|
2015-03-25 16:30:59 +05:30
|
|
|
bool
|
|
|
|
xbps_repo_lock(struct xbps_handle *xhp, const char *repodir,
|
|
|
|
int *lockfd, char **lockfname)
|
|
|
|
{
|
|
|
|
char *repofile, *lockfile;
|
|
|
|
int fd, rv;
|
|
|
|
|
|
|
|
assert(repodir);
|
|
|
|
assert(lockfd);
|
|
|
|
assert(lockfname);
|
|
|
|
|
|
|
|
repofile = xbps_repo_path(xhp, repodir);
|
|
|
|
assert(repofile);
|
|
|
|
|
|
|
|
lockfile = xbps_xasprintf("%s.lock", repofile);
|
|
|
|
free(repofile);
|
|
|
|
|
|
|
|
for (;;) {
|
2015-04-16 00:32:38 +05:30
|
|
|
fd = open(lockfile, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0660);
|
2015-03-25 16:30:59 +05:30
|
|
|
rv = errno;
|
|
|
|
if (fd != -1)
|
|
|
|
break;
|
|
|
|
if (rv != EEXIST) {
|
|
|
|
xbps_dbg_printf(xhp, "[repo] `%s' failed to "
|
|
|
|
"create lock file %s\n", lockfile, strerror(rv));
|
|
|
|
free(lockfile);
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
xbps_dbg_printf(xhp, "[repo] `%s' lock file exists,"
|
2015-06-04 11:38:40 +05:30
|
|
|
"waiting for 1s...\n", lockfile);
|
|
|
|
sleep(1);
|
2015-03-25 16:30:59 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
*lockfname = lockfile;
|
|
|
|
*lockfd = fd;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
xbps_repo_unlock(int lockfd, char *lockfname)
|
|
|
|
{
|
|
|
|
if (lockfd != -1) {
|
|
|
|
close(lockfd);
|
|
|
|
}
|
|
|
|
if (lockfname) {
|
|
|
|
unlink(lockfname);
|
|
|
|
free(lockfname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-26 20:37:16 +05:30
|
|
|
static bool
|
2015-03-25 16:30:59 +05:30
|
|
|
repo_open_local(struct xbps_repo *repo, const char *repofile)
|
2014-11-26 20:37:16 +05:30
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
int rv = 0;
|
|
|
|
|
|
|
|
if (fstat(repo->fd, &st) == -1) {
|
|
|
|
rv = errno;
|
|
|
|
xbps_dbg_printf(repo->xhp, "[repo] `%s' fstat repodata %s\n",
|
2014-11-29 13:14:21 +05:30
|
|
|
repofile, strerror(rv));
|
2014-11-26 20:37:16 +05:30
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
repo->ar = archive_read_new();
|
|
|
|
archive_read_support_compression_gzip(repo->ar);
|
|
|
|
archive_read_support_format_tar(repo->ar);
|
|
|
|
|
|
|
|
if (archive_read_open_fd(repo->ar, repo->fd, st.st_blksize) == ARCHIVE_FATAL) {
|
|
|
|
rv = archive_errno(repo->ar);
|
|
|
|
xbps_dbg_printf(repo->xhp,
|
|
|
|
"[repo] `%s' failed to open repodata archive %s\n",
|
2014-11-29 13:14:21 +05:30
|
|
|
repofile, strerror(rv));
|
2014-11-26 20:37:16 +05:30
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ((repo->idx = repo_get_dict(repo)) == NULL) {
|
|
|
|
rv = archive_errno(repo->ar);
|
2014-11-29 13:14:21 +05:30
|
|
|
xbps_dbg_printf(repo->xhp, "[repo] `%s' failed to internalize "
|
|
|
|
" index on archive, removing file.\n", repofile);
|
|
|
|
/* broken archive, remove it */
|
|
|
|
(void)unlink(repofile);
|
2014-11-26 20:37:16 +05:30
|
|
|
return false;
|
|
|
|
}
|
2015-05-22 15:49:48 +05:30
|
|
|
xbps_dictionary_make_immutable(repo->idx);
|
2014-11-26 20:37:16 +05:30
|
|
|
repo->idxmeta = repo_get_dict(repo);
|
2015-05-22 15:49:48 +05:30
|
|
|
if (repo->idxmeta != NULL) {
|
2014-11-26 20:37:16 +05:30
|
|
|
repo->is_signed = true;
|
2015-05-22 15:49:48 +05:30
|
|
|
xbps_dictionary_make_immutable(repo->idxmeta);
|
|
|
|
}
|
2014-11-26 20:37:16 +05:30
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
repo_open_remote(struct xbps_repo *repo)
|
|
|
|
{
|
|
|
|
char *rpath;
|
|
|
|
bool rv;
|
|
|
|
|
|
|
|
rpath = xbps_repo_path(repo->xhp, repo->uri);
|
|
|
|
rv = xbps_repo_fetch_remote(repo, rpath);
|
|
|
|
free(rpath);
|
|
|
|
if (rv) {
|
|
|
|
xbps_dbg_printf(repo->xhp, "[repo] `%s' used remotely (kept in memory).\n", repo->uri);
|
|
|
|
if (repo->xhp->state_cb && xbps_repo_key_import(repo) != 0)
|
|
|
|
rv = false;
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-01-10 16:29:31 +05:30
|
|
|
bool
|
|
|
|
xbps_repo_store(struct xbps_handle *xhp, const char *repo)
|
|
|
|
{
|
|
|
|
char *url = NULL;
|
|
|
|
|
|
|
|
assert(xhp);
|
|
|
|
assert(repo);
|
|
|
|
|
2015-01-10 16:30:36 +05:30
|
|
|
if (xhp->repositories == NULL) {
|
2015-01-10 16:29:31 +05:30
|
|
|
xhp->repositories = xbps_array_create();
|
|
|
|
assert(xhp->repositories);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* If it's a local repo and path is relative, make it absolute.
|
|
|
|
*/
|
|
|
|
if (!xbps_repository_is_remote(repo)) {
|
|
|
|
if (repo[0] != '/' && repo[0] != '\0') {
|
|
|
|
if ((url = realpath(repo, NULL)) == NULL)
|
|
|
|
xbps_dbg_printf(xhp, "[repo] %s: realpath %s\n", __func__, repo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (xbps_match_string_in_array(xhp->repositories, url ? url : repo)) {
|
|
|
|
xbps_dbg_printf(xhp, "[repo] `%s' already stored\n", url ? url : repo);
|
|
|
|
if (url)
|
|
|
|
free(url);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (xbps_array_add_cstring(xhp->repositories, url ? url : repo)) {
|
|
|
|
xbps_dbg_printf(xhp, "[repo] `%s' stored successfully\n", url ? url : repo);
|
|
|
|
if (url)
|
|
|
|
free(url);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (url)
|
|
|
|
free(url);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-10 13:58:39 +05:30
|
|
|
struct xbps_repo *
|
2015-03-25 16:30:59 +05:30
|
|
|
xbps_repo_open(struct xbps_handle *xhp, const char *url)
|
2013-06-10 13:58:39 +05:30
|
|
|
{
|
2013-12-24 15:13:55 +05:30
|
|
|
struct xbps_repo *repo;
|
2013-06-10 13:58:39 +05:30
|
|
|
const char *arch;
|
|
|
|
char *repofile;
|
|
|
|
|
|
|
|
assert(xhp);
|
|
|
|
assert(url);
|
|
|
|
|
|
|
|
if (xhp->target_arch)
|
|
|
|
arch = xhp->target_arch;
|
|
|
|
else
|
|
|
|
arch = xhp->native_arch;
|
|
|
|
|
2013-12-24 15:13:55 +05:30
|
|
|
repo = calloc(1, sizeof(struct xbps_repo));
|
|
|
|
assert(repo);
|
2015-03-25 16:30:59 +05:30
|
|
|
repo->fd = -1;
|
2013-12-24 15:13:55 +05:30
|
|
|
repo->xhp = xhp;
|
|
|
|
repo->uri = url;
|
|
|
|
|
2013-06-10 13:58:39 +05:30
|
|
|
if (xbps_repository_is_remote(url)) {
|
|
|
|
/* remote repository */
|
|
|
|
char *rpath;
|
|
|
|
|
2014-04-19 19:37:29 +05:30
|
|
|
if ((rpath = xbps_get_remote_repo_string(url)) == NULL) {
|
|
|
|
free(repo);
|
2013-06-10 13:58:39 +05:30
|
|
|
return NULL;
|
2014-04-19 19:37:29 +05:30
|
|
|
}
|
2013-06-10 13:58:39 +05:30
|
|
|
repofile = xbps_xasprintf("%s/%s/%s-repodata", xhp->metadir, rpath, arch);
|
|
|
|
free(rpath);
|
2013-12-24 15:13:55 +05:30
|
|
|
repo->is_remote = true;
|
2013-06-10 13:58:39 +05:30
|
|
|
} else {
|
|
|
|
/* local repository */
|
|
|
|
repofile = xbps_repo_path(xhp, url);
|
|
|
|
}
|
2014-09-05 15:56:42 +05:30
|
|
|
/*
|
2014-11-27 15:12:05 +05:30
|
|
|
* In memory repo sync.
|
|
|
|
*/
|
|
|
|
if (xhp->flags & XBPS_FLAG_REPOS_MEMSYNC) {
|
|
|
|
if (repo_open_remote(repo))
|
|
|
|
return repo;
|
|
|
|
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Open the repository archive.
|
2014-09-05 15:56:42 +05:30
|
|
|
*/
|
2015-04-16 00:32:38 +05:30
|
|
|
repo->fd = open(repofile, O_RDONLY|O_CLOEXEC);
|
2014-09-05 15:56:42 +05:30
|
|
|
if (repo->fd == -1) {
|
2014-11-26 20:37:16 +05:30
|
|
|
int rv = errno;
|
|
|
|
xbps_dbg_printf(xhp, "[repo] `%s' open repodata %s\n",
|
|
|
|
repofile, strerror(rv));
|
2013-10-07 13:49:04 +05:30
|
|
|
goto out;
|
2013-06-10 13:58:39 +05:30
|
|
|
}
|
2015-03-25 16:30:59 +05:30
|
|
|
if (repo_open_local(repo, repofile)) {
|
2014-11-29 13:14:21 +05:30
|
|
|
free(repofile);
|
2014-11-26 20:37:16 +05:30
|
|
|
return repo;
|
2014-11-29 13:14:21 +05:30
|
|
|
}
|
2014-09-05 15:56:42 +05:30
|
|
|
|
|
|
|
out:
|
|
|
|
free(repofile);
|
2015-03-25 16:30:59 +05:30
|
|
|
xbps_repo_close(repo);
|
2014-09-05 15:56:42 +05:30
|
|
|
return NULL;
|
2013-06-10 13:58:39 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-01-11 13:41:38 +05:30
|
|
|
xbps_repo_close(struct xbps_repo *repo)
|
2013-06-10 13:58:39 +05:30
|
|
|
{
|
|
|
|
assert(repo);
|
|
|
|
|
2013-10-05 15:08:04 +05:30
|
|
|
if (repo->ar != NULL)
|
|
|
|
archive_read_finish(repo->ar);
|
2013-07-26 15:12:52 +05:30
|
|
|
|
2013-10-05 15:08:04 +05:30
|
|
|
if (repo->idx != NULL) {
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_object_release(repo->idx);
|
2013-10-05 15:08:04 +05:30
|
|
|
repo->idx = NULL;
|
|
|
|
}
|
2014-01-30 17:37:34 +05:30
|
|
|
if (repo->idxmeta != NULL) {
|
|
|
|
xbps_object_release(repo->idxmeta);
|
|
|
|
repo->idxmeta = NULL;
|
|
|
|
}
|
2015-03-25 16:30:59 +05:30
|
|
|
if (repo->fd != -1)
|
|
|
|
close(repo->fd);
|
2014-09-05 15:56:42 +05:30
|
|
|
|
2015-03-25 16:30:59 +05:30
|
|
|
free(repo);
|
2013-06-10 13:58:39 +05:30
|
|
|
}
|
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_t
|
2013-06-10 13:58:39 +05:30
|
|
|
xbps_repo_get_virtualpkg(struct xbps_repo *repo, const char *pkg)
|
|
|
|
{
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_t pkgd;
|
2013-06-10 13:58:39 +05:30
|
|
|
|
|
|
|
assert(repo);
|
|
|
|
assert(pkg);
|
|
|
|
|
2013-12-24 15:13:55 +05:30
|
|
|
if (repo->idx == NULL)
|
2013-07-26 15:12:52 +05:30
|
|
|
return NULL;
|
|
|
|
|
2013-06-10 13:58:39 +05:30
|
|
|
pkgd = xbps_find_virtualpkg_in_dict(repo->xhp, repo->idx, pkg);
|
|
|
|
if (pkgd) {
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_set_cstring_nocopy(pkgd,
|
2013-06-10 13:58:39 +05:30
|
|
|
"repository", repo->uri);
|
|
|
|
return pkgd;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_t
|
2013-06-10 13:58:39 +05:30
|
|
|
xbps_repo_get_pkg(struct xbps_repo *repo, const char *pkg)
|
|
|
|
{
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_t pkgd;
|
2013-06-10 13:58:39 +05:30
|
|
|
|
|
|
|
assert(repo);
|
|
|
|
assert(pkg);
|
|
|
|
|
2013-12-24 15:13:55 +05:30
|
|
|
if (repo->idx == NULL)
|
2013-07-26 15:12:52 +05:30
|
|
|
return NULL;
|
|
|
|
|
2013-06-10 13:58:39 +05:30
|
|
|
pkgd = xbps_find_pkg_in_dict(repo->idx, pkg);
|
|
|
|
if (pkgd) {
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_set_cstring_nocopy(pkgd,
|
2013-06-10 13:58:39 +05:30
|
|
|
"repository", repo->uri);
|
|
|
|
return pkgd;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-06-14 11:43:51 +05:30
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_t
|
|
|
|
xbps_repo_get_pkg_plist(struct xbps_handle *xhp, xbps_dictionary_t pkgd,
|
2013-06-14 13:52:10 +05:30
|
|
|
const char *plist)
|
|
|
|
{
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_t bpkgd;
|
2013-06-14 13:52:10 +05:30
|
|
|
char *url;
|
|
|
|
|
|
|
|
url = xbps_repository_pkg_path(xhp, pkgd);
|
|
|
|
if (url == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2014-11-21 14:21:57 +05:30
|
|
|
bpkgd = xbps_archive_fetch_plist(url, plist);
|
2013-06-14 13:52:10 +05:30
|
|
|
free(url);
|
|
|
|
return bpkgd;
|
|
|
|
}
|
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
static xbps_array_t
|
|
|
|
revdeps_match(struct xbps_repo *repo, xbps_dictionary_t tpkgd, const char *str)
|
2013-06-14 11:43:51 +05:30
|
|
|
{
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_t pkgd;
|
|
|
|
xbps_array_t revdeps = NULL, pkgdeps, provides;
|
|
|
|
xbps_object_iterator_t iter;
|
|
|
|
xbps_object_t obj;
|
2013-06-14 11:43:51 +05:30
|
|
|
const char *pkgver, *tpkgver, *arch, *vpkg;
|
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
iter = xbps_dictionary_iterator(repo->idx);
|
2013-06-14 11:43:51 +05:30
|
|
|
assert(iter);
|
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
while ((obj = xbps_object_iterator_next(iter))) {
|
|
|
|
pkgd = xbps_dictionary_get_keysym(repo->idx, obj);
|
|
|
|
if (xbps_dictionary_equals(pkgd, tpkgd))
|
2013-06-14 11:43:51 +05:30
|
|
|
continue;
|
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
pkgdeps = xbps_dictionary_get(pkgd, "run_depends");
|
|
|
|
if (!xbps_array_count(pkgdeps))
|
2013-06-14 11:43:51 +05:30
|
|
|
continue;
|
|
|
|
/*
|
|
|
|
* Try to match passed in string.
|
|
|
|
*/
|
|
|
|
if (str) {
|
|
|
|
if (!xbps_match_pkgdep_in_array(pkgdeps, str))
|
|
|
|
continue;
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_get_cstring_nocopy(pkgd,
|
2013-06-14 11:43:51 +05:30
|
|
|
"architecture", &arch);
|
|
|
|
if (!xbps_pkg_arch_match(repo->xhp, arch, NULL))
|
|
|
|
continue;
|
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_get_cstring_nocopy(pkgd,
|
2013-06-14 11:43:51 +05:30
|
|
|
"pkgver", &tpkgver);
|
|
|
|
/* match */
|
|
|
|
if (revdeps == NULL)
|
2013-06-20 13:56:12 +05:30
|
|
|
revdeps = xbps_array_create();
|
2013-06-14 11:43:51 +05:30
|
|
|
|
|
|
|
if (!xbps_match_string_in_array(revdeps, tpkgver))
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_array_add_cstring_nocopy(revdeps, tpkgver);
|
2013-06-14 11:43:51 +05:30
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Try to match any virtual package.
|
|
|
|
*/
|
2013-06-20 13:56:12 +05:30
|
|
|
provides = xbps_dictionary_get(tpkgd, "provides");
|
2013-09-15 13:36:49 +05:30
|
|
|
for (unsigned int i = 0; i < xbps_array_count(provides); i++) {
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_array_get_cstring_nocopy(provides, i, &vpkg);
|
2015-01-10 11:56:23 +05:30
|
|
|
if (!xbps_match_pkgdep_in_array(pkgdeps, vpkg))
|
2013-06-14 11:43:51 +05:30
|
|
|
continue;
|
2015-01-10 11:56:23 +05:30
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_get_cstring_nocopy(pkgd,
|
2013-06-14 11:43:51 +05:30
|
|
|
"architecture", &arch);
|
|
|
|
if (!xbps_pkg_arch_match(repo->xhp, arch, NULL))
|
|
|
|
continue;
|
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_get_cstring_nocopy(pkgd, "pkgver",
|
2013-06-14 11:43:51 +05:30
|
|
|
&tpkgver);
|
|
|
|
/* match */
|
|
|
|
if (revdeps == NULL)
|
2013-06-20 13:56:12 +05:30
|
|
|
revdeps = xbps_array_create();
|
2013-06-14 11:43:51 +05:30
|
|
|
|
|
|
|
if (!xbps_match_string_in_array(revdeps, tpkgver))
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_array_add_cstring_nocopy(revdeps, tpkgver);
|
2013-06-14 11:43:51 +05:30
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Try to match by pkgver.
|
|
|
|
*/
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_get_cstring_nocopy(tpkgd, "pkgver", &pkgver);
|
2013-06-14 11:43:51 +05:30
|
|
|
if (!xbps_match_pkgdep_in_array(pkgdeps, pkgver))
|
|
|
|
continue;
|
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_get_cstring_nocopy(pkgd,
|
2013-06-14 11:43:51 +05:30
|
|
|
"architecture", &arch);
|
|
|
|
if (!xbps_pkg_arch_match(repo->xhp, arch, NULL))
|
|
|
|
continue;
|
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_get_cstring_nocopy(pkgd, "pkgver", &tpkgver);
|
2013-06-14 11:43:51 +05:30
|
|
|
/* match */
|
|
|
|
if (revdeps == NULL)
|
2013-06-20 13:56:12 +05:30
|
|
|
revdeps = xbps_array_create();
|
2013-06-14 11:43:51 +05:30
|
|
|
|
|
|
|
if (!xbps_match_string_in_array(revdeps, tpkgver))
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_array_add_cstring_nocopy(revdeps, tpkgver);
|
2013-06-14 11:43:51 +05:30
|
|
|
}
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_object_iterator_release(iter);
|
2013-06-14 11:43:51 +05:30
|
|
|
return revdeps;
|
|
|
|
}
|
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_array_t
|
2013-06-14 11:43:51 +05:30
|
|
|
xbps_repo_get_pkg_revdeps(struct xbps_repo *repo, const char *pkg)
|
|
|
|
{
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_array_t revdeps = NULL, vdeps = NULL;
|
|
|
|
xbps_dictionary_t pkgd;
|
2013-06-14 11:43:51 +05:30
|
|
|
const char *vpkg;
|
2013-06-27 21:44:38 +05:30
|
|
|
bool match = false;
|
2013-06-14 11:43:51 +05:30
|
|
|
|
2013-10-05 15:08:04 +05:30
|
|
|
if (repo->idx == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2015-01-06 12:28:45 +05:30
|
|
|
if (((pkgd = xbps_repo_get_pkg(repo, pkg)) == NULL) &&
|
|
|
|
((pkgd = xbps_repo_get_virtualpkg(repo, pkg)) == NULL)) {
|
2013-06-14 11:43:51 +05:30
|
|
|
errno = ENOENT;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* If pkg is a virtual pkg let's match it instead of the real pkgver.
|
|
|
|
*/
|
2013-06-20 13:56:12 +05:30
|
|
|
if ((vdeps = xbps_dictionary_get(pkgd, "provides"))) {
|
2013-09-15 13:36:49 +05:30
|
|
|
for (unsigned int i = 0; i < xbps_array_count(vdeps); i++) {
|
2013-06-14 11:43:51 +05:30
|
|
|
char *vpkgn;
|
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_array_get_cstring_nocopy(vdeps, i, &vpkg);
|
2015-01-10 11:56:23 +05:30
|
|
|
vpkgn = xbps_pkg_name(vpkg);
|
2013-06-14 11:43:51 +05:30
|
|
|
assert(vpkgn);
|
|
|
|
if (strcmp(vpkgn, pkg) == 0) {
|
|
|
|
free(vpkgn);
|
2015-01-10 11:56:23 +05:30
|
|
|
match = true;
|
2013-06-14 11:43:51 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
free(vpkgn);
|
2015-01-10 11:56:23 +05:30
|
|
|
vpkg = NULL;
|
2013-06-14 11:43:51 +05:30
|
|
|
}
|
2015-01-10 11:56:23 +05:30
|
|
|
if (match)
|
|
|
|
revdeps = revdeps_match(repo, pkgd, vpkg);
|
2013-06-14 11:43:51 +05:30
|
|
|
}
|
2013-06-27 21:44:38 +05:30
|
|
|
if (!match)
|
2013-06-14 11:43:51 +05:30
|
|
|
revdeps = revdeps_match(repo, pkgd, NULL);
|
|
|
|
|
|
|
|
return revdeps;
|
|
|
|
}
|
2013-12-24 15:13:55 +05:30
|
|
|
|
|
|
|
int
|
|
|
|
xbps_repo_key_import(struct xbps_repo *repo)
|
|
|
|
{
|
|
|
|
xbps_dictionary_t repokeyd = NULL;
|
2014-01-30 17:37:34 +05:30
|
|
|
xbps_data_t pubkey = NULL;
|
|
|
|
uint16_t pubkey_size = 0;
|
2014-10-05 11:08:14 +05:30
|
|
|
const char *signedby = NULL;
|
|
|
|
char *hexfp = NULL;
|
2013-12-24 15:13:55 +05:30
|
|
|
char *p, *dbkeyd, *rkeyfile = NULL;
|
|
|
|
int import, rv = 0;
|
|
|
|
|
|
|
|
assert(repo);
|
|
|
|
/*
|
|
|
|
* If repository does not have required metadata plist, ignore it.
|
|
|
|
*/
|
2014-01-30 17:37:34 +05:30
|
|
|
if (!xbps_dictionary_count(repo->idxmeta)) {
|
2013-12-24 15:13:55 +05:30
|
|
|
xbps_dbg_printf(repo->xhp,
|
|
|
|
"[repo] `%s' unsigned repository!\n", repo->uri);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*
|
2014-01-30 17:37:34 +05:30
|
|
|
* Check for required objects in index-meta:
|
|
|
|
* - signature-by (string)
|
|
|
|
* - public-key (data)
|
|
|
|
* - public-key-size (number)
|
2013-12-24 15:13:55 +05:30
|
|
|
*/
|
2014-01-30 17:37:34 +05:30
|
|
|
xbps_dictionary_get_cstring_nocopy(repo->idxmeta, "signature-by", &signedby);
|
|
|
|
xbps_dictionary_get_uint16(repo->idxmeta, "public-key-size", &pubkey_size);
|
|
|
|
pubkey = xbps_dictionary_get(repo->idxmeta, "public-key");
|
|
|
|
|
|
|
|
if (signedby == NULL || pubkey_size == 0 ||
|
|
|
|
xbps_object_type(pubkey) != XBPS_TYPE_DATA) {
|
2013-12-24 15:13:55 +05:30
|
|
|
xbps_dbg_printf(repo->xhp,
|
2014-01-30 17:37:34 +05:30
|
|
|
"[repo] `%s': incomplete signed repository "
|
|
|
|
"(missing objs)\n", repo->uri);
|
2013-12-24 15:13:55 +05:30
|
|
|
rv = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
2014-01-30 17:37:34 +05:30
|
|
|
hexfp = xbps_pubkey2fp(repo->xhp, pubkey);
|
2013-12-24 15:13:55 +05:30
|
|
|
/*
|
|
|
|
* Check if the public key is alredy stored.
|
|
|
|
*/
|
2014-01-30 17:37:34 +05:30
|
|
|
rkeyfile = xbps_xasprintf("%s/keys/%s.plist", repo->xhp->metadir, hexfp);
|
2015-05-28 13:45:05 +05:30
|
|
|
repokeyd = xbps_plist_dictionary_from_file(repo->xhp, rkeyfile);
|
2013-12-24 15:13:55 +05:30
|
|
|
if (xbps_object_type(repokeyd) == XBPS_TYPE_DICTIONARY) {
|
|
|
|
xbps_dbg_printf(repo->xhp,
|
|
|
|
"[repo] `%s' public key already stored.\n", repo->uri);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Notify the client and take appropiate action to import
|
|
|
|
* the repository public key. Pass back the public key openssh fingerprint
|
|
|
|
* to the client.
|
|
|
|
*/
|
|
|
|
import = xbps_set_cb_state(repo->xhp, XBPS_STATE_REPO_KEY_IMPORT, 0,
|
2014-01-30 17:37:34 +05:30
|
|
|
hexfp, "`%s' repository has been RSA signed by \"%s\"",
|
|
|
|
repo->uri, signedby);
|
2013-12-24 15:13:55 +05:30
|
|
|
if (import <= 0) {
|
|
|
|
rv = EAGAIN;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = strdup(rkeyfile);
|
|
|
|
dbkeyd = dirname(p);
|
|
|
|
assert(dbkeyd);
|
|
|
|
if (access(dbkeyd, R_OK|W_OK) == -1) {
|
2014-10-05 15:52:18 +05:30
|
|
|
rv = errno;
|
|
|
|
if (rv == ENOENT)
|
|
|
|
rv = xbps_mkpath(dbkeyd, 0755);
|
|
|
|
if (rv != 0) {
|
2014-10-05 16:41:59 +05:30
|
|
|
rv = errno;
|
2013-12-24 15:13:55 +05:30
|
|
|
xbps_dbg_printf(repo->xhp,
|
|
|
|
"[repo] `%s' cannot create %s: %s\n",
|
|
|
|
repo->uri, dbkeyd, strerror(errno));
|
|
|
|
free(p);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(p);
|
|
|
|
|
|
|
|
repokeyd = xbps_dictionary_create();
|
2014-01-30 17:37:34 +05:30
|
|
|
xbps_dictionary_set(repokeyd, "public-key", pubkey);
|
|
|
|
xbps_dictionary_set_uint16(repokeyd, "public-key-size", pubkey_size);
|
|
|
|
xbps_dictionary_set_cstring_nocopy(repokeyd, "signature-by", signedby);
|
2013-12-24 15:13:55 +05:30
|
|
|
|
2014-10-24 13:45:41 +05:30
|
|
|
if (!xbps_dictionary_externalize_to_file(repokeyd, rkeyfile)) {
|
2013-12-24 15:13:55 +05:30
|
|
|
rv = errno;
|
|
|
|
xbps_dbg_printf(repo->xhp,
|
|
|
|
"[repo] `%s' failed to externalize %s: %s\n",
|
|
|
|
repo->uri, rkeyfile, strerror(rv));
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
2014-10-05 11:02:43 +05:30
|
|
|
if (hexfp)
|
|
|
|
free(hexfp);
|
2013-12-24 15:13:55 +05:30
|
|
|
if (repokeyd)
|
|
|
|
xbps_object_release(repokeyd);
|
|
|
|
if (rkeyfile)
|
|
|
|
free(rkeyfile);
|
|
|
|
return rv;
|
|
|
|
}
|