mirror of
https://github.com/servalproject/serval-dna.git
synced 2024-12-19 21:27:57 +00:00
Remove file hash command
This commit is contained in:
parent
c5d6579397
commit
dc7226b6a5
4
cli.c
4
cli.c
@ -387,12 +387,12 @@ int cli_bid(const char *arg)
|
||||
|
||||
int cli_optional_bid(const char *arg)
|
||||
{
|
||||
return !arg[0] || str_to_rhizome_bid_t(NULL, arg) != -1;
|
||||
return !arg[0] || is_xsubstring(arg, sizeof(rhizome_bid_t));
|
||||
}
|
||||
|
||||
int cli_fileid(const char *arg)
|
||||
{
|
||||
return str_to_rhizome_filehash_t(NULL, arg) != -1;
|
||||
return is_xsubstring(arg, sizeof(rhizome_filehash_t));
|
||||
}
|
||||
|
||||
int cli_optional_bundle_crypt_key(const char *arg)
|
||||
|
@ -136,29 +136,13 @@ int cmp_rhizome_filehash_t(const rhizome_filehash_t *a, const rhizome_filehash_t
|
||||
|
||||
int str_to_rhizome_filehash_t(rhizome_filehash_t *hashp, const char *hex)
|
||||
{
|
||||
return parse_rhizome_filehash_t(hashp, hex, -1, NULL); // checks for nul terminator
|
||||
return parse_hex_t(hashp, hex);
|
||||
}
|
||||
|
||||
int strn_to_rhizome_filehash_t(rhizome_filehash_t *hashp, const char *hex, size_t hexlen)
|
||||
{
|
||||
return parse_rhizome_filehash_t(hashp, hex, hexlen, NULL); // does not check for nul terminator
|
||||
}
|
||||
|
||||
int parse_rhizome_filehash_t(rhizome_filehash_t *hashp, const char *hex, ssize_t hexlen, const char **endp)
|
||||
{
|
||||
if (hexlen != -1 && hexlen != RHIZOME_FILEHASH_STRLEN)
|
||||
return -1;
|
||||
rhizome_filehash_t tmp;
|
||||
int n = fromhex(tmp.binary, hex, sizeof tmp.binary);
|
||||
if (n != sizeof tmp.binary)
|
||||
return -1;
|
||||
if (endp)
|
||||
*endp = hex + RHIZOME_FILEHASH_STRLEN;
|
||||
else if (hexlen == -1 && hex[RHIZOME_FILEHASH_STRLEN] != '\0')
|
||||
return -1;
|
||||
if (hashp)
|
||||
*hashp = tmp;
|
||||
return 0;
|
||||
const char *endp;
|
||||
return parse_hexn_t(hashp, hex, hexlen, &endp);
|
||||
}
|
||||
|
||||
int str_to_rhizome_bk_t(rhizome_bk_t *bkp, const char *hex)
|
||||
|
@ -47,4 +47,7 @@ uint16_t read_uint16(const unsigned char *o);
|
||||
int compare_wrapped_uint8(uint8_t one, uint8_t two);
|
||||
int compare_wrapped_uint16(uint16_t one, uint16_t two);
|
||||
|
||||
#define parse_hex_t(bin, hex) fromhexstr(bin->binary, sizeof bin->binary, hex)
|
||||
#define parse_hexn_t(bin, hex, hexlen, endp) fromhexstrn(bin->binary, sizeof bin->binary, hex, hexlen, endp)
|
||||
|
||||
#endif //__SERVAL_DNA___DATA_FORMATS_H
|
||||
|
@ -466,8 +466,6 @@ const char *rhizome_manifest_validate_reason(rhizome_manifest *m);
|
||||
int rhizome_manifest_parse(rhizome_manifest *m);
|
||||
int rhizome_manifest_verify(rhizome_manifest *m);
|
||||
|
||||
int rhizome_hash_file(rhizome_manifest *m, const char *path, rhizome_filehash_t *hash_out, uint64_t *size_out);
|
||||
|
||||
void _rhizome_manifest_free(struct __sourceloc, rhizome_manifest *m);
|
||||
#define rhizome_manifest_free(m) _rhizome_manifest_free(__WHENCE__,m)
|
||||
rhizome_manifest *_rhizome_new_manifest(struct __sourceloc);
|
||||
|
@ -1184,48 +1184,6 @@ int rhizome_read_manifest_from_file(rhizome_manifest *m, const char *filename)
|
||||
return rhizome_manifest_parse(m);
|
||||
}
|
||||
|
||||
int rhizome_hash_file(rhizome_manifest *m, const char *path, rhizome_filehash_t *hash_out, uint64_t *size_out)
|
||||
{
|
||||
/* Gnarf! NaCl's crypto_hash() function needs the whole file passed in in one
|
||||
go. Trouble is, we need to run Serval DNA on filesystems that lack mmap(),
|
||||
and may be very resource constrained. Thus we need a streamable SHA-512
|
||||
implementation.
|
||||
*/
|
||||
// TODO encrypted payloads
|
||||
if (m && m->payloadEncryption == PAYLOAD_ENCRYPTED)
|
||||
return WHY("Encryption of payloads not implemented");
|
||||
uint64_t filesize = 0;
|
||||
crypto_hash_sha512_state context;
|
||||
crypto_hash_sha512_init(&context);
|
||||
if (path[0]) {
|
||||
int fd = open(path, O_RDONLY);
|
||||
if (fd == -1)
|
||||
return WHYF_perror("open(%s,O_RDONLY)", alloca_str_toprint(path));
|
||||
unsigned char buffer[8192];
|
||||
ssize_t r;
|
||||
while ((r = read(fd, buffer, sizeof buffer))) {
|
||||
if (r == -1) {
|
||||
WHYF_perror("read(%s,%zu)", alloca_str_toprint(path), sizeof buffer);
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
crypto_hash_sha512_update(&context, buffer, (size_t) r);
|
||||
filesize += (size_t) r;
|
||||
}
|
||||
close(fd);
|
||||
}
|
||||
// Empty files (including empty path) have no hash.
|
||||
if (hash_out) {
|
||||
if (filesize > 0)
|
||||
crypto_hash_sha512_final(&context, hash_out->binary);
|
||||
else
|
||||
*hash_out = RHIZOME_FILEHASH_NONE;
|
||||
}
|
||||
if (size_out)
|
||||
*size_out = filesize;
|
||||
return 0;
|
||||
}
|
||||
|
||||
rhizome_manifest *_rhizome_new_manifest(struct __sourceloc __whence)
|
||||
{
|
||||
rhizome_manifest *m=emalloc_zero(sizeof(rhizome_manifest));
|
||||
|
@ -84,24 +84,6 @@ static void cli_put_manifest(struct cli_context *context, const rhizome_manifest
|
||||
cli_put_long(context, m->inserttime, "\n");
|
||||
}
|
||||
|
||||
DEFINE_CMD(app_rhizome_hash_file, 0,
|
||||
"Compute the Rhizome hash of a file",
|
||||
"rhizome","hash","file","<filepath>");
|
||||
static int app_rhizome_hash_file(const struct cli_parsed *parsed, struct cli_context *context)
|
||||
{
|
||||
DEBUG_cli_parsed(verbose, parsed);
|
||||
/* compute hash of file. We do this without a manifest, so it will necessarily
|
||||
return the hash of the file unencrypted. */
|
||||
const char *filepath;
|
||||
cli_arg(parsed, "filepath", &filepath, NULL, "");
|
||||
rhizome_filehash_t hash;
|
||||
uint64_t size;
|
||||
if (rhizome_hash_file(NULL, filepath, &hash, &size) == -1)
|
||||
return -1;
|
||||
cli_put_string(context, size ? alloca_tohex_rhizome_filehash_t(hash) : "", "\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int append_manifest_zip_comment(const char *filepath, rhizome_manifest *m)
|
||||
{
|
||||
int fd = open(filepath, O_RDWR);
|
||||
|
@ -98,7 +98,6 @@ typedef struct rhizome_filehash_binary {
|
||||
int cmp_rhizome_filehash_t(const rhizome_filehash_t *a, const rhizome_filehash_t *b);
|
||||
int str_to_rhizome_filehash_t(rhizome_filehash_t *fh, const char *hex);
|
||||
int strn_to_rhizome_filehash_t(rhizome_filehash_t *fh, const char *hex, size_t hexlen);
|
||||
int parse_rhizome_filehash_t(rhizome_filehash_t *fh, const char *hex, ssize_t hexlen, const char **endp);
|
||||
|
||||
/* Fundamental data type: Rhizome Bundle Key (BK)
|
||||
*
|
||||
|
9
str.c
9
str.c
@ -52,7 +52,7 @@ size_t fromhex(unsigned char *dstBinary, const char *srcHex, size_t nbinary)
|
||||
return -1;
|
||||
}
|
||||
|
||||
int fromhexstr(unsigned char *dstBinary, const char *srcHex, size_t nbinary)
|
||||
int fromhexstr(unsigned char *dstBinary, size_t nbinary, const char *srcHex)
|
||||
{
|
||||
const char *p;
|
||||
if (strn_fromhex(dstBinary, nbinary, srcHex, &p) == nbinary && *p == '\0')
|
||||
@ -60,6 +60,13 @@ int fromhexstr(unsigned char *dstBinary, const char *srcHex, size_t nbinary)
|
||||
return -1;
|
||||
}
|
||||
|
||||
int fromhexstrn(unsigned char *dstBinary, size_t nbinary, const char *srcHex, size_t nHex, const char **afterHex)
|
||||
{
|
||||
if (nbinary *2 == nHex && strn_fromhex(dstBinary, nbinary, srcHex, afterHex) == nbinary)
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
|
||||
size_t strn_fromhex(unsigned char *dstBinary, ssize_t dstsiz, const char *srcHex, const char **afterHex)
|
||||
{
|
||||
unsigned char *dstorig = dstBinary;
|
||||
|
3
str.h
3
str.h
@ -108,7 +108,8 @@ size_t fromhex(unsigned char *dstBinary, const char *srcHex, size_t nbinary);
|
||||
*
|
||||
* @author Andrew Bettison <andrew@servalproject.com>
|
||||
*/
|
||||
int fromhexstr(unsigned char *dstBinary, const char *srcHex, size_t nbinary);
|
||||
int fromhexstr(unsigned char *dstBinary, size_t nbinary, const char *srcHex);
|
||||
int fromhexstrn(unsigned char *dstBinary, size_t nbinary, const char *srcHex, size_t nHex, const char **afterHex);
|
||||
|
||||
/* Decode pairs of ASCII hex characters [0-9A-Fa-f] into binary data with an optional upper limit on
|
||||
* the number of binary bytes produced (destination buffer size). Returns the number of binary
|
||||
|
@ -128,7 +128,7 @@ assert_stdout_add_file() {
|
||||
local filename="$1"
|
||||
shift
|
||||
unpack_manifest_for_grep "$filename" "$manifestname"
|
||||
compute_filehash actual_filehash "$filename" actual_filesize
|
||||
actual_filesize=$(( $(cat "$filename" | wc -c) + 0 ))
|
||||
opt_service=
|
||||
opt_manifestid=
|
||||
opt_author=
|
||||
@ -139,10 +139,6 @@ assert_stdout_add_file() {
|
||||
if replayStdout | $GREP -q '^service:file$'; then
|
||||
opt_name=true
|
||||
fi
|
||||
opt_filehash=true
|
||||
if [ "$re_crypt" = 1 ]; then
|
||||
opt_filehash=false
|
||||
fi
|
||||
fieldnames='service|manifestid|.author|.secret|BK|filesize|filehash|name'
|
||||
for arg; do
|
||||
case "$arg" in
|
||||
@ -169,8 +165,6 @@ assert_stdout_add_file() {
|
||||
${opt_filesize:-true} && assertStdoutGrep --matches=1 "^filesize:$actual_filesize\$"
|
||||
if replayStdout | $GREP -q '^filesize:0$'; then
|
||||
assertStdoutGrep --matches=0 "^filehash:"
|
||||
else
|
||||
${opt_filehash:-true} && assertStdoutGrep --matches=1 "^filehash:$actual_filehash\$"
|
||||
fi
|
||||
${opt_name:-true} && assertStdoutGrep --matches=1 "^name:$re_name\$"
|
||||
}
|
||||
@ -378,22 +372,6 @@ extract_manifest_crypt() {
|
||||
extract_manifest "$1" "$2" crypt "$rexp_crypt"
|
||||
}
|
||||
|
||||
compute_filehash() {
|
||||
local _filehashvar="$1"
|
||||
local _file="$2"
|
||||
local _filesizevar="$3"
|
||||
local _hash=
|
||||
local _size=0
|
||||
if [ -s "$_file" ]; then
|
||||
local _hash=$($servald rhizome hash file "$_file") || error "$servald failed to compute file hash"
|
||||
[ -z "${_hash//[0-9a-fA-F]/}" ] || error "file hash contains non-hex: $_hash"
|
||||
[ "${#_hash}" -eq 128 ] || error "file hash incorrect length: $_hash"
|
||||
local _size=$(( $(cat "$filename" | wc -c) + 0 ))
|
||||
fi
|
||||
[ -n "$_filehashvar" ] && eval $_filehashvar="\$_hash"
|
||||
[ -n "$_filesizevar" ] && eval $_filesizevar="\$_size"
|
||||
}
|
||||
|
||||
rhizome_http_server_started() {
|
||||
local logvar=LOG${1#+}
|
||||
$GREP 'HTTP SERVER START.*port=[0-9].*services=[^ ]*\<Rhizome\>' "${!logvar}"
|
||||
|
Loading…
Reference in New Issue
Block a user