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-23 02:15:42 +00:00
|
|
|
#include "serval.h"
|
2012-12-04 03:42:28 +00:00
|
|
|
#include "conf.h"
|
2012-11-07 06:12:45 +00:00
|
|
|
#include "str.h"
|
2011-12-18 21:40:02 +00:00
|
|
|
#include "rhizome.h"
|
2012-01-03 06:05:02 +00:00
|
|
|
#include <stdlib.h>
|
2011-12-13 09:04:12 +00:00
|
|
|
|
2012-11-30 04:17:27 +00:00
|
|
|
int is_rhizome_enabled()
|
2012-07-11 07:20:50 +00:00
|
|
|
{
|
2012-12-04 03:42:28 +00:00
|
|
|
return config.rhizome.enable;
|
2012-11-30 04:17:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int is_rhizome_http_enabled()
|
|
|
|
{
|
2012-12-07 03:39:55 +00:00
|
|
|
return config.rhizome.enable
|
|
|
|
&& config.rhizome.http.enable
|
2012-11-30 04:17:27 +00:00
|
|
|
&& rhizome_db;
|
|
|
|
}
|
|
|
|
|
|
|
|
int is_rhizome_mdp_enabled()
|
|
|
|
{
|
2012-12-07 03:39:55 +00:00
|
|
|
return config.rhizome.enable
|
|
|
|
&& config.rhizome.mdp.enable
|
2012-11-30 04:17:27 +00:00
|
|
|
&& rhizome_db;
|
|
|
|
}
|
|
|
|
|
|
|
|
int is_rhizome_mdp_server_running()
|
|
|
|
{
|
|
|
|
return is_rhizome_mdp_enabled();
|
|
|
|
}
|
|
|
|
|
|
|
|
int is_rhizome_advertise_enabled()
|
|
|
|
{
|
2012-12-07 03:39:55 +00:00
|
|
|
return config.rhizome.enable
|
|
|
|
&& config.rhizome.advertise.enable
|
2012-11-30 04:17:27 +00:00
|
|
|
&& rhizome_db
|
2012-12-07 03:39:55 +00:00
|
|
|
&& (is_rhizome_http_server_running() || is_rhizome_mdp_server_running());
|
2012-10-24 04:43:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int rhizome_fetch_delay_ms()
|
|
|
|
{
|
2012-12-04 03:42:28 +00:00
|
|
|
return config.rhizome.fetch_delay_ms;
|
2012-07-11 07:20:50 +00:00
|
|
|
}
|
|
|
|
|
2012-10-02 07:45:14 +00:00
|
|
|
/* Import a bundle from a pair of files, one containing the manifest and the optional other
|
|
|
|
containing the payload. The logic is all in rhizome_bundle_import(). This function just wraps
|
|
|
|
that function and manages file and object buffers and lifetimes.
|
2011-12-13 09:04:12 +00:00
|
|
|
*/
|
2012-04-02 08:12:40 +00:00
|
|
|
|
2012-12-20 04:48:59 +00:00
|
|
|
int rhizome_bundle_import_files(rhizome_manifest *m, const char *manifest_path, const char *filepath)
|
2011-12-13 09:04:12 +00:00
|
|
|
{
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.debug.rhizome)
|
2012-12-20 04:48:59 +00:00
|
|
|
DEBUGF("(manifest_path=%s, filepath=%s)",
|
2012-09-28 08:25:43 +00:00
|
|
|
manifest_path ? alloca_str_toprint(manifest_path) : "NULL",
|
2012-12-20 04:48:59 +00:00
|
|
|
filepath ? alloca_str_toprint(filepath) : "NULL");
|
|
|
|
|
2013-01-11 03:49:26 +00:00
|
|
|
unsigned char buffer[MAX_MANIFEST_BYTES];
|
|
|
|
int buffer_len=0;
|
|
|
|
|
|
|
|
// manifest has been appended to the end of the file.
|
|
|
|
if (strcmp(manifest_path, filepath)==0){
|
|
|
|
unsigned char marker[4];
|
|
|
|
int ret=0;
|
|
|
|
FILE *f = fopen(filepath, "r");
|
|
|
|
|
|
|
|
if (f == NULL)
|
|
|
|
return WHYF_perror("Could not open manifest file %s for reading.", filepath);
|
|
|
|
|
|
|
|
if (fseek(f, -sizeof(marker), SEEK_END))
|
|
|
|
ret=WHY_perror("Unable to seek to end of file");
|
|
|
|
|
|
|
|
if (ret==0){
|
|
|
|
ret = fread(marker, 1, sizeof(marker), f);
|
|
|
|
if (ret==sizeof(marker))
|
|
|
|
ret=0;
|
|
|
|
else
|
|
|
|
ret=WHY_perror("Unable to read end of manifest marker");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret==0){
|
|
|
|
if (marker[2]!=0x41 || marker[3]!=0x10)
|
|
|
|
ret=WHYF("Expected 0x4110 marker at end of file");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret==0){
|
|
|
|
buffer_len = read_uint16(marker);
|
|
|
|
if (buffer_len < 1 || buffer_len > MAX_MANIFEST_BYTES)
|
|
|
|
ret=WHYF("Invalid manifest length %d", buffer_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret==0){
|
|
|
|
if (fseek(f, -(buffer_len+sizeof(marker)), SEEK_END))
|
|
|
|
ret=WHY_perror("Unable to seek to end of file");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret==0){
|
|
|
|
ret = fread(buffer, 1, buffer_len, f);
|
|
|
|
if (ret==buffer_len)
|
|
|
|
ret=0;
|
|
|
|
else
|
|
|
|
ret=WHY_perror("Unable to read manifest contents");
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
manifest_path=(char*)buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rhizome_read_manifest_file(m, manifest_path, buffer_len) == -1)
|
2012-12-20 04:48:59 +00:00
|
|
|
return WHY("could not read manifest file");
|
|
|
|
if (rhizome_manifest_verify(m))
|
|
|
|
return WHY("could not verify manifest");
|
|
|
|
|
|
|
|
/* Make sure we store signatures */
|
|
|
|
// TODO, why do we need this? Why isn't the state correct from rhizome_read_manifest_file?
|
|
|
|
// This feels like a hack...
|
|
|
|
m->manifest_bytes=m->manifest_all_bytes;
|
|
|
|
|
|
|
|
int status = rhizome_import_file(m, filepath);
|
|
|
|
if (status<0)
|
|
|
|
return status;
|
|
|
|
|
2013-01-15 00:02:48 +00:00
|
|
|
status = rhizome_manifest_check_duplicate(m, NULL, 0);
|
|
|
|
if (status)
|
2012-12-20 04:48:59 +00:00
|
|
|
return status;
|
2013-01-15 00:02:48 +00:00
|
|
|
|
|
|
|
return rhizome_add_manifest(m, 1);
|
2012-10-02 07:45:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Import a bundle from a finalised manifest struct. The dataFileName element must give the path
|
|
|
|
of a readable file containing the payload unless the payload is null (zero length). The logic is
|
|
|
|
all in rhizome_add_manifest(). This function just wraps that function and manages object buffers
|
|
|
|
and lifetimes.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int rhizome_bundle_import(rhizome_manifest *m, int ttl)
|
|
|
|
{
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.debug.rhizome)
|
2012-10-02 07:45:14 +00:00
|
|
|
DEBUGF("(m=%p, ttl=%d)", m, ttl);
|
2013-01-15 00:02:48 +00:00
|
|
|
int ret = rhizome_manifest_check_duplicate(m, NULL, 0);
|
2012-10-02 07:45:14 +00:00
|
|
|
if (ret == 0) {
|
2012-09-28 08:25:43 +00:00
|
|
|
ret = rhizome_add_manifest(m, ttl);
|
2012-10-02 07:45:14 +00:00
|
|
|
if (ret == -1)
|
|
|
|
WHY("rhizome_add_manifest() failed");
|
2012-04-02 08:12:40 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-05-25 23:06:29 +00:00
|
|
|
int rhizome_manifest_check_sanity(rhizome_manifest *m_in)
|
2012-04-02 08:12:40 +00:00
|
|
|
{
|
2012-04-12 09:00:52 +00:00
|
|
|
/* Ensure manifest meets basic sanity checks. */
|
2012-05-17 06:39:57 +00:00
|
|
|
const char *service = rhizome_manifest_get(m_in, "service", NULL, 0);
|
2012-05-25 15:20:48 +00:00
|
|
|
const char *sender = rhizome_manifest_get(m_in, "sender", NULL, 0);
|
|
|
|
const char *recipient = rhizome_manifest_get(m_in, "recipient", NULL, 0);
|
|
|
|
|
2012-05-17 06:39:57 +00:00
|
|
|
if (service == NULL || !service[0])
|
|
|
|
return WHY("Manifest missing 'service' field");
|
2012-04-13 08:17:20 +00:00
|
|
|
if (rhizome_manifest_get_ll(m_in, "date") == -1)
|
2012-04-12 09:00:52 +00:00
|
|
|
return WHY("Manifest missing 'date' field");
|
2013-01-15 00:02:48 +00:00
|
|
|
|
|
|
|
/* Get manifest version number. */
|
|
|
|
m_in->version = rhizome_manifest_get_ll(m_in, "version");
|
|
|
|
if (m_in->version==-1)
|
|
|
|
return WHY("Manifest must have a version number");
|
|
|
|
|
2012-05-20 06:37:22 +00:00
|
|
|
if (strcasecmp(service, RHIZOME_SERVICE_FILE) == 0) {
|
2012-05-19 04:39:50 +00:00
|
|
|
const char *name = rhizome_manifest_get(m_in, "name", NULL, 0);
|
2012-06-05 06:15:53 +00:00
|
|
|
if (name == NULL)
|
2012-05-21 01:07:24 +00:00
|
|
|
return WHY("Manifest missing 'name' field");
|
2012-05-20 06:37:22 +00:00
|
|
|
} else if (strcasecmp(service, RHIZOME_SERVICE_MESHMS) == 0) {
|
2012-05-20 14:39:14 +00:00
|
|
|
if (sender == NULL || !sender[0])
|
2012-05-25 15:20:48 +00:00
|
|
|
return WHY("MeshMS Manifest missing 'sender' field");
|
2012-07-12 06:44:25 +00:00
|
|
|
if (!str_is_subscriber_id(sender))
|
|
|
|
return WHYF("MeshMS Manifest contains invalid 'sender' field: %s", sender);
|
2012-05-20 14:39:14 +00:00
|
|
|
if (recipient == NULL || !recipient[0])
|
2012-05-25 15:20:48 +00:00
|
|
|
return WHY("MeshMS Manifest missing 'recipient' field");
|
2012-07-12 06:44:25 +00:00
|
|
|
if (!str_is_subscriber_id(recipient))
|
|
|
|
return WHYF("MeshMS Manifest contains invalid 'recipient' field: %s", recipient);
|
2012-05-25 23:06:29 +00:00
|
|
|
} else {
|
|
|
|
return WHY("Invalid service type");
|
2012-05-25 15:20:48 +00:00
|
|
|
}
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.debug.rhizome)
|
2012-07-12 06:44:25 +00:00
|
|
|
DEBUGF("sender='%s'", sender ? sender : "(null)");
|
2012-05-25 15:20:48 +00:00
|
|
|
|
|
|
|
/* passes all sanity checks */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
A bundle can either be an ordinary manifest-payload pair, or a group description.
|
|
|
|
|
|
|
|
- Group descriptions are manifests with no payload that have the "isagroup" variable set. They
|
|
|
|
get stored in the manifests table AND a reference is added to the grouplist table. Any
|
|
|
|
manifest, including any group manifest, may be a member of zero or one group. This allows a
|
|
|
|
nested, i.e., multi-level group hierarchy where sub-groups will only typically be discovered
|
|
|
|
by joining the parent group.
|
|
|
|
*/
|
|
|
|
|
2012-10-09 07:13:34 +00:00
|
|
|
int rhizome_manifest_bind_id(rhizome_manifest *m_in)
|
2012-05-25 15:20:48 +00:00
|
|
|
{
|
2012-10-09 07:13:34 +00:00
|
|
|
if (rhizome_manifest_createid(m_in) == -1)
|
|
|
|
return -1;
|
2012-05-25 15:20:48 +00:00
|
|
|
/* The ID is implicit in transit, but we need to store it in the file, so that reimporting
|
|
|
|
manifests on receiver nodes works easily. We might implement something that strips the id
|
|
|
|
variable out of the manifest when sending it, or some other scheme to avoid sending all the
|
|
|
|
extra bytes. */
|
2012-10-11 07:28:24 +00:00
|
|
|
rhizome_manifest_set(m_in, "id", alloca_tohex_bid(m_in->cryptoSignPublic));
|
2012-10-09 07:13:34 +00:00
|
|
|
if (!is_sid_any(m_in->author)) {
|
2012-05-25 15:20:48 +00:00
|
|
|
/* Set the BK using the provided authorship information.
|
|
|
|
Serval Security Framework defines BK as being:
|
|
|
|
BK = privateKey XOR sha512(RS##BID), where BID = cryptoSignPublic,
|
|
|
|
and RS is the rhizome secret for the specified author.
|
|
|
|
The nice thing about this specification is that:
|
|
|
|
privateKey = BK XOR sha512(RS##BID), so the same function can be used
|
|
|
|
to encrypt and decrypt the BK field. */
|
2012-10-09 16:37:49 +00:00
|
|
|
const unsigned char *rs;
|
|
|
|
int rs_len=0;
|
2012-05-25 15:20:48 +00:00
|
|
|
unsigned char bkbytes[RHIZOME_BUNDLE_KEY_BYTES];
|
2012-10-09 16:37:49 +00:00
|
|
|
|
|
|
|
if (rhizome_find_secret(m_in->author,&rs_len,&rs)) {
|
|
|
|
return WHYF("Failed to obtain RS for %s to calculate BK",
|
|
|
|
alloca_tohex_sid(m_in->author));
|
|
|
|
}
|
|
|
|
if (!rhizome_secret2bk(m_in->cryptoSignPublic,rs,rs_len,bkbytes,m_in->cryptoSignSecret)) {
|
2012-05-25 15:20:48 +00:00
|
|
|
char bkhex[RHIZOME_BUNDLE_KEY_STRLEN + 1];
|
2012-06-08 05:57:15 +00:00
|
|
|
(void) tohex(bkhex, bkbytes, RHIZOME_BUNDLE_KEY_BYTES);
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.debug.rhizome) DEBUGF("set BK=%s", bkhex);
|
2012-05-25 15:20:48 +00:00
|
|
|
rhizome_manifest_set(m_in, "BK", bkhex);
|
|
|
|
} else {
|
|
|
|
return WHY("Failed to set BK");
|
|
|
|
}
|
2012-05-19 04:39:50 +00:00
|
|
|
}
|
2012-05-25 15:20:48 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2012-04-12 09:00:52 +00:00
|
|
|
|
2012-05-25 15:20:48 +00:00
|
|
|
/* Check if a manifest is already stored for the same payload with the same details.
|
2013-01-03 00:48:30 +00:00
|
|
|
This catches the case of "rhizome add file <filename>" on the same file more than once.
|
2012-05-25 15:20:48 +00:00
|
|
|
(Debounce!) */
|
2013-01-15 00:02:48 +00:00
|
|
|
int rhizome_manifest_check_duplicate(rhizome_manifest *m_in, rhizome_manifest **m_out, int check_author)
|
2012-05-25 15:20:48 +00:00
|
|
|
{
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.debug.rhizome) DEBUG("Checking for duplicate");
|
2012-05-25 15:20:48 +00:00
|
|
|
if (m_out) *m_out = NULL;
|
2012-04-12 09:00:52 +00:00
|
|
|
rhizome_manifest *dupm = NULL;
|
2013-01-15 00:02:48 +00:00
|
|
|
if (rhizome_find_duplicate(m_in, &dupm, check_author) == -1)
|
2012-04-12 09:00:52 +00:00
|
|
|
return WHY("Errors encountered searching for duplicate manifest");
|
|
|
|
if (dupm) {
|
2012-04-16 02:16:58 +00:00
|
|
|
/* If the caller wants the duplicate manifest, it must be finalised, otherwise discarded. */
|
2012-04-13 08:17:20 +00:00
|
|
|
if (m_out) {
|
|
|
|
*m_out = dupm;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
rhizome_manifest_free(dupm);
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.debug.rhizome) DEBUG("Found a duplicate");
|
2012-04-16 02:16:58 +00:00
|
|
|
return 2;
|
2012-04-12 09:00:52 +00:00
|
|
|
}
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.debug.rhizome) DEBUG("No duplicate found");
|
2012-05-25 15:20:48 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rhizome_add_manifest(rhizome_manifest *m_in,int ttl)
|
|
|
|
{
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.debug.rhizome)
|
2012-05-25 15:20:48 +00:00
|
|
|
DEBUGF("rhizome_add_manifest(m_in=%p, ttl=%d)",m_in, ttl);
|
|
|
|
|
|
|
|
if (m_in->finalised==0)
|
|
|
|
return WHY("Manifest must be finalised before being stored");
|
|
|
|
|
|
|
|
/* Store time to live, clamped to within legal range */
|
|
|
|
m_in->ttl = ttl < 0 ? 0 : ttl > 254 ? 254 : ttl;
|
|
|
|
|
2012-05-25 23:06:29 +00:00
|
|
|
if (rhizome_manifest_check_sanity(m_in))
|
2013-01-15 00:02:48 +00:00
|
|
|
return -1;
|
2012-05-25 15:20:48 +00:00
|
|
|
|
2012-12-20 04:48:59 +00:00
|
|
|
if (m_in->fileLength){
|
|
|
|
if (!rhizome_exists(m_in->fileHexHash))
|
|
|
|
return WHY("File has not been imported");
|
|
|
|
}
|
2012-05-25 15:20:48 +00:00
|
|
|
|
2012-04-13 08:17:20 +00:00
|
|
|
/* If the manifest already has an ID */
|
2012-05-23 06:34:00 +00:00
|
|
|
char id[SID_STRLEN + 1];
|
2013-01-15 00:02:48 +00:00
|
|
|
if (!rhizome_manifest_get(m_in, "id", id, SID_STRLEN + 1))
|
|
|
|
/* no manifest ID */
|
2012-05-25 15:20:48 +00:00
|
|
|
return WHY("Manifest does not have an ID");
|
2013-01-15 00:02:48 +00:00
|
|
|
|
|
|
|
str_toupper_inplace(id);
|
|
|
|
/* Discard the new manifest unless it is newer than the most recent known version with the same ID */
|
|
|
|
long long storedversion = -1;
|
|
|
|
switch (sqlite_exec_int64(&storedversion, "SELECT version from manifests where id='%s';", id)) {
|
|
|
|
case -1:
|
|
|
|
return WHY("Select failed");
|
|
|
|
case 0:
|
|
|
|
if (config.debug.rhizome) DEBUG("No existing manifest");
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
if (config.debug.rhizome) DEBUGF("Found existing version=%lld, new version=%lld", storedversion, m_in->version);
|
|
|
|
if (m_in->version < storedversion)
|
|
|
|
return WHY("Newer version exists");
|
|
|
|
if (m_in->version == storedversion)
|
|
|
|
return WHY("Same version of manifest exists, not adding");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return WHY("Select found too many rows!");
|
2011-12-20 00:55:52 +00:00
|
|
|
}
|
2011-12-13 09:04:12 +00:00
|
|
|
|
|
|
|
/* Okay, it is written, and can be put directly into the rhizome database now */
|
2013-01-15 00:02:48 +00:00
|
|
|
return rhizome_store_bundle(m_in);
|
2011-12-13 09:04:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Update an existing Rhizome bundle */
|
|
|
|
int rhizome_bundle_push_update(char *id,long long version,unsigned char *data,int appendP)
|
|
|
|
{
|
|
|
|
return WHY("Not implemented");
|
|
|
|
}
|
|
|
|
|
2012-05-22 03:35:29 +00:00
|
|
|
/* When voice traffic is being carried, we need to throttle Rhizome down
|
|
|
|
to a more sensible level. Or possibly even supress it entirely.
|
|
|
|
*/
|
2012-08-09 02:44:32 +00:00
|
|
|
time_ms_t rhizome_voice_timeout = -1;
|
2012-05-22 03:35:29 +00:00
|
|
|
int rhizome_saw_voice_traffic()
|
|
|
|
{
|
|
|
|
/* We are in "voice mode" for a second after sending a voice frame */
|
2012-07-27 05:29:01 +00:00
|
|
|
rhizome_voice_timeout=gettime_ms()+1000;
|
2012-05-22 03:35:29 +00:00
|
|
|
return 0;
|
|
|
|
}
|