2011-12-21 09:55:05 +00:00
|
|
|
/*
|
|
|
|
Serval Distributed Numbering Architecture (DNA)
|
|
|
|
Copyright (C) 2010 Paul Gardner-Stephen
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2012-02-24 06:51:22 +00:00
|
|
|
#include <sqlite3.h>
|
2011-12-18 21:40:02 +00:00
|
|
|
#include "sha2.h"
|
2012-05-25 04:59:55 +00:00
|
|
|
#include "strbuf.h"
|
2011-12-18 21:40:02 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2012-05-23 06:34:00 +00:00
|
|
|
#define RHIZOME_MANIFEST_ID_BYTES crypto_sign_edwards25519sha512batch_PUBLICKEYBYTES
|
|
|
|
#define RHIZOME_MANIFEST_ID_STRLEN (RHIZOME_MANIFEST_ID_BYTES * 2)
|
|
|
|
#define RHIZOME_BUNDLE_KEY_BYTES crypto_sign_edwards25519sha512batch_SECRETKEYBYTES
|
|
|
|
#define RHIZOME_BUNDLE_KEY_STRLEN (RHIZOME_BUNDLE_KEY_BYTES * 2)
|
|
|
|
#define RHIZOME_FILEHASH_BYTES SHA512_DIGEST_LENGTH
|
|
|
|
#define RHIZOME_FILEHASH_STRLEN (RHIZOME_FILEHASH_BYTES * 2)
|
|
|
|
|
2012-05-26 04:12:33 +00:00
|
|
|
#define RHIZOME_CRYPT_PAGE_SIZE 4096
|
2012-05-26 09:35:45 +00:00
|
|
|
#define RHIZOME_CRYPT_KEY_BYTES crypto_stream_xsalsa20_ref_KEYBYTES
|
2012-05-26 04:12:33 +00:00
|
|
|
|
2011-12-21 05:58:08 +00:00
|
|
|
#define RHIZOME_HTTP_PORT 4110
|
|
|
|
|
2012-05-22 03:35:29 +00:00
|
|
|
extern long long rhizome_voice_timeout;
|
|
|
|
|
2011-12-22 17:55:18 +00:00
|
|
|
typedef struct rhizome_http_request {
|
|
|
|
int socket;
|
|
|
|
long long last_activity; /* time of last activity in ms */
|
|
|
|
long long initiate_time; /* time connection was initiated */
|
|
|
|
|
|
|
|
/* The HTTP request as currently received */
|
|
|
|
int request_length;
|
|
|
|
#define RHIZOME_HTTP_REQUEST_MAXLEN 1024
|
|
|
|
char request[RHIZOME_HTTP_REQUEST_MAXLEN];
|
|
|
|
|
|
|
|
/* Nature of the request */
|
|
|
|
int request_type;
|
|
|
|
#define RHIZOME_HTTP_REQUEST_RECEIVING -1
|
2012-01-02 22:27:52 +00:00
|
|
|
#define RHIZOME_HTTP_REQUEST_FROMBUFFER 1
|
|
|
|
#define RHIZOME_HTTP_REQUEST_FILE 2
|
|
|
|
#define RHIZOME_HTTP_REQUEST_SUBSCRIBEDGROUPLIST 4
|
|
|
|
#define RHIZOME_HTTP_REQUEST_ALLGROUPLIST 8
|
|
|
|
#define RHIZOME_HTTP_REQUEST_BUNDLESINGROUP 16
|
|
|
|
// manifests are small enough to send from a buffer
|
|
|
|
// #define RHIZOME_HTTP_REQUEST_BUNDLEMANIFEST 32
|
|
|
|
// for anything too big, we can just use a blob
|
|
|
|
#define RHIZOME_HTTP_REQUEST_BLOB 64
|
2012-01-03 04:15:50 +00:00
|
|
|
#define RHIZOME_HTTP_REQUEST_FAVICON 128
|
2011-12-22 17:55:18 +00:00
|
|
|
|
|
|
|
/* Local buffer of data to be sent.
|
|
|
|
If a RHIZOME_HTTP_REQUEST_FROMBUFFER, then the buffer is sent, and when empty
|
|
|
|
the request is closed.
|
|
|
|
Else emptying the buffer triggers a request to fetch more data. Only if no
|
|
|
|
more data is provided do we then close the request. */
|
|
|
|
unsigned char *buffer;
|
|
|
|
int buffer_size; // size
|
|
|
|
int buffer_length; // number of bytes loaded into buffer
|
|
|
|
int buffer_offset; // where we are between [0,buffer_length)
|
|
|
|
|
|
|
|
/* The source specification data which are used in different ways by different
|
|
|
|
request types */
|
|
|
|
unsigned char source[1024];
|
|
|
|
long long source_index;
|
2012-01-03 04:15:50 +00:00
|
|
|
long long source_count;
|
|
|
|
int source_record_size;
|
2012-01-03 04:24:32 +00:00
|
|
|
unsigned int source_flags;
|
2011-12-22 17:55:18 +00:00
|
|
|
|
2012-01-02 22:27:52 +00:00
|
|
|
char *blob_table;
|
|
|
|
char *blob_column;
|
|
|
|
unsigned long long blob_rowid;
|
|
|
|
/* source_index used for offset in blob */
|
|
|
|
unsigned long long blob_end;
|
|
|
|
|
2011-12-22 17:55:18 +00:00
|
|
|
} rhizome_http_request;
|
|
|
|
|
|
|
|
#define RHIZOME_SERVER_MAX_LIVE_REQUESTS 32
|
|
|
|
|
2011-12-20 11:39:49 +00:00
|
|
|
#define RHIZOME_PRIORITY_HIGHEST RHIZOME_PRIORITY_SERVAL_CORE
|
|
|
|
#define RHIZOME_PRIORITY_SERVAL_CORE 5
|
|
|
|
#define RHIZOME_PRIORITY_SUBSCRIBED 4
|
|
|
|
#define RHIZOME_PRIORITY_SERVAL_OPTIONAL 3
|
|
|
|
#define RHIZOME_PRIORITY_DEFAULT 2
|
|
|
|
#define RHIZOME_PRIORITY_SERVAL_BULK 1
|
|
|
|
#define RHIZOME_PRIORITY_NOTINTERESTED 0
|
|
|
|
|
2011-12-20 06:57:24 +00:00
|
|
|
typedef struct rhizome_signature {
|
|
|
|
unsigned char signature[crypto_sign_edwards25519sha512batch_BYTES
|
2011-12-20 21:16:12 +00:00
|
|
|
+crypto_sign_edwards25519sha512batch_PUBLICKEYBYTES+1];
|
2011-12-20 06:57:24 +00:00
|
|
|
int signatureLength;
|
|
|
|
} rhizome_signature;
|
|
|
|
|
2012-01-03 06:05:02 +00:00
|
|
|
#define RHIZOME_BAR_BYTES 32
|
|
|
|
|
2011-12-18 21:34:31 +00:00
|
|
|
#define MAX_MANIFEST_VARS 256
|
|
|
|
#define MAX_MANIFEST_BYTES 8192
|
|
|
|
typedef struct rhizome_manifest {
|
2012-01-28 01:15:45 +00:00
|
|
|
int manifest_record_number;
|
2011-12-18 21:34:31 +00:00
|
|
|
int manifest_bytes;
|
2012-01-13 10:43:17 +00:00
|
|
|
int manifest_all_bytes;
|
2011-12-18 21:34:31 +00:00
|
|
|
unsigned char manifestdata[MAX_MANIFEST_BYTES];
|
2011-12-18 21:52:34 +00:00
|
|
|
unsigned char manifesthash[crypto_hash_sha512_BYTES];
|
2011-12-18 21:34:31 +00:00
|
|
|
|
|
|
|
/* CryptoSign key pair for this manifest.
|
|
|
|
The filename as distributed on Rhizome will be the public key
|
|
|
|
of this pair, thus ensuring that noone can tamper with a bundle
|
|
|
|
except the creator. */
|
2011-12-18 21:52:34 +00:00
|
|
|
unsigned char cryptoSignPublic[crypto_sign_edwards25519sha512batch_PUBLICKEYBYTES];
|
|
|
|
unsigned char cryptoSignSecret[crypto_sign_edwards25519sha512batch_SECRETKEYBYTES];
|
2011-12-18 21:34:31 +00:00
|
|
|
|
|
|
|
int var_count;
|
|
|
|
char *vars[MAX_MANIFEST_VARS];
|
|
|
|
char *values[MAX_MANIFEST_VARS];
|
|
|
|
|
|
|
|
int sig_count;
|
2011-12-20 21:16:12 +00:00
|
|
|
/* Parties who have signed this manifest (raw byte format) */
|
|
|
|
unsigned char *signatories[MAX_MANIFEST_VARS];
|
|
|
|
/*
|
|
|
|
0x61 = crypto_sign_edwards25519sha512batch()
|
|
|
|
*/
|
2011-12-18 21:34:31 +00:00
|
|
|
unsigned char signatureTypes[MAX_MANIFEST_VARS];
|
2011-12-20 21:16:12 +00:00
|
|
|
|
2012-01-12 03:35:05 +00:00
|
|
|
int errors; /* if non-zero, then manifest should not be trusted */
|
2011-12-18 21:34:31 +00:00
|
|
|
|
|
|
|
/* Set non-zero after variables have been packed and
|
|
|
|
signature blocks appended.
|
2011-12-20 00:55:52 +00:00
|
|
|
All fields below may not be valid until the manifest has been finalised */
|
2011-12-18 21:34:31 +00:00
|
|
|
int finalised;
|
|
|
|
|
2012-01-03 06:05:02 +00:00
|
|
|
/* time-to-live in hops of this manifest. */
|
|
|
|
int ttl;
|
|
|
|
|
2011-12-18 21:34:31 +00:00
|
|
|
/* When finalised, we keep the filehash and maximum priority due to any
|
|
|
|
group membership handy */
|
|
|
|
long long fileLength;
|
2011-12-20 00:55:52 +00:00
|
|
|
int fileHashedP;
|
2011-12-18 21:34:31 +00:00
|
|
|
char fileHexHash[SHA512_DIGEST_STRING_LENGTH];
|
|
|
|
int fileHighestPriority;
|
2012-05-25 15:20:48 +00:00
|
|
|
/* Absolute path of the file associated with the manifest */
|
|
|
|
char *dataFileName;
|
|
|
|
/* Whether the paylaod is encrypted or not */
|
|
|
|
int payloadEncryption;
|
2011-12-18 21:34:31 +00:00
|
|
|
|
2011-12-20 05:18:26 +00:00
|
|
|
/* Whether we have the secret for this manifest on hand */
|
|
|
|
int haveSecret;
|
|
|
|
/* Whether the manifest contains a signature that corresponds to the
|
|
|
|
manifest id (ie public key) */
|
|
|
|
int selfSigned;
|
|
|
|
|
2011-12-18 21:34:31 +00:00
|
|
|
/* Version of the manifest. Typically the number of milliseconds since 1970. */
|
|
|
|
long long version;
|
|
|
|
|
2011-12-20 02:54:09 +00:00
|
|
|
int group_count;
|
|
|
|
char *groups[MAX_MANIFEST_VARS];
|
|
|
|
|
2011-12-18 21:34:31 +00:00
|
|
|
} rhizome_manifest;
|
|
|
|
|
2012-05-20 06:37:22 +00:00
|
|
|
/* Supported service identifiers. These go in the 'service' field of every
|
|
|
|
* manifest, and indicate which application must be used to process the bundle
|
|
|
|
* after it is received by Rhizome.
|
|
|
|
*/
|
|
|
|
#define RHIZOME_SERVICE_FILE "file"
|
|
|
|
#define RHIZOME_SERVICE_MESHMS "MeshMS1"
|
|
|
|
|
2011-12-18 21:40:02 +00:00
|
|
|
extern long long rhizome_space;
|
2012-05-24 01:58:32 +00:00
|
|
|
|
2012-05-15 03:26:10 +00:00
|
|
|
int rhizome_set_datastore_path(const char *path);
|
2012-05-24 01:58:32 +00:00
|
|
|
|
|
|
|
const char *rhizome_datastore_path();
|
2012-05-14 06:02:28 +00:00
|
|
|
int form_rhizome_datastore_path(char * buf, size_t bufsiz, const char *fmt, ...);
|
|
|
|
int create_rhizome_datastore_dir();
|
|
|
|
|
2012-05-24 01:58:32 +00:00
|
|
|
int form_rhizome_import_path(char * buf, size_t bufsiz, const char *fmt, ...);
|
|
|
|
int create_rhizome_import_dir();
|
|
|
|
|
2012-05-14 06:02:28 +00:00
|
|
|
/* Handy statement for forming the path of a rhizome store file in a char buffer whose declaration
|
|
|
|
* is in scope (so that sizeof(buf) will work). Evaluates to true if the pathname fitted into
|
2012-05-24 01:58:32 +00:00
|
|
|
* the provided buffer, false (0) otherwise (after logging an error). */
|
2012-05-14 06:02:28 +00:00
|
|
|
#define FORM_RHIZOME_DATASTORE_PATH(buf,fmt,...) (form_rhizome_datastore_path((buf), sizeof(buf), (fmt), ##__VA_ARGS__))
|
2012-05-24 01:58:32 +00:00
|
|
|
#define FORM_RHIZOME_IMPORT_PATH(buf,fmt,...) (form_rhizome_import_path((buf), sizeof(buf), (fmt), ##__VA_ARGS__))
|
2011-12-18 21:34:31 +00:00
|
|
|
|
2011-12-18 21:40:02 +00:00
|
|
|
extern sqlite3 *rhizome_db;
|
2011-12-18 21:34:31 +00:00
|
|
|
|
2011-12-20 00:55:52 +00:00
|
|
|
int rhizome_opendb();
|
2011-12-18 21:34:31 +00:00
|
|
|
int rhizome_manifest_createid(rhizome_manifest *m);
|
2012-05-02 08:27:35 +00:00
|
|
|
int rhizome_strn_is_manifest_id(const char *text);
|
|
|
|
int rhizome_str_is_manifest_id(const char *text);
|
|
|
|
int rhizome_strn_is_file_hash(const char *text);
|
|
|
|
int rhizome_str_is_file_hash(const char *text);
|
2012-04-02 08:12:40 +00:00
|
|
|
int rhizome_write_manifest_file(rhizome_manifest *m, const char *filename);
|
2012-05-25 15:20:48 +00:00
|
|
|
int rhizome_manifest_selfsign(rhizome_manifest *m);
|
2012-05-27 06:30:51 +00:00
|
|
|
int rhizome_drop_stored_file(const char *id,int maximum_priority);
|
2011-12-18 21:34:31 +00:00
|
|
|
int rhizome_manifest_priority(char *id);
|
2012-05-25 23:54:47 +00:00
|
|
|
int rhizome_read_manifest_file(rhizome_manifest *m, const char *filename, int bufferPAndSize);
|
2012-05-25 15:20:48 +00:00
|
|
|
int rhizome_hash_file(rhizome_manifest *m, const char *filename,char *hash_out);
|
2012-04-12 09:00:52 +00:00
|
|
|
char *rhizome_manifest_get(const rhizome_manifest *m, const char *var, char *out, int maxlen);
|
|
|
|
long long rhizome_manifest_get_ll(rhizome_manifest *m, const char *var);
|
2011-12-18 21:34:31 +00:00
|
|
|
int rhizome_manifest_set_ll(rhizome_manifest *m,char *var,long long value);
|
2012-04-02 08:12:40 +00:00
|
|
|
int rhizome_manifest_set(rhizome_manifest *m, const char *var, const char *value);
|
2011-12-18 21:34:31 +00:00
|
|
|
long long rhizome_file_size(char *filename);
|
2012-01-28 01:15:45 +00:00
|
|
|
void _rhizome_manifest_free(const char *sourcefile,const char *funcname,int line,
|
|
|
|
rhizome_manifest *m);
|
|
|
|
#define rhizome_manifest_free(m) _rhizome_manifest_free(__FILE__,__FUNCTION__,__LINE__,m)
|
|
|
|
rhizome_manifest *_rhizome_new_manifest(const char *file,const char *func,int line);
|
|
|
|
#define rhizome_new_manifest() _rhizome_new_manifest(__FILE__,__FUNCTION__,__LINE__)
|
2011-12-18 21:34:31 +00:00
|
|
|
int rhizome_manifest_pack_variables(rhizome_manifest *m);
|
2012-05-25 15:20:48 +00:00
|
|
|
int rhizome_store_bundle(rhizome_manifest *m);
|
2011-12-18 21:34:31 +00:00
|
|
|
int rhizome_manifest_add_group(rhizome_manifest *m,char *groupid);
|
2012-05-25 04:59:55 +00:00
|
|
|
int rhizome_clean_payload(const char *fileidhex);
|
2012-05-26 04:19:13 +00:00
|
|
|
int rhizome_store_file(rhizome_manifest *m,const unsigned char *key);
|
2011-12-18 21:34:31 +00:00
|
|
|
int rhizome_finish_sqlstatement(sqlite3_stmt *statement);
|
2012-05-25 15:20:48 +00:00
|
|
|
int rhizome_bundle_import(rhizome_manifest *m_in, rhizome_manifest **m_out,
|
|
|
|
const char *bundle, int ttl);
|
|
|
|
|
2012-05-27 06:30:51 +00:00
|
|
|
int rhizome_manifest_verify(rhizome_manifest *m);
|
2012-05-25 23:06:29 +00:00
|
|
|
int rhizome_manifest_check_sanity(rhizome_manifest *m_in);
|
2012-05-25 15:20:48 +00:00
|
|
|
int rhizome_manifest_check_file(rhizome_manifest *m_in);
|
|
|
|
int rhizome_manifest_check_duplicate(rhizome_manifest *m_in,rhizome_manifest **m_out);
|
|
|
|
|
|
|
|
int rhizome_manifest_bind_id(rhizome_manifest *m_in,const char *author);
|
|
|
|
int rhizome_manifest_bind_file(rhizome_manifest *m_in,const char *filename,int encryptP);
|
|
|
|
int rhizome_manifest_finalise(rhizome_manifest *m);
|
|
|
|
int rhizome_add_manifest(rhizome_manifest *m_in,int ttl);
|
|
|
|
|
2012-05-23 06:34:00 +00:00
|
|
|
void rhizome_bytes_to_hex_upper(unsigned const char *in, char *out, int byteCount);
|
2012-05-02 06:33:09 +00:00
|
|
|
int rhizome_hex_to_bytes(const char *in,unsigned char *out,int hexChars);
|
2012-05-15 07:54:25 +00:00
|
|
|
int rhizome_find_privatekey(rhizome_manifest *m);
|
2012-05-15 10:34:41 +00:00
|
|
|
rhizome_signature *rhizome_sign_hash(rhizome_manifest *m,const char *author);
|
2011-12-22 17:55:18 +00:00
|
|
|
int rhizome_server_free_http_request(rhizome_http_request *r);
|
|
|
|
int rhizome_server_close_http_request(int i);
|
|
|
|
int rhizome_server_http_send_bytes(int rn,rhizome_http_request *r);
|
|
|
|
int rhizome_server_parse_http_request(int rn,rhizome_http_request *r);
|
2011-12-28 23:41:03 +00:00
|
|
|
int rhizome_server_simple_http_response(rhizome_http_request *r,int result, char *response);
|
2012-05-25 10:12:45 +00:00
|
|
|
long long sqlite_exec_void(const char *sqlformat,...);
|
|
|
|
long long sqlite_exec_int64(const char *sqlformat,...);
|
|
|
|
int sqlite_exec_strbuf(strbuf sb, const char *sqlformat,...);
|
2012-01-02 22:27:52 +00:00
|
|
|
int rhizome_server_http_response_header(rhizome_http_request *r,int result,
|
|
|
|
char *mime_type,unsigned long long bytes);
|
2012-01-03 04:15:50 +00:00
|
|
|
int rhizome_server_sql_query_fill_buffer(int rn,rhizome_http_request *r);
|
2012-01-03 06:05:02 +00:00
|
|
|
double rhizome_manifest_get_double(rhizome_manifest *m,char *var,double default_value);
|
|
|
|
int chartonybl(int c);
|
|
|
|
int rhizome_manifest_extract_signature(rhizome_manifest *m,int *ofs);
|
2012-05-26 04:12:33 +00:00
|
|
|
int rhizome_update_file_priority(const char *fileid);
|
2012-05-25 23:02:17 +00:00
|
|
|
int rhizome_find_duplicate(const rhizome_manifest *m, rhizome_manifest **found,
|
|
|
|
int checkVersionP);
|
2012-01-12 03:35:05 +00:00
|
|
|
int rhizome_manifest_to_bar(rhizome_manifest *m,unsigned char *bar);
|
2012-05-23 06:34:00 +00:00
|
|
|
char nybltochar_upper(int n);
|
2012-05-25 07:31:56 +00:00
|
|
|
int rhizome_queue_manifest_import(rhizome_manifest *m, struct sockaddr_in *peerip, int *manifest_kept);
|
2012-05-16 04:16:42 +00:00
|
|
|
int rhizome_list_manifests(const char *service, const char *sender_sid, const char *recipient_sid, int limit, int offset);
|
2012-05-02 08:27:35 +00:00
|
|
|
int rhizome_retrieve_manifest(const char *manifestid, rhizome_manifest **mp);
|
2012-05-26 04:12:33 +00:00
|
|
|
int rhizome_retrieve_file(const char *fileid, const char *filepath,
|
|
|
|
const unsigned char *key);
|
2012-01-12 03:35:05 +00:00
|
|
|
|
|
|
|
#define RHIZOME_DONTVERIFY 0
|
|
|
|
#define RHIZOME_VERIFY 1
|
2012-01-13 06:51:06 +00:00
|
|
|
|
|
|
|
int rhizome_fetching_get_fds(struct pollfd *fds,int *fdcount,int fdmax);
|
2012-01-27 06:41:18 +00:00
|
|
|
int rhizome_manifest_version_cache_lookup(rhizome_manifest *m);
|
|
|
|
int rhizome_manifest_version_cache_store(rhizome_manifest *m);
|
2012-05-11 21:54:52 +00:00
|
|
|
int monitor_announce_bundle(rhizome_manifest *m);
|
2012-05-15 07:54:25 +00:00
|
|
|
int rhizome_bk_xor(const char *author,
|
|
|
|
unsigned char bid[crypto_sign_edwards25519sha512batch_PUBLICKEYBYTES],
|
|
|
|
unsigned char bkin[crypto_sign_edwards25519sha512batch_SECRETKEYBYTES],
|
|
|
|
unsigned char bkout[crypto_sign_edwards25519sha512batch_SECRETKEYBYTES]);
|
2012-05-15 10:34:41 +00:00
|
|
|
int rhizome_extract_privatekey(rhizome_manifest *m,const char *authorHex);
|
2012-05-22 04:45:35 +00:00
|
|
|
int rhizome_queue_ignore_manifest(rhizome_manifest *m,
|
|
|
|
struct sockaddr_in *peerip,int timeout);
|
|
|
|
int rhizome_ignore_manifest_check(rhizome_manifest *m,
|
|
|
|
struct sockaddr_in *peerip);
|
2012-05-22 05:32:28 +00:00
|
|
|
|
|
|
|
/* one manifest is required per candidate, plus a few spare.
|
|
|
|
so MAX_RHIZOME_MANIFESTS must be > MAX_CANDIDATES.
|
|
|
|
*/
|
|
|
|
#define MAX_RHIZOME_MANIFESTS 24
|
|
|
|
#define MAX_CANDIDATES 16
|
2012-05-22 06:06:21 +00:00
|
|
|
|
|
|
|
int rhizome_suggest_queue_manifest_import(rhizome_manifest *m,
|
|
|
|
struct sockaddr_in *peerip);
|
2012-05-22 06:18:12 +00:00
|
|
|
int rhizome_enqueue_suggestions();
|