2011-07-20 00:51:28 +05:30
|
|
|
/*-
|
2015-04-16 00:32:38 +05:30
|
|
|
* Copyright (c) 2008-2015 Juan Romero Pardines.
|
2011-07-20 00:51:28 +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.
|
|
|
|
*/
|
2014-09-27 16:30:34 +05:30
|
|
|
#include <sys/mman.h>
|
2011-07-20 00:51:28 +05:30
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <limits.h>
|
|
|
|
|
|
|
|
#include <openssl/sha.h>
|
|
|
|
|
|
|
|
#include "xbps_api_impl.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file lib/util.c
|
|
|
|
* @brief Utility routines
|
|
|
|
* @defgroup util Utility functions
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
digest2string(const uint8_t *digest, char *string, size_t len)
|
|
|
|
{
|
|
|
|
while (len--) {
|
|
|
|
if (*digest / 16 < 10)
|
|
|
|
*string++ = '0' + *digest / 16;
|
|
|
|
else
|
|
|
|
*string++ = 'a' + *digest / 16 - 10;
|
|
|
|
if (*digest % 16 < 10)
|
|
|
|
*string++ = '0' + *digest % 16;
|
|
|
|
else
|
|
|
|
*string++ = 'a' + *digest % 16 - 10;
|
|
|
|
++digest;
|
|
|
|
}
|
|
|
|
*string = '\0';
|
|
|
|
}
|
|
|
|
|
2014-09-27 16:30:34 +05:30
|
|
|
bool
|
|
|
|
xbps_mmap_file(const char *file, void **mmf, size_t *mmflen, size_t *filelen)
|
2011-07-20 00:51:28 +05:30
|
|
|
{
|
|
|
|
struct stat st;
|
2014-09-27 16:30:34 +05:30
|
|
|
size_t pgsize = (size_t)sysconf(_SC_PAGESIZE);
|
|
|
|
size_t pgmask = pgsize - 1, mapsize;
|
|
|
|
unsigned char *mf;
|
|
|
|
bool need_guard = false;
|
2011-07-20 00:51:28 +05:30
|
|
|
int fd;
|
|
|
|
|
2014-09-27 16:30:34 +05:30
|
|
|
assert(file);
|
|
|
|
|
2015-04-16 00:32:38 +05:30
|
|
|
if ((fd = open(file, O_RDONLY|O_CLOEXEC)) == -1)
|
2014-09-27 16:30:34 +05:30
|
|
|
return false;
|
2014-09-17 10:20:46 +05:30
|
|
|
|
2011-07-20 00:51:28 +05:30
|
|
|
if (fstat(fd, &st) == -1) {
|
|
|
|
(void)close(fd);
|
2014-09-27 16:30:34 +05:30
|
|
|
return false;
|
2011-07-20 00:51:28 +05:30
|
|
|
}
|
|
|
|
if (st.st_size > SSIZE_MAX - 1) {
|
|
|
|
(void)close(fd);
|
2014-09-27 16:30:34 +05:30
|
|
|
return false;
|
|
|
|
}
|
|
|
|
mapsize = ((size_t)st.st_size + pgmask) & ~pgmask;
|
|
|
|
if (mapsize < (size_t)st.st_size) {
|
|
|
|
(void)close(fd);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* If the file length is an integral number of pages, then we
|
|
|
|
* need to map a guard page at the end in order to provide the
|
|
|
|
* necessary NUL-termination of the buffer.
|
|
|
|
*/
|
|
|
|
if ((st.st_size & pgmask) == 0)
|
|
|
|
need_guard = true;
|
|
|
|
|
|
|
|
mf = mmap(NULL, need_guard ? mapsize + pgsize : mapsize,
|
|
|
|
PROT_READ, MAP_PRIVATE, fd, 0);
|
|
|
|
(void)close(fd);
|
|
|
|
if (mf == MAP_FAILED) {
|
|
|
|
(void)munmap(mf, mapsize);
|
|
|
|
return false;
|
2011-07-20 00:51:28 +05:30
|
|
|
}
|
|
|
|
|
2014-09-27 16:30:34 +05:30
|
|
|
*mmf = mf;
|
|
|
|
*mmflen = mapsize;
|
|
|
|
*filelen = st.st_size;
|
2013-11-26 17:38:44 +05:30
|
|
|
|
2014-09-27 16:30:34 +05:30
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-06-16 02:51:29 +05:30
|
|
|
unsigned char *
|
|
|
|
xbps_file_hash_raw(const char *file)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
ssize_t len;
|
|
|
|
unsigned char *digest, buf[65536];
|
|
|
|
SHA256_CTX sha256;
|
|
|
|
|
|
|
|
if ((fd = open(file, O_RDONLY)) < 0)
|
|
|
|
return NULL;
|
|
|
|
digest = malloc(SHA256_DIGEST_LENGTH);
|
|
|
|
assert(digest);
|
|
|
|
SHA256_Init(&sha256);
|
|
|
|
while ((len = read(fd, buf, sizeof(buf))) > 0)
|
|
|
|
SHA256_Update(&sha256, buf, len);
|
|
|
|
SHA256_Final(digest, &sha256);
|
|
|
|
close(fd);
|
2016-06-17 20:25:02 +05:30
|
|
|
if(len < 0)
|
|
|
|
return NULL;
|
2016-06-16 02:51:29 +05:30
|
|
|
|
|
|
|
return digest;
|
|
|
|
}
|
|
|
|
|
2014-09-27 16:30:34 +05:30
|
|
|
char *
|
|
|
|
xbps_file_hash(const char *file)
|
|
|
|
{
|
2016-06-16 10:20:14 +05:30
|
|
|
char *hash;
|
2016-06-16 02:51:29 +05:30
|
|
|
unsigned char *digest;
|
2014-09-27 16:30:34 +05:30
|
|
|
|
2016-06-16 02:51:29 +05:30
|
|
|
if (!(digest = xbps_file_hash_raw(file)))
|
2011-07-20 00:51:28 +05:30
|
|
|
return NULL;
|
2014-09-17 10:20:46 +05:30
|
|
|
|
2016-06-16 10:20:14 +05:30
|
|
|
hash = malloc(SHA256_DIGEST_LENGTH * 2 + 1);
|
|
|
|
assert(hash);
|
2011-07-20 00:51:28 +05:30
|
|
|
digest2string(digest, hash, SHA256_DIGEST_LENGTH);
|
2016-06-16 02:51:29 +05:30
|
|
|
free(digest);
|
2011-07-20 00:51:28 +05:30
|
|
|
|
2016-06-16 10:20:14 +05:30
|
|
|
return hash;
|
2011-07-20 00:51:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
xbps_file_hash_check(const char *file, const char *sha256)
|
|
|
|
{
|
|
|
|
char *res;
|
|
|
|
|
|
|
|
assert(file != NULL);
|
|
|
|
assert(sha256 != NULL);
|
|
|
|
|
|
|
|
res = xbps_file_hash(file);
|
|
|
|
if (res == NULL)
|
|
|
|
return errno;
|
|
|
|
|
|
|
|
if (strcmp(sha256, res)) {
|
|
|
|
free(res);
|
|
|
|
return ERANGE;
|
|
|
|
}
|
|
|
|
free(res);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-30 11:41:51 +05:30
|
|
|
static const char *
|
2013-06-20 13:56:12 +05:30
|
|
|
file_hash_dictionary(xbps_dictionary_t d, const char *key, const char *file)
|
2011-07-20 00:51:28 +05:30
|
|
|
{
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_object_t obj;
|
|
|
|
xbps_object_iterator_t iter;
|
2012-06-06 00:21:00 +05:30
|
|
|
const char *curfile = NULL, *sha256 = NULL;
|
2011-07-20 00:51:28 +05:30
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
assert(xbps_object_type(d) == XBPS_TYPE_DICTIONARY);
|
2011-07-20 00:51:28 +05:30
|
|
|
assert(key != NULL);
|
|
|
|
assert(file != NULL);
|
|
|
|
|
|
|
|
iter = xbps_array_iter_from_dict(d, key);
|
2011-08-01 16:05:47 +05:30
|
|
|
if (iter == NULL) {
|
|
|
|
errno = ENOENT;
|
2011-07-20 00:51:28 +05:30
|
|
|
return NULL;
|
2011-08-01 16:05:47 +05:30
|
|
|
}
|
2013-06-20 13:56:12 +05:30
|
|
|
while ((obj = xbps_object_iterator_next(iter)) != NULL) {
|
|
|
|
xbps_dictionary_get_cstring_nocopy(obj,
|
2011-07-20 00:51:28 +05:30
|
|
|
"file", &curfile);
|
2012-06-05 23:52:47 +05:30
|
|
|
if (strcmp(file, curfile) == 0) {
|
|
|
|
/* file matched */
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_get_cstring_nocopy(obj,
|
2012-06-05 23:52:47 +05:30
|
|
|
"sha256", &sha256);
|
|
|
|
break;
|
|
|
|
}
|
2011-07-20 00:51:28 +05:30
|
|
|
}
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_object_iterator_release(iter);
|
2011-07-20 00:51:28 +05:30
|
|
|
if (sha256 == NULL)
|
|
|
|
errno = ENOENT;
|
|
|
|
|
|
|
|
return sha256;
|
|
|
|
}
|
2011-08-01 16:05:47 +05:30
|
|
|
|
2012-11-30 11:41:51 +05:30
|
|
|
int HIDDEN
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_file_hash_check_dictionary(struct xbps_handle *xhp,
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_t d,
|
2011-08-01 16:05:47 +05:30
|
|
|
const char *key,
|
|
|
|
const char *file)
|
|
|
|
{
|
2012-06-06 00:21:00 +05:30
|
|
|
const char *sha256d = NULL;
|
|
|
|
char *buf;
|
2011-08-01 16:05:47 +05:30
|
|
|
int rv;
|
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
assert(xbps_object_type(d) == XBPS_TYPE_DICTIONARY);
|
2011-08-01 16:05:47 +05:30
|
|
|
assert(key != NULL);
|
|
|
|
assert(file != NULL);
|
|
|
|
|
2012-11-30 11:41:51 +05:30
|
|
|
if ((sha256d = file_hash_dictionary(d, key, file)) == NULL) {
|
2011-08-01 16:05:47 +05:30
|
|
|
if (errno == ENOENT)
|
|
|
|
return 1; /* no match, file not found */
|
|
|
|
|
|
|
|
return -1; /* error */
|
|
|
|
}
|
|
|
|
|
2012-06-06 00:21:00 +05:30
|
|
|
if (strcmp(xhp->rootdir, "/") == 0) {
|
|
|
|
rv = xbps_file_hash_check(file, sha256d);
|
|
|
|
} else {
|
|
|
|
buf = xbps_xasprintf("%s/%s", xhp->rootdir, file);
|
|
|
|
rv = xbps_file_hash_check(buf, sha256d);
|
|
|
|
free(buf);
|
|
|
|
}
|
|
|
|
if (rv == 0)
|
2011-08-01 16:05:47 +05:30
|
|
|
return 0; /* matched */
|
|
|
|
else if (rv == ERANGE || rv == ENOENT)
|
|
|
|
return 1; /* no match */
|
|
|
|
else
|
|
|
|
return -1; /* error */
|
|
|
|
}
|