2011-12-21 09:55:05 +00:00
|
|
|
/*
|
2013-11-21 02:12:59 +00:00
|
|
|
Serval DNA Rhizome file distribution
|
2013-12-04 06:26:55 +00:00
|
|
|
Copyright (C) 2012-2013 Serval Project Inc.
|
2013-11-21 02:12:59 +00:00
|
|
|
Copyright (C) 2011-2012 Paul Gardner-Stephen
|
2011-12-21 09:55:05 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2013-12-07 17:38:14 +00:00
|
|
|
/*
|
|
|
|
Portions Copyright (C) 2013 Petter Reinholdtsen
|
|
|
|
Some rights reserved
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
1. Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the
|
|
|
|
distribution.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <assert.h>
|
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"
|
2013-11-25 02:39:54 +00:00
|
|
|
#include "dataformats.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-10-10 07:53:06 +00:00
|
|
|
size_t buffer_len = 0;
|
2013-12-19 08:37:14 +00:00
|
|
|
int ret = 0;
|
2013-01-11 03:49:26 +00:00
|
|
|
|
|
|
|
// manifest has been appended to the end of the file.
|
|
|
|
if (strcmp(manifest_path, filepath)==0){
|
|
|
|
unsigned char marker[4];
|
|
|
|
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)
|
2013-10-13 19:09:23 +00:00
|
|
|
ret=WHYF("Invalid manifest length %zu", buffer_len);
|
2013-01-11 03:49:26 +00:00
|
|
|
}
|
|
|
|
if (ret==0){
|
|
|
|
if (fseek(f, -(buffer_len+sizeof(marker)), SEEK_END))
|
|
|
|
ret=WHY_perror("Unable to seek to end of file");
|
|
|
|
}
|
2013-12-19 08:37:14 +00:00
|
|
|
if (ret == 0 && fread(m->manifestdata, buffer_len, 1, f) != 1) {
|
|
|
|
if (ferror(f))
|
|
|
|
ret = WHYF("fread(%p,%zu,1,%s) error", m->manifestdata, buffer_len, alloca_str_toprint(filepath));
|
|
|
|
else if (feof(f))
|
|
|
|
ret = WHYF("fread(%p,%zu,1,%s) hit end of file", m->manifestdata, buffer_len, alloca_str_toprint(filepath));
|
2013-01-11 03:49:26 +00:00
|
|
|
}
|
|
|
|
fclose(f);
|
2013-12-19 08:37:14 +00:00
|
|
|
} else {
|
|
|
|
ssize_t size = read_whole_file(manifest_path, m->manifestdata, sizeof m->manifestdata);
|
|
|
|
if (size == -1)
|
|
|
|
ret = -1;
|
|
|
|
buffer_len = (size_t) size;
|
2013-01-11 03:49:26 +00:00
|
|
|
}
|
2013-12-19 08:37:14 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
m->manifest_all_bytes = buffer_len;
|
|
|
|
if (rhizome_manifest_parse(m) == -1)
|
|
|
|
return WHY("could not parse manifest file");
|
2013-11-28 07:14:37 +00:00
|
|
|
if (!rhizome_manifest_validate(m))
|
|
|
|
return WHY("manifest is invalid");
|
|
|
|
if (!rhizome_manifest_verify(m))
|
2012-12-20 04:48:59 +00:00
|
|
|
return WHY("could not verify manifest");
|
|
|
|
|
2013-08-01 02:07:35 +00:00
|
|
|
/* Do we already have this manifest or newer? */
|
2013-12-11 00:41:34 +00:00
|
|
|
uint64_t dbVersion = 0;
|
|
|
|
if (sqlite_exec_uint64(&dbVersion, "SELECT version FROM MANIFESTS WHERE id = ?;", RHIZOME_BID_T, &m->cryptoSignPublic, END) == -1)
|
2013-08-01 02:07:35 +00:00
|
|
|
return WHY("Select failure");
|
|
|
|
|
2013-12-11 00:41:34 +00:00
|
|
|
if (dbVersion >= m->version)
|
2013-08-01 02:07:35 +00:00
|
|
|
return 2;
|
|
|
|
|
2012-12-20 04:48:59 +00:00
|
|
|
int status = rhizome_import_file(m, filepath);
|
|
|
|
if (status<0)
|
|
|
|
return status;
|
|
|
|
|
2013-01-15 00:02:48 +00:00
|
|
|
return rhizome_add_manifest(m, 1);
|
2012-10-02 07:45:14 +00:00
|
|
|
}
|
|
|
|
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
int rhizome_manifest_check_sanity(rhizome_manifest *m)
|
2012-04-02 08:12:40 +00:00
|
|
|
{
|
2012-04-12 09:00:52 +00:00
|
|
|
/* Ensure manifest meets basic sanity checks. */
|
2013-11-26 07:16:07 +00:00
|
|
|
int ret = 0;
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
if (m->version == 0)
|
2013-11-26 07:16:07 +00:00
|
|
|
ret = WHY("Manifest must have a version number");
|
|
|
|
if (m->filesize == RHIZOME_SIZE_UNSET)
|
|
|
|
ret = WHY("Manifest missing 'filesize' field");
|
|
|
|
else if (m->filesize && rhizome_filehash_t_is_zero(m->filehash))
|
|
|
|
ret = WHY("Manifest 'filehash' field has not been set");
|
|
|
|
if (m->service == NULL)
|
|
|
|
ret = WHY("Manifest missing 'service' field");
|
|
|
|
else if (strcasecmp(m->service, RHIZOME_SERVICE_FILE) == 0) {
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
if (m->name == NULL)
|
2013-11-26 07:16:07 +00:00
|
|
|
ret = WHY("Manifest with service='" RHIZOME_SERVICE_FILE "' missing 'name' field");
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
} else if (strcasecmp(m->service, RHIZOME_SERVICE_MESHMS) == 0
|
|
|
|
|| strcasecmp(m->service, RHIZOME_SERVICE_MESHMS2) == 0) {
|
|
|
|
if (!m->has_sender)
|
2013-11-26 07:16:07 +00:00
|
|
|
ret = WHYF("Manifest with service='%s' missing 'sender' field", m->service);
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
if (!m->has_recipient)
|
2013-11-26 07:16:07 +00:00
|
|
|
ret = WHYF("Manifest with service='%s' missing 'recipient' field", m->service);
|
2012-05-25 15:20:48 +00:00
|
|
|
}
|
2013-11-26 07:16:07 +00:00
|
|
|
else if (!rhizome_str_is_manifest_service(m->service))
|
|
|
|
ret = WHYF("Manifest invalid 'service' field %s", alloca_str_toprint(m->service));
|
|
|
|
if (!m->has_date)
|
|
|
|
ret = WHY("Manifest missing 'date' field");
|
|
|
|
return ret;
|
2012-05-25 15:20:48 +00:00
|
|
|
}
|
|
|
|
|
2013-11-05 07:28:03 +00:00
|
|
|
/* Sets the bundle key "BK" field of a manifest. Returns 1 if the field was set, 0 if not.
|
|
|
|
*
|
|
|
|
* This function must not be called unless the bundle secret is known.
|
|
|
|
*
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
|
|
|
int rhizome_manifest_add_bundle_key(rhizome_manifest *m)
|
2012-05-25 15:20:48 +00:00
|
|
|
{
|
2013-11-05 07:28:03 +00:00
|
|
|
IN();
|
|
|
|
assert(m->haveSecret);
|
|
|
|
switch (m->authorship) {
|
|
|
|
case ANONYMOUS: // there can be no BK field without an author
|
|
|
|
case AUTHOR_UNKNOWN: // we already know the author is not in the keyring
|
|
|
|
case AUTHENTICATION_ERROR: // already tried and failed to get Rhizome Secret
|
|
|
|
break;
|
|
|
|
case AUTHOR_NOT_CHECKED:
|
|
|
|
case AUTHOR_LOCAL:
|
|
|
|
case AUTHOR_AUTHENTIC:
|
|
|
|
case AUTHOR_IMPOSTOR: {
|
|
|
|
/* Set the BK using the provided author. Serval Security Framework defines BK as being:
|
|
|
|
* BK = privateKey XOR sha512(RS##BID)
|
|
|
|
* where BID = cryptoSignPublic,
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
const unsigned char *rs;
|
|
|
|
size_t rs_len = 0;
|
|
|
|
enum rhizome_secret_disposition d = find_rhizome_secret(&m->author, &rs_len, &rs);
|
|
|
|
switch (d) {
|
|
|
|
case FOUND_RHIZOME_SECRET: {
|
|
|
|
rhizome_bk_t bkey;
|
|
|
|
if (rhizome_secret2bk(&m->cryptoSignPublic, rs, rs_len, bkey.binary, m->cryptoSignSecret) == 0) {
|
|
|
|
rhizome_manifest_set_bundle_key(m, &bkey);
|
|
|
|
m->authorship = AUTHOR_AUTHENTIC;
|
|
|
|
RETURN(1);
|
|
|
|
} else
|
|
|
|
m->authorship = AUTHENTICATION_ERROR;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IDENTITY_NOT_FOUND:
|
|
|
|
m->authorship = AUTHOR_UNKNOWN;
|
|
|
|
break;
|
|
|
|
case IDENTITY_HAS_NO_RHIZOME_SECRET:
|
|
|
|
m->authorship = AUTHENTICATION_ERROR;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
FATALF("find_rhizome_secret() returned unknown code %d", (int)d);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
FATALF("m->authorship = %d", (int)m->authorship);
|
2012-05-19 04:39:50 +00:00
|
|
|
}
|
2013-11-05 07:28:03 +00:00
|
|
|
rhizome_manifest_del_bundle_key(m);
|
|
|
|
switch (m->authorship) {
|
|
|
|
case AUTHOR_UNKNOWN:
|
|
|
|
WHYF("Cannot set BK because author=%s is not in keyring", alloca_tohex_sid_t(m->author));
|
|
|
|
break;
|
|
|
|
case AUTHENTICATION_ERROR:
|
|
|
|
WHY("Cannot set BK due to error");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
RETURN(0);
|
2012-05-25 15:20:48 +00:00
|
|
|
}
|
2012-04-12 09:00:52 +00:00
|
|
|
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
int rhizome_add_manifest(rhizome_manifest *m, int ttl)
|
2012-05-25 15:20:48 +00:00
|
|
|
{
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.debug.rhizome)
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
DEBUGF("rhizome_add_manifest(m=%p, ttl=%d)",m, ttl);
|
2012-05-25 15:20:48 +00:00
|
|
|
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
if (m->finalised==0)
|
2012-05-25 15:20:48 +00:00
|
|
|
return WHY("Manifest must be finalised before being stored");
|
|
|
|
|
|
|
|
/* Store time to live, clamped to within legal range */
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
m->ttl = ttl < 0 ? 0 : ttl > 254 ? 254 : ttl;
|
2012-05-25 15:20:48 +00:00
|
|
|
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
if (rhizome_manifest_check_sanity(m))
|
2013-01-15 00:02:48 +00:00
|
|
|
return -1;
|
2012-05-25 15:20:48 +00:00
|
|
|
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
assert(m->filesize != RHIZOME_SIZE_UNSET);
|
|
|
|
if (m->filesize > 0 && !rhizome_exists(&m->filehash))
|
2013-10-10 07:53:06 +00:00
|
|
|
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 */
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
if (rhizome_bid_t_is_zero(m->cryptoSignPublic))
|
2012-05-25 15:20:48 +00:00
|
|
|
return WHY("Manifest does not have an ID");
|
2013-01-15 00:02:48 +00:00
|
|
|
|
|
|
|
/* Discard the new manifest unless it is newer than the most recent known version with the same ID */
|
2013-12-11 00:41:34 +00:00
|
|
|
uint64_t storedversion = -1;
|
|
|
|
switch (sqlite_exec_uint64(&storedversion, "SELECT version FROM MANIFESTS WHERE id = ?;", RHIZOME_BID_T, &m->cryptoSignPublic, END)) {
|
2013-01-15 00:02:48 +00:00
|
|
|
case -1:
|
|
|
|
return WHY("Select failed");
|
|
|
|
case 0:
|
|
|
|
if (config.debug.rhizome) DEBUG("No existing manifest");
|
|
|
|
break;
|
|
|
|
case 1:
|
2013-07-15 00:29:24 +00:00
|
|
|
if (config.debug.rhizome)
|
2013-12-11 00:41:34 +00:00
|
|
|
DEBUGF("Found existing version=%"PRIu64", new version=%"PRIu64, storedversion, m->version);
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
if (m->version < storedversion)
|
2013-01-15 00:02:48 +00:00
|
|
|
return WHY("Newer version exists");
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
if (m->version == storedversion)
|
2013-12-11 00:41:34 +00:00
|
|
|
return WHYF("Already have %s:%"PRIu64", not adding", alloca_tohex_rhizome_bid_t(m->cryptoSignPublic), m->version);
|
2013-01-15 00:02:48 +00:00
|
|
|
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 */
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
return rhizome_store_bundle(m);
|
2011-12-13 09:04:12 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|