mirror of
https://github.com/zerotier/ZeroTierOne.git
synced 2025-04-07 19:24:13 +00:00
Comments, change .nfo to .sig for uploads, clean some unused code from Utils.
This commit is contained in:
parent
9fdec3acfc
commit
9455b1cc81
@ -80,23 +80,28 @@ public:
|
||||
{
|
||||
}
|
||||
|
||||
// home of saved state, identity, etc.
|
||||
// Full path to home folder
|
||||
std::string homePath;
|
||||
|
||||
// signal() to prematurely interrupt main loop wait to cause loop to run
|
||||
// again and detect some kind of change, exit, etc.
|
||||
// Main loop waits on this condition when it delays between runs, so
|
||||
// signaling this will prematurely wake it.
|
||||
Condition mainLoopWaitCondition;
|
||||
|
||||
// This node's identity
|
||||
Identity identity;
|
||||
|
||||
// hacky... want to get rid of this flag...
|
||||
// Indicates that we are shutting down -- this is hacky, want to factor out
|
||||
volatile bool shutdownInProgress;
|
||||
|
||||
// Order matters a bit here. These are constructed in this order
|
||||
// and then deleted in the opposite order on Node exit. The order ensures
|
||||
// that things that are needed are there before they're needed.
|
||||
/*
|
||||
* Order matters a bit here. These are constructed in this order
|
||||
* and then deleted in the opposite order on Node exit. The order ensures
|
||||
* that things that are needed are there before they're needed.
|
||||
*
|
||||
* These are constant and never null after startup unless indicated.
|
||||
*/
|
||||
|
||||
Logger *log; // may be null
|
||||
Logger *log; // null if logging is disabled
|
||||
CMWC4096 *prng;
|
||||
Multicaster *mc;
|
||||
Switch *sw;
|
||||
@ -105,9 +110,9 @@ public:
|
||||
SysEnv *sysEnv;
|
||||
NodeConfig *nc;
|
||||
Node *node;
|
||||
Updater *updater; // may be null if updates are disabled
|
||||
Updater *updater; // null if auto-updates are disabled
|
||||
#ifndef __WINDOWS__
|
||||
Service *netconfService; // may be null
|
||||
Service *netconfService; // null if no netconf service running
|
||||
#endif
|
||||
};
|
||||
|
||||
|
@ -61,38 +61,38 @@ void Updater::refreshShared()
|
||||
for(std::map<std::string,bool>::iterator u(ud.begin());u!=ud.end();++u) {
|
||||
if (u->second)
|
||||
continue; // skip directories
|
||||
if ((u->first.length() >= 4)&&(!strcasecmp(u->first.substr(u->first.length() - 4).c_str(),".nfo")))
|
||||
continue; // skip .nfo companion files
|
||||
if ((u->first.length() >= 4)&&(!strcasecmp(u->first.substr(u->first.length() - 4).c_str(),".sig")))
|
||||
continue; // skip .sig companion files
|
||||
|
||||
std::string fullPath(updatesPath + ZT_PATH_SEPARATOR_S + u->first);
|
||||
std::string nfoPath(fullPath + ".nfo");
|
||||
std::string sigPath(fullPath + ".sig");
|
||||
|
||||
std::string buf;
|
||||
if (Utils::readFile(nfoPath.c_str(),buf)) {
|
||||
Dictionary nfo(buf);
|
||||
if (Utils::readFile(sigPath.c_str(),buf)) {
|
||||
Dictionary sig(buf);
|
||||
|
||||
SharedUpdate shared;
|
||||
shared.fullPath = fullPath;
|
||||
shared.filename = u->first;
|
||||
|
||||
std::string sha512(Utils::unhex(nfo.get("sha512",std::string())));
|
||||
std::string sha512(Utils::unhex(sig.get("sha512",std::string())));
|
||||
if (sha512.length() < sizeof(shared.sha512)) {
|
||||
TRACE("skipped shareable update due to missing fields in companion .nfo: %s",fullPath.c_str());
|
||||
TRACE("skipped shareable update due to missing fields in companion .sig: %s",fullPath.c_str());
|
||||
continue;
|
||||
}
|
||||
memcpy(shared.sha512,sha512.data(),sizeof(shared.sha512));
|
||||
|
||||
std::string sig(Utils::unhex(nfo.get("sha512sig_ed25519",std::string())));
|
||||
if (sig.length() < shared.sig.size()) {
|
||||
TRACE("skipped shareable update due to missing fields in companion .nfo: %s",fullPath.c_str());
|
||||
std::string signature(Utils::unhex(sig.get("sha512sig_ed25519",std::string())));
|
||||
if (signature.length() < shared.sig.size()) {
|
||||
TRACE("skipped shareable update due to missing fields in companion .sig: %s",fullPath.c_str());
|
||||
continue;
|
||||
}
|
||||
memcpy(shared.sig.data,sig.data(),shared.sig.size());
|
||||
memcpy(shared.sig.data,signature.data(),shared.sig.size());
|
||||
|
||||
// Check signature to guard against updates.d being used as a data
|
||||
// exfiltration mechanism. We will only share properly signed updates,
|
||||
// nothing else.
|
||||
Address signedBy(nfo.get("signedBy",std::string()));
|
||||
Address signedBy(sig.get("signedBy",std::string()));
|
||||
std::map< Address,Identity >::const_iterator authority(ZT_DEFAULTS.updateAuthorities.find(signedBy));
|
||||
if ((authority == ZT_DEFAULTS.updateAuthorities.end())||(!authority->second.verify(shared.sha512,64,shared.sig))) {
|
||||
TRACE("skipped shareable update: not signed by valid authority or signature invalid: %s",fullPath.c_str());
|
||||
@ -110,7 +110,7 @@ void Updater::refreshShared()
|
||||
LOG("sharing software update %s to other peers",shared.filename.c_str());
|
||||
_sharedUpdates.push_back(shared);
|
||||
} else {
|
||||
TRACE("skipped shareable update due to missing companion .nfo: %s",fullPath.c_str());
|
||||
TRACE("skipped shareable update due to missing companion .sig: %s",fullPath.c_str());
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
@ -50,9 +50,6 @@ namespace ZeroTier {
|
||||
|
||||
const char Utils::HEXCHARS[16] = { '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f' };
|
||||
|
||||
static const char *DAY_NAMES[7] = { "Sun","Mon","Tue","Wed","Thu","Fri","Sat" };
|
||||
static const char *MONTH_NAMES[12] = { "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };
|
||||
|
||||
std::map<std::string,bool> Utils::listDirectory(const char *path)
|
||||
{
|
||||
std::map<std::string,bool> r;
|
||||
@ -62,7 +59,8 @@ std::map<std::string,bool> Utils::listDirectory(const char *path)
|
||||
WIN32_FIND_DATAA ffd;
|
||||
if ((hFind = FindFirstFileA((std::string(path) + "\\*").c_str(),&ffd)) != INVALID_HANDLE_VALUE) {
|
||||
do {
|
||||
r[std::string(ffd.cFileName)] = ((ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0);
|
||||
if ((strcmp(ffd.cFileName,"."))&&(strcmp(ffd.cFileName,"..")))
|
||||
r[std::string(ffd.cFileName)] = ((ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0);
|
||||
} while (FindNextFileA(hFind,&ffd));
|
||||
FindClose(hFind);
|
||||
}
|
||||
@ -275,94 +273,6 @@ int64_t Utils::getFileSize(const char *path)
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::string Utils::toRfc1123(uint64_t t64)
|
||||
{
|
||||
struct tm t;
|
||||
char buf[128];
|
||||
time_t utc = (time_t)(t64 / 1000ULL);
|
||||
#ifdef __WINDOWS__
|
||||
gmtime_s(&t,&utc);
|
||||
#else
|
||||
gmtime_r(&utc,&t);
|
||||
#endif
|
||||
Utils::snprintf(buf,sizeof(buf),"%3s, %02d %3s %4d %02d:%02d:%02d GMT",DAY_NAMES[t.tm_wday],t.tm_mday,MONTH_NAMES[t.tm_mon],t.tm_year + 1900,t.tm_hour,t.tm_min,t.tm_sec);
|
||||
return std::string(buf);
|
||||
}
|
||||
|
||||
#ifdef __WINDOWS__
|
||||
static int is_leap(unsigned y) {
|
||||
y += 1900;
|
||||
return (y % 4) == 0 && ((y % 100) != 0 || (y % 400) == 0);
|
||||
}
|
||||
static time_t timegm(struct tm *tm) {
|
||||
static const unsigned ndays[2][12] = {
|
||||
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
|
||||
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
|
||||
};
|
||||
time_t res = 0;
|
||||
int i;
|
||||
for (i = 70; i < tm->tm_year; ++i)
|
||||
res += is_leap(i) ? 366 : 365;
|
||||
|
||||
for (i = 0; i < tm->tm_mon; ++i)
|
||||
res += ndays[is_leap(tm->tm_year)][i];
|
||||
res += tm->tm_mday - 1;
|
||||
res *= 24;
|
||||
res += tm->tm_hour;
|
||||
res *= 60;
|
||||
res += tm->tm_min;
|
||||
res *= 60;
|
||||
res += tm->tm_sec;
|
||||
return res;
|
||||
}
|
||||
#endif
|
||||
|
||||
uint64_t Utils::fromRfc1123(const char *tstr)
|
||||
{
|
||||
struct tm t;
|
||||
char wdays[128],mons[128];
|
||||
|
||||
int l = (int)strlen(tstr);
|
||||
if ((l < 29)||(l > 64))
|
||||
return 0;
|
||||
int assigned = sscanf(tstr,"%3s, %02d %3s %4d %02d:%02d:%02d GMT",wdays,&t.tm_mday,mons,&t.tm_year,&t.tm_hour,&t.tm_min,&t.tm_sec);
|
||||
if (assigned != 7)
|
||||
return 0;
|
||||
|
||||
wdays[3] = '\0';
|
||||
for(t.tm_wday=0;t.tm_wday<7;++t.tm_wday) {
|
||||
#ifdef __WINDOWS__
|
||||
if (!_stricmp(DAY_NAMES[t.tm_wday],wdays))
|
||||
break;
|
||||
#else
|
||||
if (!strcasecmp(DAY_NAMES[t.tm_wday],wdays))
|
||||
break;
|
||||
#endif
|
||||
}
|
||||
if (t.tm_wday == 7)
|
||||
return 0;
|
||||
mons[3] = '\0';
|
||||
for(t.tm_mon=0;t.tm_mon<12;++t.tm_mon) {
|
||||
#ifdef __WINDOWS__
|
||||
if (!_stricmp(MONTH_NAMES[t.tm_mday],mons))
|
||||
break;
|
||||
#else
|
||||
if (!strcasecmp(MONTH_NAMES[t.tm_mday],mons))
|
||||
break;
|
||||
#endif
|
||||
}
|
||||
if (t.tm_mon == 12)
|
||||
return 0;
|
||||
|
||||
t.tm_wday = 0; // ignored by timegm
|
||||
t.tm_yday = 0; // ignored by timegm
|
||||
t.tm_isdst = 0; // ignored by timegm
|
||||
|
||||
time_t utc = timegm(&t);
|
||||
|
||||
return ((utc > 0) ? (1000ULL * (uint64_t)utc) : 0ULL);
|
||||
}
|
||||
|
||||
bool Utils::readFile(const char *path,std::string &buf)
|
||||
{
|
||||
char tmp[4096];
|
||||
|
199
node/Utils.hpp
199
node/Utils.hpp
@ -41,9 +41,6 @@
|
||||
|
||||
#include "Constants.hpp"
|
||||
|
||||
#include "../ext/lz4/lz4.h"
|
||||
#include "../ext/lz4/lz4hc.h"
|
||||
|
||||
#ifdef __WINDOWS__
|
||||
#include <WinSock2.h>
|
||||
#include <Windows.h>
|
||||
@ -53,11 +50,6 @@
|
||||
#include <arpa/inet.h>
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Maximum compression/decompression block size (do not change)
|
||||
*/
|
||||
#define ZT_COMPRESSION_BLOCK_SIZE 16777216
|
||||
|
||||
namespace ZeroTier {
|
||||
|
||||
/**
|
||||
@ -108,14 +100,13 @@ public:
|
||||
return (unlink(path) == 0);
|
||||
#endif
|
||||
}
|
||||
static inline bool rm(const std::string &path)
|
||||
throw()
|
||||
{
|
||||
return rm(path.c_str());
|
||||
}
|
||||
static inline bool rm(const std::string &path) throw() { return rm(path.c_str()); }
|
||||
|
||||
/**
|
||||
* List a directory's contents
|
||||
*
|
||||
* Keys in returned map are filenames only and don't include the leading
|
||||
* path. Pseudo-paths like . and .. are not returned.
|
||||
*
|
||||
* @param path Path to list
|
||||
* @return Map of entries and whether or not they are also directories (empty on failure)
|
||||
@ -199,187 +190,6 @@ public:
|
||||
*/
|
||||
static int64_t getFileSize(const char *path);
|
||||
|
||||
/**
|
||||
* @param t64 Time in ms since epoch
|
||||
* @return RFC1123 date string
|
||||
*/
|
||||
static std::string toRfc1123(uint64_t t64);
|
||||
|
||||
/**
|
||||
* @param tstr Time in RFC1123 string format
|
||||
* @return Time in ms since epoch
|
||||
*/
|
||||
static uint64_t fromRfc1123(const char *tstr);
|
||||
static inline uint64_t fromRfc1123(const std::string &tstr) { return fromRfc1123(tstr.c_str()); }
|
||||
|
||||
/**
|
||||
* String append output function object for use with compress/decompress
|
||||
*/
|
||||
class StringAppendOutput
|
||||
{
|
||||
public:
|
||||
StringAppendOutput(std::string &s) : _s(s) {}
|
||||
inline void operator()(const void *data,unsigned int len) { _s.append((const char *)data,len); }
|
||||
private:
|
||||
std::string &_s;
|
||||
};
|
||||
|
||||
/**
|
||||
* STDIO FILE append output function object for compress/decompress
|
||||
*
|
||||
* Throws std::runtime_error on write error.
|
||||
*/
|
||||
class FILEAppendOutput
|
||||
{
|
||||
public:
|
||||
FILEAppendOutput(FILE *f) : _f(f) {}
|
||||
inline void operator()(const void *data,unsigned int len)
|
||||
throw(std::runtime_error)
|
||||
{
|
||||
if ((int)fwrite(data,1,len,_f) != (int)len)
|
||||
throw std::runtime_error("write failed");
|
||||
}
|
||||
private:
|
||||
FILE *_f;
|
||||
};
|
||||
|
||||
/**
|
||||
* Compress data
|
||||
*
|
||||
* O must be a function or function object that takes the following
|
||||
* arguments: (const void *data,unsigned int len)
|
||||
*
|
||||
* @param in Input iterator that reads bytes (char, uint8_t, etc.)
|
||||
* @param out Output iterator that writes bytes
|
||||
*/
|
||||
template<typename I,typename O>
|
||||
static inline void compress(I begin,I end,O out)
|
||||
{
|
||||
unsigned int bufLen = LZ4_compressBound(ZT_COMPRESSION_BLOCK_SIZE);
|
||||
char *buf = new char[bufLen * 2];
|
||||
char *buf2 = buf + bufLen;
|
||||
|
||||
try {
|
||||
I inp(begin);
|
||||
for(;;) {
|
||||
unsigned int readLen = 0;
|
||||
while ((readLen < ZT_COMPRESSION_BLOCK_SIZE)&&(inp != end)) {
|
||||
buf[readLen++] = (char)*inp;
|
||||
++inp;
|
||||
}
|
||||
if (!readLen)
|
||||
break;
|
||||
|
||||
uint32_t l = hton((uint32_t)readLen);
|
||||
out((const void *)&l,4); // original size
|
||||
|
||||
if (readLen < 32) { // don't bother compressing itty bitty blocks
|
||||
l = 0; // stored
|
||||
out((const void *)&l,4);
|
||||
out((const void *)buf,readLen);
|
||||
continue;
|
||||
}
|
||||
|
||||
int lz4CompressedLen = LZ4_compressHC(buf,buf2,(int)readLen);
|
||||
if ((lz4CompressedLen <= 0)||(lz4CompressedLen >= (int)readLen)) {
|
||||
l = 0; // stored
|
||||
out((const void *)&l,4);
|
||||
out((const void *)buf,readLen);
|
||||
continue;
|
||||
}
|
||||
|
||||
l = hton((uint32_t)lz4CompressedLen); // lz4 only
|
||||
out((const void *)&l,4);
|
||||
out((const void *)buf2,(unsigned int)lz4CompressedLen);
|
||||
}
|
||||
|
||||
delete [] buf;
|
||||
} catch ( ... ) {
|
||||
delete [] buf;
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Decompress data
|
||||
*
|
||||
* O must be a function or function object that takes the following
|
||||
* arguments: (const void *data,unsigned int len)
|
||||
*
|
||||
* @param in Input iterator that reads bytes (char, uint8_t, etc.)
|
||||
* @param out Output iterator that writes bytes
|
||||
* @return False on decompression error
|
||||
*/
|
||||
template<typename I,typename O>
|
||||
static inline bool decompress(I begin,I end,O out)
|
||||
{
|
||||
volatile char i32c[4];
|
||||
void *const i32cp = (void *)i32c;
|
||||
unsigned int bufLen = LZ4_compressBound(ZT_COMPRESSION_BLOCK_SIZE);
|
||||
char *buf = new char[bufLen * 2];
|
||||
char *buf2 = buf + bufLen;
|
||||
|
||||
try {
|
||||
I inp(begin);
|
||||
while (inp != end) {
|
||||
i32c[0] = (char)*inp; if (++inp == end) { delete [] buf; return false; }
|
||||
i32c[1] = (char)*inp; if (++inp == end) { delete [] buf; return false; }
|
||||
i32c[2] = (char)*inp; if (++inp == end) { delete [] buf; return false; }
|
||||
i32c[3] = (char)*inp; if (++inp == end) { delete [] buf; return false; }
|
||||
unsigned int originalSize = ntoh(*((const uint32_t *)i32cp));
|
||||
i32c[0] = (char)*inp; if (++inp == end) { delete [] buf; return false; }
|
||||
i32c[1] = (char)*inp; if (++inp == end) { delete [] buf; return false; }
|
||||
i32c[2] = (char)*inp; if (++inp == end) { delete [] buf; return false; }
|
||||
i32c[3] = (char)*inp; if (++inp == end) { delete [] buf; return false; }
|
||||
uint32_t _compressedSize = ntoh(*((const uint32_t *)i32cp));
|
||||
unsigned int compressedSize = _compressedSize & 0x7fffffff;
|
||||
|
||||
if (compressedSize) {
|
||||
if (compressedSize > bufLen) {
|
||||
delete [] buf;
|
||||
return false;
|
||||
}
|
||||
unsigned int readLen = 0;
|
||||
while ((readLen < compressedSize)&&(inp != end)) {
|
||||
buf[readLen++] = (char)*inp;
|
||||
++inp;
|
||||
}
|
||||
if (readLen != compressedSize) {
|
||||
delete [] buf;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (LZ4_uncompress_unknownOutputSize(buf,buf2,compressedSize,bufLen) != (int)originalSize) {
|
||||
delete [] buf;
|
||||
return false;
|
||||
} else out((const void *)buf2,(unsigned int)originalSize);
|
||||
} else { // stored
|
||||
if (originalSize > bufLen) {
|
||||
delete [] buf;
|
||||
return false;
|
||||
}
|
||||
unsigned int readLen = 0;
|
||||
while ((readLen < originalSize)&&(inp != end)) {
|
||||
buf[readLen++] = (char)*inp;
|
||||
++inp;
|
||||
}
|
||||
if (readLen != originalSize) {
|
||||
delete [] buf;
|
||||
return false;
|
||||
}
|
||||
|
||||
out((const void *)buf,(unsigned int)originalSize);
|
||||
}
|
||||
}
|
||||
|
||||
delete [] buf;
|
||||
return true;
|
||||
} catch ( ... ) {
|
||||
delete [] buf;
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Current time in milliseconds since epoch
|
||||
*/
|
||||
@ -682,6 +492,7 @@ public:
|
||||
return ((*aptr & mask) == (*aptr & mask));
|
||||
}
|
||||
|
||||
// Byte swappers for big/little endian conversion
|
||||
static inline uint8_t hton(uint8_t n) throw() { return n; }
|
||||
static inline int8_t hton(int8_t n) throw() { return n; }
|
||||
static inline uint16_t hton(uint16_t n) throw() { return htons(n); }
|
||||
|
Loading…
x
Reference in New Issue
Block a user