From dc7226b6a583f11ca5f717898c5ba18d8764f83d Mon Sep 17 00:00:00 2001 From: Jeremy Lakeman Date: Mon, 15 Aug 2016 17:18:58 +0930 Subject: [PATCH] Remove file hash command --- cli.c | 4 ++-- dataformats.c | 22 +++------------------- dataformats.h | 3 +++ rhizome.h | 2 -- rhizome_bundle.c | 42 ------------------------------------------ rhizome_cli.c | 18 ------------------ rhizome_types.h | 1 - str.c | 9 ++++++++- str.h | 3 ++- testdefs_rhizome.sh | 24 +----------------------- 10 files changed, 19 insertions(+), 109 deletions(-) diff --git a/cli.c b/cli.c index aba059ec..091a5934 100644 --- a/cli.c +++ b/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) diff --git a/dataformats.c b/dataformats.c index 6dd101c6..932b2363 100644 --- a/dataformats.c +++ b/dataformats.c @@ -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) diff --git a/dataformats.h b/dataformats.h index 2770a111..f787254b 100644 --- a/dataformats.h +++ b/dataformats.h @@ -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 diff --git a/rhizome.h b/rhizome.h index 2fe991f2..d93e680c 100644 --- a/rhizome.h +++ b/rhizome.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); diff --git a/rhizome_bundle.c b/rhizome_bundle.c index 0c559f77..126ea20a 100644 --- a/rhizome_bundle.c +++ b/rhizome_bundle.c @@ -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)); diff --git a/rhizome_cli.c b/rhizome_cli.c index aa537602..e458071e 100644 --- a/rhizome_cli.c +++ b/rhizome_cli.c @@ -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",""); -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); diff --git a/rhizome_types.h b/rhizome_types.h index 7f2aa402..5f840966 100644 --- a/rhizome_types.h +++ b/rhizome_types.h @@ -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) * diff --git a/str.c b/str.c index be89eea2..cc7f3995 100644 --- a/str.c +++ b/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; diff --git a/str.h b/str.h index 33040a02..4c984bc1 100644 --- a/str.h +++ b/str.h @@ -108,7 +108,8 @@ size_t fromhex(unsigned char *dstBinary, const char *srcHex, size_t nbinary); * * @author Andrew Bettison */ -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 diff --git a/testdefs_rhizome.sh b/testdefs_rhizome.sh index b0e3e965..147b9979 100644 --- a/testdefs_rhizome.sh +++ b/testdefs_rhizome.sh @@ -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=[^ ]*\' "${!logvar}"