33#include "xbps_api_impl.h"
37verify_binpkg(
struct xbps_handle *xhp, xbps_dictionary_t pkgd)
39 char binfile[PATH_MAX];
41 const char *pkgver, *repoloc, *sha256;
45 xbps_dictionary_get_cstring_nocopy(pkgd,
"repository", &repoloc);
46 xbps_dictionary_get_cstring_nocopy(pkgd,
"pkgver", &pkgver);
58 xbps_dbg_printf(
"%s: failed to get repository "
59 "%s: %s\n", pkgver, repoloc, strerror(errno));
64 xbps_set_cb_state(
xhp, XBPS_STATE_VERIFY, 0, pkgver,
65 "%s: verifying RSA signature...", pkgver);
69 xbps_set_cb_state(
xhp, XBPS_STATE_VERIFY_FAIL, rv, pkgver,
70 "%s: the RSA signature is not valid!", pkgver);
71 xbps_set_cb_state(
xhp, XBPS_STATE_VERIFY_FAIL, rv, pkgver,
72 "%s: removed pkg archive and its signature.", pkgver);
73 (void)remove(binfile);
74 if (
xbps_strlcat(binfile,
".sig2",
sizeof(binfile)) <
sizeof(binfile))
75 (void)remove(binfile);
80 xbps_set_cb_state(
xhp, XBPS_STATE_VERIFY, 0, pkgver,
81 "%s: verifying SHA256 hash...", pkgver);
82 xbps_dictionary_get_cstring_nocopy(pkgd,
"filename-sha256", &sha256);
84 xbps_set_cb_state(
xhp, XBPS_STATE_VERIFY_FAIL, rv, pkgver,
85 "%s: SHA256 hash is not valid: %s", pkgver, strerror(rv));
95download_binpkg(
struct xbps_handle *
xhp, xbps_dictionary_t repo_pkgd)
100 const char *pkgver, *arch, *fetchstr, *repoloc;
101 unsigned char digest[XBPS_SHA256_DIGEST_SIZE] = {0};
104 xbps_dictionary_get_cstring_nocopy(repo_pkgd,
"repository", &repoloc);
108 xbps_dictionary_get_cstring_nocopy(repo_pkgd,
"pkgver", &pkgver);
109 xbps_dictionary_get_cstring_nocopy(repo_pkgd,
"architecture", &arch);
111 snprintf(buf,
sizeof buf,
"%s/%s.%s.xbps.sig2", repoloc, pkgver, arch);
112 sigsuffix = buf+(strlen(buf)-
sizeof (
".sig2")+1);
114 xbps_set_cb_state(
xhp, XBPS_STATE_DOWNLOAD, 0, pkgver,
115 "Downloading `%s' signature (from `%s')...", pkgver, repoloc);
118 rv = fetchLastErrCode ? fetchLastErrCode : errno;
120 xbps_set_cb_state(
xhp, XBPS_STATE_DOWNLOAD_FAIL, rv,
121 pkgver,
"[trans] failed to download `%s' signature from `%s': %s",
122 pkgver, repoloc, fetchstr ? fetchstr : strerror(rv));
129 xbps_set_cb_state(
xhp, XBPS_STATE_DOWNLOAD, 0, pkgver,
130 "Downloading `%s' package (from `%s')...", pkgver, repoloc);
133 sizeof digest)) == -1) {
134 rv = fetchLastErrCode ? fetchLastErrCode : errno;
136 xbps_set_cb_state(
xhp, XBPS_STATE_DOWNLOAD_FAIL, rv,
137 pkgver,
"[trans] failed to download `%s' package from `%s': %s",
138 pkgver, repoloc, fetchstr ? fetchstr : strerror(rv));
143 xbps_set_cb_state(
xhp, XBPS_STATE_VERIFY, 0, pkgver,
144 "%s: verifying RSA signature...", pkgver);
146 snprintf(buf,
sizeof buf,
"%s/%s.%s.xbps.sig2",
xhp->
cachedir, pkgver, arch);
147 sigsuffix = buf+(strlen(buf)-
sizeof (
".sig2")+1);
151 xbps_dbg_printf(
"%s: failed to get repository "
152 "%s: %s\n", pkgver, repoloc, strerror(errno));
160 if (fetchLastErrCode == FETCH_UNCHANGED) {
182 xbps_set_cb_state(
xhp, XBPS_STATE_VERIFY_FAIL, rv, pkgver,
183 "%s: the RSA signature is not valid!", pkgver);
184 xbps_set_cb_state(
xhp, XBPS_STATE_VERIFY_FAIL, rv, pkgver,
185 "%s: removed pkg archive and its signature.", pkgver);
192xbps_transaction_fetch(
struct xbps_handle *
xhp, xbps_object_iterator_t iter)
194 xbps_array_t fetch = NULL, verify = NULL;
201 xbps_object_iterator_reset(iter);
203 while ((obj = xbps_object_iterator_next(iter)) != NULL) {
205 if (ttype == XBPS_TRANS_REMOVE || ttype == XBPS_TRANS_HOLD ||
206 ttype == XBPS_TRANS_CONFIGURE) {
209 xbps_dictionary_get_cstring_nocopy(obj,
"repository", &repoloc);
217 if (!fetch && !(fetch = xbps_array_create())) {
221 xbps_array_add(fetch, obj);
228 if (!verify && !(verify = xbps_array_create())) {
232 xbps_array_add(verify, obj);
234 xbps_object_iterator_reset(iter);
240 n = xbps_array_count(fetch);
242 xbps_set_cb_state(
xhp, XBPS_STATE_TRANS_DOWNLOAD, 0, NULL, NULL);
243 xbps_dbg_printf(
"[trans] downloading %d packages.\n", n);
245 for (i = 0; i < n; i++) {
246 if ((rv = download_binpkg(
xhp, xbps_array_get(fetch, i))) != 0) {
247 xbps_dbg_printf(
"[trans] failed to download binpkgs: "
248 "%s\n", strerror(rv));
256 n = xbps_array_count(verify);
258 xbps_set_cb_state(
xhp, XBPS_STATE_TRANS_VERIFY, 0, NULL, NULL);
259 xbps_dbg_printf(
"[trans] verifying %d packages.\n", n);
261 for (i = 0; i < n; i++) {
262 if ((rv = verify_binpkg(
xhp, xbps_array_get(verify, i))) != 0) {
263 xbps_dbg_printf(
"[trans] failed to check binpkgs: "
264 "%s\n", strerror(rv));
271 xbps_object_release(fetch);
273 xbps_object_release(verify);
int xbps_fetch_file_sha256(struct xbps_handle *xhp, const char *uri, const char *flags, unsigned char *digest, size_t digestlen)
const char * xbps_fetch_error_string(void)
int xbps_fetch_file(struct xbps_handle *xhp, const char *uri, const char *flags)
char cachedir[XBPS_MAXPATH+sizeof(XBPS_CACHE_PATH)]
Generic XBPS structure handler for initialization.
struct xbps_repo * xbps_rpool_get_repo(const char *url)
xbps_trans_type_t xbps_transaction_pkg_type(xbps_dictionary_t pkg_repod)
bool xbps_verify_file_signature(struct xbps_repo *repo, const char *fname)
int xbps_file_sha256_check(const char *file, const char *sha256)
bool xbps_verify_signature(struct xbps_repo *repo, const char *sigfile, unsigned char *digest)
bool xbps_remote_binpkg_exists(struct xbps_handle *xhp, xbps_dictionary_t pkgd)
ssize_t xbps_pkg_path(struct xbps_handle *xhp, char *dst, size_t dstsz, xbps_dictionary_t pkgd)
bool xbps_repository_is_remote(const char *uri)
size_t xbps_strlcat(char *dst, const char *src, size_t dstsize)