diff --git a/CMakeLists.txt b/CMakeLists.txt index adca72c..d12b8c8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,7 +12,7 @@ set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--no-as-needed") add_library(mfapi SHARED mfapi/mfconn.c mfapi/file.c mfapi/folder.c mfapi/apicalls/file_get_info.c mfapi/apicalls/user_get_info.c mfapi/apicalls/file_get_links.c mfapi/apicalls/user_session.c mfapi/apicalls/folder_get_info.c mfapi/apicalls/folder_create.c mfapi/apicalls/folder_get_content.c mfapi/apicalls/folder_delete.c mfapi/apicalls/device_get_status.c mfapi/apicalls/device_get_changes.c) -add_library(mfutils SHARED utils/http.c utils/json.c utils/strings.c utils/stringv.c utils/xdelta3.c) +add_library(mfutils SHARED utils/http.c utils/json.c utils/strings.c utils/stringv.c utils/xdelta3.c utils/hash.c) add_executable(mediafire-shell mfshell/main.c mfshell/mfshell.c mfshell/commands/folder.c mfshell/commands/auth.c mfshell/commands/chdir.c mfshell/commands/debug.c mfshell/commands/file.c mfshell/commands/get.c mfshell/commands/help.c mfshell/commands/host.c mfshell/commands/lcd.c mfshell/commands/links.c mfshell/commands/list.c mfshell/commands/lpwd.c mfshell/commands/mkdir.c mfshell/commands/pwd.c mfshell/commands/whoami.c mfshell/commands/rmdir.c mfshell/commands/status.c mfshell/commands/changes.c mfshell/config.c mfshell/options.c) target_link_libraries(mediafire-shell curl ssl crypto jansson mfapi mfutils) diff --git a/fuse/hashtbl.c b/fuse/hashtbl.c index 4aedce9..7e8f868 100644 --- a/fuse/hashtbl.c +++ b/fuse/hashtbl.c @@ -33,6 +33,7 @@ #include #include #include +#include #include "hashtbl.h" #include "../mfapi/mfconn.h" @@ -41,6 +42,7 @@ #include "../mfapi/apicalls.h" #include "../utils/strings.h" #include "../utils/http.h" +#include "../utils/hash.h" /* * we build a hashtable using the first three characters of the file or folder @@ -50,7 +52,9 @@ #define NUM_BUCKETS 46656 /* - * we use this table to convert from a base36 char to an integer + * we use this table to convert from a base36 char (ignoring case) to an + * integer or from a hex string to binary (in the latter case letters g-z and + * G-Z remain unused) * we "waste" these 128 bytes of memory so that we don't need branching * instructions when decoding * we only need 128 bytes because the input is a *signed* char @@ -60,12 +64,41 @@ static unsigned char base36_decoding_table[] = { /* 0x10 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x30 */ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, -/* 0x40 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -/* 0x50 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +/* 0x40 */ 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, +/* 0x50 */ 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 0, 0, 0, 0, 0, /* 0x60 */ 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, /* 0x70 */ 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 0, 0, 0, 0, 0 }; +/* + * table to convert from a byte into the two hexadecimal digits representing + * it + */ +static char base16_encoding_table[][2] = { + "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "0A", "0B", + "0C", "0D", "0E", "0F", "10", "11", "12", "13", "14", "15", "16", "17", + "18", "19", "1A", "1B", "1C", "1D", "1E", "1F", "20", "21", "22", "23", + "24", "25", "26", "27", "28", "29", "2A", "2B", "2C", "2D", "2E", "2F", + "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "3A", "3B", + "3C", "3D", "3E", "3F", "40", "41", "42", "43", "44", "45", "46", "47", + "48", "49", "4A", "4B", "4C", "4D", "4E", "4F", "50", "51", "52", "53", + "54", "55", "56", "57", "58", "59", "5A", "5B", "5C", "5D", "5E", "5F", + "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "6A", "6B", + "6C", "6D", "6E", "6F", "70", "71", "72", "73", "74", "75", "76", "77", + "78", "79", "7A", "7B", "7C", "7D", "7E", "7F", "80", "81", "82", "83", + "84", "85", "86", "87", "88", "89", "8A", "8B", "8C", "8D", "8E", "8F", + "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "9A", "9B", + "9C", "9D", "9E", "9F", "A0", "A1", "A2", "A3", "A4", "A5", "A6", "A7", + "A8", "A9", "AA", "AB", "AC", "AD", "AE", "AF", "B0", "B1", "B2", "B3", + "B4", "B5", "B6", "B7", "B8", "B9", "BA", "BB", "BC", "BD", "BE", "BF", + "C0", "C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "CA", "CB", + "CC", "CD", "CE", "CF", "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7", + "D8", "D9", "DA", "DB", "DC", "DD", "DE", "DF", "E0", "E1", "E2", "E3", + "E4", "E5", "E6", "E7", "E8", "E9", "EA", "EB", "EC", "ED", "EE", "EF", + "F0", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "FA", "FB", + "FC", "FD", "FE", "FF" +}; + /* * a macro to convert a char* of the key into a hash of its first three * characters, treating those first three characters as if they represented a @@ -79,6 +112,47 @@ static unsigned char base36_decoding_table[] = { base36_decoding_table[(int)(key)[1]]*36+\ base36_decoding_table[(int)(key)[2]] +/* decodes a zero terminated string containing hex characters into their + * binary representation. The length of the string must be even as pairs of + * characters are converted to one output byte. The output buffer must be at + * least half the length of the input string. + */ +static void hex2binary(const char *hex, unsigned char *binary) +{ + unsigned char val1, + val2; + const char *c1, + *c2; + unsigned char *b; + + for (b = binary, c1 = hex, c2 = hex + 1; + *c1 != '\0' && *c2 != '\0'; b++, c1 += 2, c2 += 2) { + val1 = base36_decoding_table[(int)(*c1)]; + val2 = base36_decoding_table[(int)(*c2)]; + *b = (val1 << 4) | val2; + } +} + +static char *binary2hex(unsigned char *binary, size_t length) +{ + char *out; + char *p; + size_t i; + + out = malloc(length * 2 + 1); + if (out == NULL) { + fprintf(stderr, "cannot allocate memory\n"); + return NULL; + } + for (i = 0; i < length; i++) { + p = base16_encoding_table[binary[i]]; + out[i * 2] = p[0]; + out[i * 2 + 1] = p[1]; + } + out[length * 2] = '\0'; + return out; +} + struct h_entry { /* * keys are either 13 (folders) or 15 (files) long since the structure @@ -124,7 +198,7 @@ struct h_entry { * only for files * ******************/ /* SHA256 is 256 bits = 32 bytes */ - unsigned char hash[32]; + unsigned char hash[SHA256_DIGEST_LENGTH]; /* * last access time to remove old locally cached files * atime is also never zero for files @@ -751,6 +825,9 @@ int folder_tree_open_file(folder_tree * tree, mfconn * conn, const char *path) mffile *file; int retval; mfhttp *http; + FILE *fh; + unsigned char hash[SHA256_DIGEST_LENGTH]; + char *hexhash; entry = folder_tree_lookup_path(tree, conn, path); @@ -827,6 +904,39 @@ int folder_tree_open_file(folder_tree * tree, mfconn * conn, const char *path) return -1; } + /* size matches - now compare the hash as well */ + fh = fopen(cachefile, "r"); + if (fh == NULL) { + perror("cannot open file"); + free(cachefile); + file_free(file); + return -1; + } + + retval = calc_sha256(fh, hash); + if (retval != 0) { + fprintf(stderr, "failed to calculate hash\n"); + free(cachefile); + file_free(file); + fclose(fh); + return -1; + } + + fclose(fh); + + if (memcmp(entry->hash, hash, SHA256_DIGEST_LENGTH) != 0) { + fprintf(stderr, "hashes are not equal\n"); + hexhash = binary2hex(entry->hash, SHA256_DIGEST_LENGTH); + fprintf(stderr, "remote: %s\n", hexhash); + free(hexhash); + hexhash = binary2hex(hash, SHA256_DIGEST_LENGTH); + fprintf(stderr, "downloaded: %s\n", hexhash); + free(hexhash); + free(cachefile); + file_free(file); + return -1; + } + file_free(file); fd = open(cachefile, O_RDWR); @@ -1030,6 +1140,9 @@ static struct h_entry *folder_tree_add_file(folder_tree * tree, mffile * file, new_entry->fsize = file_get_size(file); new_entry->needs_update = false; + /* convert the hex string into its binary representation */ + hex2binary(file_get_hash(file), new_entry->hash); + /* mark this h_entry struct as a file if its atime is not set yet */ if (new_entry->atime == 0) new_entry->atime = 1; diff --git a/mfapi/apicalls/folder_get_content.c b/mfapi/apicalls/folder_get_content.c index 554ebf4..8e7d54e 100644 --- a/mfapi/apicalls/folder_get_content.c +++ b/mfapi/apicalls/folder_get_content.c @@ -267,7 +267,7 @@ static int _decode_folder_get_content_files(mfhttp * conn, void *user_ptr) file_set_revision(tmp_file, atoll(json_string_value(j_obj))); } - // FIXME don't save hex ascii string but binary chars instead + j_obj = json_object_get(data, "hash"); if (j_obj != NULL) { file_set_hash(tmp_file, json_string_value(j_obj)); diff --git a/mfapi/file.c b/mfapi/file.c index 70a4503..3428997 100644 --- a/mfapi/file.c +++ b/mfapi/file.c @@ -23,6 +23,7 @@ #include #include #include +#include #include "file.h" #include "apicalls.h" @@ -30,7 +31,9 @@ struct mffile { char quickkey[MFAPI_MAX_LEN_KEY + 1]; char parent[MFAPI_MAX_LEN_NAME + 1]; - char hash[65]; + /* the hex representation takes twice the amount of the binary length plus + * 1 for the terminating zero byte */ + char hash[SHA256_DIGEST_LENGTH * 2 + 1]; char name[MFAPI_MAX_LEN_NAME + 1]; time_t created; uint64_t revision; diff --git a/utils/hash.c b/utils/hash.c new file mode 100644 index 0000000..5ad24c7 --- /dev/null +++ b/utils/hash.c @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2014 Johannes Schauer + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2, as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#include +#include +#include +#include + +#define bufsize 32768 + +int calc_md5(FILE * file, unsigned char *hash) +{ + int bytesRead; + char *buffer; + MD5_CTX md5; + + MD5_Init(&md5); + buffer = malloc(bufsize); + if (buffer == NULL) { + return -1; + } + while ((bytesRead = fread(buffer, 1, bufsize, file))) { + MD5_Update(&md5, buffer, bytesRead); + } + MD5_Final(hash, &md5); + free(buffer); + return 0; +} + +int calc_sha256(FILE * file, unsigned char *hash) +{ + int bytesRead; + char *buffer; + SHA256_CTX sha256; + + SHA256_Init(&sha256); + buffer = malloc(bufsize); + if (buffer == NULL) { + return -1; + } + while ((bytesRead = fread(buffer, 1, bufsize, file))) { + SHA256_Update(&sha256, buffer, bytesRead); + } + SHA256_Final(hash, &sha256); + free(buffer); + return 0; +} diff --git a/utils/hash.h b/utils/hash.h new file mode 100644 index 0000000..f375a1d --- /dev/null +++ b/utils/hash.h @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2014 Johannes Schauer + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2, as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#ifndef _MFSHELL_HASH_H_ +#define _MFSHELL_HASH_H_ + +int calc_md5(FILE * file, unsigned char *hash); +int calc_sha256(FILE * file, unsigned char *hash); + +#endif