2009-11-26 02:22:50 +00:00
|
|
|
/*-
|
2013-03-05 04:08:42 +01:00
|
|
|
* Copyright (c) 2009-2013 Juan Romero Pardines.
|
2009-11-26 02:22:50 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2011-11-07 20:28:35 +01:00
|
|
|
#include <sys/utsname.h>
|
2009-11-26 02:22:50 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
2012-01-15 17:23:50 +01:00
|
|
|
#include <libgen.h>
|
2009-11-26 02:22:50 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2010-11-13 03:18:58 +01:00
|
|
|
#include "xbps_api_impl.h"
|
2009-11-26 02:22:50 +00:00
|
|
|
|
2013-06-10 10:28:39 +02:00
|
|
|
struct rpool {
|
|
|
|
SIMPLEQ_ENTRY(rpool) entries;
|
|
|
|
struct xbps_repo *repo;
|
|
|
|
};
|
|
|
|
|
|
|
|
static SIMPLEQ_HEAD(rpool_head, rpool) rpool_queue =
|
|
|
|
SIMPLEQ_HEAD_INITIALIZER(rpool_queue);
|
|
|
|
|
2010-01-21 03:10:19 +01:00
|
|
|
/**
|
2012-12-19 01:01:27 +01:00
|
|
|
* @file lib/rpool.c
|
2011-01-18 14:44:39 +01:00
|
|
|
* @brief Repository pool routines
|
|
|
|
* @defgroup repopool Repository pool functions
|
2010-01-21 03:10:19 +01:00
|
|
|
*/
|
|
|
|
|
2011-06-04 13:37:53 +02:00
|
|
|
int HIDDEN
|
2012-05-30 10:56:32 +02:00
|
|
|
xbps_rpool_init(struct xbps_handle *xhp)
|
2009-11-26 02:22:50 +00:00
|
|
|
{
|
2013-06-10 10:28:39 +02:00
|
|
|
struct rpool *rp;
|
2011-06-04 13:37:53 +02:00
|
|
|
const char *repouri;
|
2013-10-07 10:19:04 +02:00
|
|
|
char *p;
|
2013-06-10 10:28:39 +02:00
|
|
|
bool foundrepo = false;
|
2013-10-05 11:38:04 +02:00
|
|
|
int retval, rv = 0;
|
2009-11-26 02:22:50 +00:00
|
|
|
|
2013-06-10 10:28:39 +02:00
|
|
|
assert(xhp);
|
|
|
|
|
|
|
|
if (xhp->rpool_initialized)
|
2009-11-26 02:22:50 +00:00
|
|
|
return 0;
|
|
|
|
|
2013-10-07 10:19:04 +02:00
|
|
|
p = xbps_xasprintf("%s/%s", xhp->metadir, XBPS_REPOKEYS);
|
|
|
|
xhp->repokeys = xbps_dictionary_internalize_from_file(p);
|
|
|
|
free(p);
|
|
|
|
|
2013-09-18 16:45:05 +02:00
|
|
|
for (unsigned int i = 0; i < xbps_array_count(xhp->repositories); i++) {
|
2013-06-10 10:28:39 +02:00
|
|
|
rp = malloc(sizeof(struct rpool));
|
|
|
|
assert(rp);
|
2013-09-18 16:45:05 +02:00
|
|
|
xbps_array_get_cstring_nocopy(xhp->repositories, i, &repouri);
|
2013-06-10 10:28:39 +02:00
|
|
|
if ((rp->repo = xbps_repo_open(xhp, repouri)) == NULL) {
|
2013-10-07 10:19:04 +02:00
|
|
|
rp->repo = calloc(1, sizeof(struct xbps_repo));
|
2013-07-26 11:42:52 +02:00
|
|
|
assert(rp->repo);
|
2013-10-07 10:19:04 +02:00
|
|
|
rp->repo->xhp = xhp;
|
|
|
|
rp->repo->uri = repouri;
|
|
|
|
if (xbps_repository_is_remote(repouri))
|
|
|
|
rp->repo->is_remote = true;
|
2011-11-11 00:07:26 +01:00
|
|
|
}
|
2013-10-07 10:19:04 +02:00
|
|
|
if (rp->repo->is_remote) {
|
|
|
|
if (!rp->repo->is_signed) {
|
|
|
|
/* ignore unsigned repositories */
|
2013-10-09 10:13:07 +02:00
|
|
|
xbps_repo_invalidate(rp->repo);
|
2013-10-05 11:38:04 +02:00
|
|
|
} else {
|
2013-10-07 10:19:04 +02:00
|
|
|
/*
|
|
|
|
* Check the repository index signature against
|
|
|
|
* stored public key.
|
|
|
|
*/
|
|
|
|
retval = xbps_repo_key_verify(rp->repo);
|
|
|
|
if (retval == 0) {
|
|
|
|
/* signed, verified */
|
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_REPO_SIGVERIFIED, 0, NULL, NULL);
|
|
|
|
} else if (retval == EPERM) {
|
|
|
|
/* signed, unverified */
|
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_REPO_SIGUNVERIFIED, 0, NULL, NULL);
|
2013-10-09 10:13:07 +02:00
|
|
|
xbps_repo_invalidate(rp->repo);
|
2013-10-07 10:19:04 +02:00
|
|
|
} else {
|
|
|
|
/* any error */
|
|
|
|
xbps_dbg_printf(xhp, "[rpool] %s: key_verify %s\n",
|
|
|
|
repouri, strerror(retval));
|
2013-10-09 10:13:07 +02:00
|
|
|
xbps_repo_invalidate(rp->repo);
|
2013-10-07 10:19:04 +02:00
|
|
|
}
|
2013-10-05 11:38:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* If repository has passed signature checks, add it to the pool.
|
|
|
|
*/
|
2013-06-10 10:28:39 +02:00
|
|
|
SIMPLEQ_INSERT_TAIL(&rpool_queue, rp, entries);
|
|
|
|
foundrepo = true;
|
2013-10-05 11:38:04 +02:00
|
|
|
xbps_dbg_printf(xhp, "[rpool] `%s' registered (%s, %s).\n",
|
|
|
|
repouri, rp->repo->is_signed ? "signed" : "unsigned",
|
|
|
|
rp->repo->is_verified ? "verified" : "unverified");
|
2009-11-26 02:22:50 +00:00
|
|
|
}
|
2013-06-10 10:28:39 +02:00
|
|
|
if (!foundrepo) {
|
2011-07-29 11:17:34 +02:00
|
|
|
/* no repositories available, error out */
|
|
|
|
rv = ENOTSUP;
|
2009-11-26 21:41:46 +01:00
|
|
|
goto out;
|
2011-07-29 11:17:34 +02:00
|
|
|
}
|
2013-06-10 10:28:39 +02:00
|
|
|
xhp->rpool_initialized = true;
|
2012-06-14 08:22:11 +02:00
|
|
|
xbps_dbg_printf(xhp, "[rpool] initialized ok.\n");
|
2009-11-26 02:22:50 +00:00
|
|
|
out:
|
2012-11-30 07:11:51 +01:00
|
|
|
if (rv != 0)
|
2012-05-30 10:56:32 +02:00
|
|
|
xbps_rpool_release(xhp);
|
2009-11-26 02:22:50 +00:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-06-04 13:37:53 +02:00
|
|
|
void HIDDEN
|
2012-05-30 10:56:32 +02:00
|
|
|
xbps_rpool_release(struct xbps_handle *xhp)
|
2009-11-26 02:22:50 +00:00
|
|
|
{
|
2013-06-10 10:28:39 +02:00
|
|
|
struct rpool *rp;
|
2009-11-26 02:22:50 +00:00
|
|
|
|
2013-06-10 10:28:39 +02:00
|
|
|
if (!xhp->rpool_initialized)
|
2009-11-26 02:22:50 +00:00
|
|
|
return;
|
|
|
|
|
2013-06-10 10:28:39 +02:00
|
|
|
while ((rp = SIMPLEQ_FIRST(&rpool_queue))) {
|
|
|
|
SIMPLEQ_REMOVE(&rpool_queue, rp, rpool, entries);
|
|
|
|
xbps_repo_close(rp->repo);
|
2013-10-05 11:38:04 +02:00
|
|
|
free(rp->repo);
|
2013-06-10 10:28:39 +02:00
|
|
|
free(rp);
|
2009-11-26 02:22:50 +00:00
|
|
|
}
|
2013-06-10 10:28:39 +02:00
|
|
|
xhp->rpool_initialized = false;
|
2012-06-14 08:22:11 +02:00
|
|
|
xbps_dbg_printf(xhp, "[rpool] released ok.\n");
|
2010-11-19 13:40:13 +01:00
|
|
|
}
|
|
|
|
|
2011-11-10 23:14:50 +01:00
|
|
|
int
|
2013-06-10 10:28:39 +02:00
|
|
|
xbps_rpool_sync(struct xbps_handle *xhp, const char *uri)
|
2011-11-10 23:14:50 +01:00
|
|
|
{
|
|
|
|
const char *repouri;
|
|
|
|
|
2013-09-18 16:45:05 +02:00
|
|
|
for (unsigned int i = 0; i < xbps_array_count(xhp->repositories); i++) {
|
|
|
|
xbps_array_get_cstring_nocopy(xhp->repositories, i, &repouri);
|
2012-06-01 15:02:06 +02:00
|
|
|
/* If argument was set just process that repository */
|
|
|
|
if (uri && strcmp(repouri, uri))
|
2012-01-15 14:24:44 +01:00
|
|
|
continue;
|
2012-11-07 10:37:58 +01:00
|
|
|
|
2013-06-10 10:28:39 +02:00
|
|
|
if (xbps_repo_sync(xhp, repouri) == -1) {
|
2012-06-14 08:22:11 +02:00
|
|
|
xbps_dbg_printf(xhp,
|
2013-06-10 10:28:39 +02:00
|
|
|
"[rpool] `%s' failed to fetch repository data: %s\n",
|
|
|
|
repouri, fetchLastErrCode == 0 ? strerror(errno) :
|
2012-06-01 15:02:06 +02:00
|
|
|
xbps_fetch_error_string());
|
2011-11-10 23:14:50 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2012-06-01 16:04:47 +02:00
|
|
|
return 0;
|
2011-11-10 23:14:50 +01:00
|
|
|
}
|
|
|
|
|
2010-11-19 13:40:13 +01:00
|
|
|
int
|
2012-06-14 08:22:11 +02:00
|
|
|
xbps_rpool_foreach(struct xbps_handle *xhp,
|
2013-10-05 11:38:04 +02:00
|
|
|
int (*fn)(struct xbps_repo *, void *, bool *),
|
|
|
|
void *arg)
|
2010-11-19 13:40:13 +01:00
|
|
|
{
|
2013-06-10 10:28:39 +02:00
|
|
|
struct rpool *rp;
|
2010-11-19 13:40:13 +01:00
|
|
|
int rv = 0;
|
|
|
|
bool done = false;
|
|
|
|
|
2011-01-25 04:14:33 +01:00
|
|
|
assert(fn != NULL);
|
2011-12-22 16:38:05 +01:00
|
|
|
/* Initialize repository pool */
|
2012-05-30 10:56:32 +02:00
|
|
|
if ((rv = xbps_rpool_init(xhp)) != 0) {
|
2011-06-22 11:55:02 +02:00
|
|
|
if (rv == ENOTSUP) {
|
2013-10-05 11:38:04 +02:00
|
|
|
xbps_dbg_printf(xhp, "[rpool] empty repository list.\n");
|
2011-06-22 11:55:02 +02:00
|
|
|
} else if (rv != ENOENT && rv != ENOTSUP) {
|
2013-10-05 11:38:04 +02:00
|
|
|
xbps_dbg_printf(xhp, "[rpool] couldn't initialize: %s\n", strerror(rv));
|
2011-06-22 11:55:02 +02:00
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
2011-12-22 16:38:05 +01:00
|
|
|
/* Iterate over repository pool */
|
2013-06-10 10:28:39 +02:00
|
|
|
SIMPLEQ_FOREACH(rp, &rpool_queue, entries) {
|
|
|
|
rv = (*fn)(rp->repo, arg, &done);
|
2012-02-01 02:30:23 +01:00
|
|
|
if (rv != 0 || done)
|
2010-11-19 13:40:13 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
2009-11-26 02:22:50 +00:00
|
|
|
}
|