2013-07-04 20:56:19 +00:00
|
|
|
/*
|
|
|
|
* ZeroTier One - Global Peer to Peer Ethernet
|
2014-02-16 20:40:22 +00:00
|
|
|
* Copyright (C) 2011-2014 ZeroTier Networks LLC
|
2013-07-04 20:56:19 +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 3 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* --
|
|
|
|
*
|
|
|
|
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
|
|
|
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
|
|
|
*
|
|
|
|
* If you would like to embed ZeroTier into a commercial application or
|
|
|
|
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
|
|
|
* LLC. Start here: http://www.zerotier.com/
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2013-07-17 18:10:44 +00:00
|
|
|
#include <stdarg.h>
|
2013-08-13 01:25:36 +00:00
|
|
|
#include <sys/stat.h>
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2013-08-10 14:12:16 +00:00
|
|
|
#include "Constants.hpp"
|
|
|
|
|
|
|
|
#ifdef __UNIX_LIKE__
|
2013-07-04 20:56:19 +00:00
|
|
|
#include <unistd.h>
|
2013-08-10 14:12:16 +00:00
|
|
|
#include <errno.h>
|
2013-07-04 20:56:19 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/types.h>
|
2014-08-07 13:35:54 +00:00
|
|
|
#include <sys/stat.h>
|
2013-08-10 14:12:16 +00:00
|
|
|
#include <sys/uio.h>
|
2013-07-25 21:53:57 +00:00
|
|
|
#include <dirent.h>
|
2013-07-04 20:56:19 +00:00
|
|
|
#endif
|
|
|
|
|
2014-08-08 18:53:55 +00:00
|
|
|
#ifdef __WINDOWS__
|
|
|
|
#include <wincrypt.h>
|
|
|
|
#endif
|
|
|
|
|
2013-07-25 21:53:57 +00:00
|
|
|
#include "Utils.hpp"
|
|
|
|
#include "Mutex.hpp"
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
namespace ZeroTier {
|
|
|
|
|
|
|
|
const char Utils::HEXCHARS[16] = { '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f' };
|
|
|
|
|
2014-08-07 13:36:46 +00:00
|
|
|
#ifdef __UNIX_LIKE__
|
2014-08-07 13:35:54 +00:00
|
|
|
bool Utils::redirectUnixOutputs(const char *stdoutPath,const char *stderrPath)
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
int fdout = ::open(stdoutPath,O_WRONLY|O_CREAT);
|
|
|
|
if (fdout > 0) {
|
|
|
|
int fderr;
|
|
|
|
if (stderrPath) {
|
|
|
|
fderr = ::open(stderrPath,O_WRONLY|O_CREAT);
|
|
|
|
if (fderr <= 0) {
|
|
|
|
::close(fdout);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else fderr = fdout;
|
|
|
|
::close(STDOUT_FILENO);
|
|
|
|
::close(STDERR_FILENO);
|
|
|
|
::dup2(fdout,STDOUT_FILENO);
|
|
|
|
::dup2(fderr,STDERR_FILENO);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2014-08-07 13:36:46 +00:00
|
|
|
#endif // __UNIX_LIKE__
|
2014-08-07 13:35:54 +00:00
|
|
|
|
2013-07-25 21:53:57 +00:00
|
|
|
std::map<std::string,bool> Utils::listDirectory(const char *path)
|
|
|
|
{
|
2013-08-12 20:57:34 +00:00
|
|
|
std::map<std::string,bool> r;
|
|
|
|
|
|
|
|
#ifdef __WINDOWS__
|
|
|
|
HANDLE hFind;
|
2013-08-14 15:19:21 +00:00
|
|
|
WIN32_FIND_DATAA ffd;
|
|
|
|
if ((hFind = FindFirstFileA((std::string(path) + "\\*").c_str(),&ffd)) != INVALID_HANDLE_VALUE) {
|
2013-08-12 20:57:34 +00:00
|
|
|
do {
|
2013-11-06 15:38:19 +00:00
|
|
|
if ((strcmp(ffd.cFileName,"."))&&(strcmp(ffd.cFileName,"..")))
|
|
|
|
r[std::string(ffd.cFileName)] = ((ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0);
|
2013-08-14 15:19:21 +00:00
|
|
|
} while (FindNextFileA(hFind,&ffd));
|
2013-08-12 20:57:34 +00:00
|
|
|
FindClose(hFind);
|
|
|
|
}
|
|
|
|
#else
|
2013-07-25 21:53:57 +00:00
|
|
|
struct dirent de;
|
|
|
|
struct dirent *dptr;
|
|
|
|
|
|
|
|
DIR *d = opendir(path);
|
|
|
|
if (!d)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
dptr = (struct dirent *)0;
|
|
|
|
for(;;) {
|
|
|
|
if (readdir_r(d,&de,&dptr))
|
|
|
|
break;
|
|
|
|
if (dptr) {
|
2013-08-06 05:28:56 +00:00
|
|
|
if ((strcmp(dptr->d_name,"."))&&(strcmp(dptr->d_name,"..")))
|
2013-07-25 21:53:57 +00:00
|
|
|
r[std::string(dptr->d_name)] = (dptr->d_type == DT_DIR);
|
|
|
|
} else break;
|
|
|
|
}
|
2013-08-12 20:57:34 +00:00
|
|
|
#endif
|
2013-07-25 21:53:57 +00:00
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
std::string Utils::hex(const void *data,unsigned int len)
|
|
|
|
{
|
|
|
|
std::string r;
|
|
|
|
r.reserve(len * 2);
|
|
|
|
for(unsigned int i=0;i<len;++i) {
|
|
|
|
r.push_back(HEXCHARS[(((const unsigned char *)data)[i] & 0xf0) >> 4]);
|
|
|
|
r.push_back(HEXCHARS[((const unsigned char *)data)[i] & 0x0f]);
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-08-05 16:56:49 +00:00
|
|
|
std::string Utils::unhex(const char *hex,unsigned int maxlen)
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
|
|
|
int n = 1;
|
|
|
|
unsigned char c,b = 0;
|
2014-08-05 16:56:49 +00:00
|
|
|
const char *eof = hex + maxlen;
|
2013-07-04 20:56:19 +00:00
|
|
|
std::string r;
|
|
|
|
|
2014-08-05 16:56:49 +00:00
|
|
|
if (!maxlen)
|
|
|
|
return r;
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
while ((c = (unsigned char)*(hex++))) {
|
|
|
|
if ((c >= 48)&&(c <= 57)) { // 0..9
|
|
|
|
if ((n ^= 1))
|
|
|
|
r.push_back((char)(b | (c - 48)));
|
|
|
|
else b = (c - 48) << 4;
|
|
|
|
} else if ((c >= 65)&&(c <= 70)) { // A..F
|
|
|
|
if ((n ^= 1))
|
|
|
|
r.push_back((char)(b | (c - (65 - 10))));
|
|
|
|
else b = (c - (65 - 10)) << 4;
|
|
|
|
} else if ((c >= 97)&&(c <= 102)) { // a..f
|
|
|
|
if ((n ^= 1))
|
|
|
|
r.push_back((char)(b | (c - (97 - 10))));
|
|
|
|
else b = (c - (97 - 10)) << 4;
|
|
|
|
}
|
2014-08-05 16:56:49 +00:00
|
|
|
if (hex == eof)
|
|
|
|
break;
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-08-05 16:56:49 +00:00
|
|
|
unsigned int Utils::unhex(const char *hex,unsigned int maxlen,void *buf,unsigned int len)
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
|
|
|
int n = 1;
|
|
|
|
unsigned char c,b = 0;
|
|
|
|
unsigned int l = 0;
|
2014-08-05 16:56:49 +00:00
|
|
|
const char *eof = hex + maxlen;
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2014-08-05 16:56:49 +00:00
|
|
|
if (!maxlen)
|
|
|
|
return 0;
|
2013-10-04 16:24:21 +00:00
|
|
|
|
2014-08-05 16:56:49 +00:00
|
|
|
while ((c = (unsigned char)*(hex++))) {
|
2013-10-04 16:24:21 +00:00
|
|
|
if ((c >= 48)&&(c <= 57)) { // 0..9
|
2013-07-04 20:56:19 +00:00
|
|
|
if ((n ^= 1)) {
|
|
|
|
if (l >= len) break;
|
|
|
|
((unsigned char *)buf)[l++] = (b | (c - 48));
|
|
|
|
} else b = (c - 48) << 4;
|
|
|
|
} else if ((c >= 65)&&(c <= 70)) { // A..F
|
|
|
|
if ((n ^= 1)) {
|
|
|
|
if (l >= len) break;
|
|
|
|
((unsigned char *)buf)[l++] = (b | (c - (65 - 10)));
|
|
|
|
} else b = (c - (65 - 10)) << 4;
|
|
|
|
} else if ((c >= 97)&&(c <= 102)) { // a..f
|
|
|
|
if ((n ^= 1)) {
|
|
|
|
if (l >= len) break;
|
|
|
|
((unsigned char *)buf)[l++] = (b | (c - (97 - 10)));
|
|
|
|
} else b = (c - (97 - 10)) << 4;
|
|
|
|
}
|
2014-08-05 16:56:49 +00:00
|
|
|
if (hex == eof)
|
|
|
|
break;
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Utils::getSecureRandom(void *buf,unsigned int bytes)
|
|
|
|
{
|
2014-08-08 18:53:55 +00:00
|
|
|
#ifdef __WINDOWS__
|
|
|
|
|
|
|
|
static HCRYPTPROV cryptProvider = NULL;
|
|
|
|
static Mutex globalLock;
|
|
|
|
|
|
|
|
Mutex::Lock _l(globalLock);
|
|
|
|
|
|
|
|
if (cryptProvider == NULL) {
|
|
|
|
if (!CryptAcquireContextA(&cryptProvider,NULL,NULL,PROV_RSA_FULL,CRYPT_VERIFYCONTEXT|CRYPT_SILENT)) {
|
|
|
|
fprintf(stderr,"FATAL ERROR: Utils::getSecureRandom() unable to obtain WinCrypt context!\r\n");
|
|
|
|
exit(1);
|
|
|
|
return;
|
|
|
|
}
|
2013-10-27 11:26:50 +00:00
|
|
|
}
|
|
|
|
|
2014-08-08 18:53:55 +00:00
|
|
|
if (!CryptGenRandom(cryptProvider,(DWORD)bytes,(BYTE *)buf)) {
|
|
|
|
fprintf(stderr,"FATAL ERROR: Utils::getSecureRandom() CryptGenRandom failed!\r\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else // not __WINDOWS__
|
|
|
|
|
2013-08-13 01:25:36 +00:00
|
|
|
#ifdef __UNIX_LIKE__
|
2014-08-08 18:53:55 +00:00
|
|
|
|
|
|
|
static char randomBuf[65536];
|
|
|
|
static unsigned int randomPtr = sizeof(randomBuf);
|
|
|
|
static int devURandomFd = -1;
|
|
|
|
static Mutex globalLock;
|
|
|
|
|
|
|
|
Mutex::Lock _l(globalLock);
|
|
|
|
|
|
|
|
if (devURandomFd <= 0) {
|
|
|
|
devURandomFd = ::open("/dev/urandom",O_RDONLY);
|
|
|
|
if (devURandomFd <= 0) {
|
|
|
|
fprintf(stderr,"FATAL ERROR: Utils::getSecureRandom() unable to open /dev/urandom\r\n");
|
|
|
|
exit(1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for(unsigned int i=0;i<bytes;++i) {
|
|
|
|
if (randomPtr >= sizeof(randomBuf)) {
|
|
|
|
if ((int)::read(devURandomFd,randomBuf,sizeof(randomBuf)) != (int)sizeof(randomBuf)) {
|
|
|
|
fprintf(stderr,"FATAL ERROR: Utils::getSecureRandom() unable to read from /dev/urandom\r\n");
|
|
|
|
exit(1);
|
|
|
|
return;
|
2013-08-13 01:25:36 +00:00
|
|
|
}
|
2014-08-08 18:53:55 +00:00
|
|
|
randomPtr = 0;
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
2014-08-08 18:53:55 +00:00
|
|
|
((char *)buf)[i] = randomBuf[randomPtr++];
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
2014-08-08 18:53:55 +00:00
|
|
|
|
|
|
|
#else // not __UNIX_LIKE__
|
|
|
|
|
|
|
|
#error No getSecureRandom() implementation available.
|
|
|
|
|
|
|
|
#endif // __UNIX_LIKE__
|
|
|
|
#endif // __WINDOWS__
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Utils::lockDownFile(const char *path,bool isDir)
|
|
|
|
{
|
2013-11-21 21:34:27 +00:00
|
|
|
#ifdef __UNIX_LIKE__
|
2013-07-04 20:56:19 +00:00
|
|
|
chmod(path,isDir ? 0700 : 0600);
|
|
|
|
#else
|
2014-01-26 18:21:43 +00:00
|
|
|
#ifdef __WINDOWS__
|
|
|
|
{
|
|
|
|
STARTUPINFOA startupInfo;
|
|
|
|
PROCESS_INFORMATION processInfo;
|
2014-08-08 21:57:13 +00:00
|
|
|
|
|
|
|
startupInfo.cb = sizeof(startupInfo);
|
2014-01-26 18:21:43 +00:00
|
|
|
memset(&startupInfo,0,sizeof(STARTUPINFOA));
|
|
|
|
memset(&processInfo,0,sizeof(PROCESS_INFORMATION));
|
2014-06-14 19:52:06 +00:00
|
|
|
if (CreateProcessA(NULL,(LPSTR)(std::string("C:\\Windows\\System32\\icacls.exe \"") + path + "\" /inheritance:d /Q").c_str(),NULL,NULL,FALSE,0,NULL,NULL,&startupInfo,&processInfo)) {
|
|
|
|
WaitForSingleObject(processInfo.hProcess,INFINITE);
|
|
|
|
CloseHandle(processInfo.hProcess);
|
|
|
|
CloseHandle(processInfo.hThread);
|
|
|
|
}
|
2014-08-08 21:57:13 +00:00
|
|
|
|
|
|
|
startupInfo.cb = sizeof(startupInfo);
|
|
|
|
memset(&startupInfo,0,sizeof(STARTUPINFOA));
|
|
|
|
memset(&processInfo,0,sizeof(PROCESS_INFORMATION));
|
2014-06-14 19:52:06 +00:00
|
|
|
if (CreateProcessA(NULL,(LPSTR)(std::string("C:\\Windows\\System32\\icacls.exe \"") + path + "\" /remove *S-1-5-32-545 /Q").c_str(),NULL,NULL,FALSE,0,NULL,NULL,&startupInfo,&processInfo)) {
|
2014-05-29 22:06:05 +00:00
|
|
|
WaitForSingleObject(processInfo.hProcess,INFINITE);
|
|
|
|
CloseHandle(processInfo.hProcess);
|
|
|
|
CloseHandle(processInfo.hThread);
|
|
|
|
}
|
2014-01-26 18:21:43 +00:00
|
|
|
}
|
2013-07-04 20:56:19 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t Utils::getLastModified(const char *path)
|
|
|
|
{
|
|
|
|
struct stat s;
|
|
|
|
if (stat(path,&s))
|
|
|
|
return 0;
|
|
|
|
return (((uint64_t)s.st_mtime) * 1000ULL);
|
|
|
|
}
|
|
|
|
|
2013-11-21 21:34:27 +00:00
|
|
|
bool Utils::fileExists(const char *path,bool followLinks)
|
|
|
|
{
|
|
|
|
struct stat s;
|
|
|
|
#ifdef __UNIX_LIKE__
|
|
|
|
if (!followLinks)
|
|
|
|
return (lstat(path,&s) == 0);
|
|
|
|
#endif
|
|
|
|
return (stat(path,&s) == 0);
|
|
|
|
}
|
|
|
|
|
2013-11-04 22:31:00 +00:00
|
|
|
int64_t Utils::getFileSize(const char *path)
|
2013-11-01 16:38:38 +00:00
|
|
|
{
|
|
|
|
struct stat s;
|
|
|
|
if (stat(path,&s))
|
|
|
|
return -1;
|
2014-01-18 01:09:59 +00:00
|
|
|
#ifdef __WINDOWS__
|
|
|
|
return s.st_size;
|
|
|
|
#else
|
2013-11-01 16:38:38 +00:00
|
|
|
if (S_ISREG(s.st_mode))
|
|
|
|
return s.st_size;
|
2014-01-18 01:09:59 +00:00
|
|
|
#endif
|
2013-11-01 16:38:38 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
bool Utils::readFile(const char *path,std::string &buf)
|
|
|
|
{
|
|
|
|
char tmp[4096];
|
|
|
|
FILE *f = fopen(path,"rb");
|
|
|
|
if (f) {
|
|
|
|
for(;;) {
|
|
|
|
long n = (long)fread(tmp,1,sizeof(tmp),f);
|
|
|
|
if (n > 0)
|
|
|
|
buf.append(tmp,n);
|
|
|
|
else break;
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Utils::writeFile(const char *path,const void *buf,unsigned int len)
|
|
|
|
{
|
|
|
|
FILE *f = fopen(path,"wb");
|
|
|
|
if (f) {
|
|
|
|
if ((long)fwrite(buf,1,len,f) != (long)len) {
|
|
|
|
fclose(f);
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
fclose(f);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> Utils::split(const char *s,const char *const sep,const char *esc,const char *quot)
|
|
|
|
{
|
|
|
|
std::vector<std::string> fields;
|
|
|
|
std::string buf;
|
|
|
|
|
|
|
|
if (!esc)
|
|
|
|
esc = "";
|
|
|
|
if (!quot)
|
|
|
|
quot = "";
|
|
|
|
|
|
|
|
bool escapeState = false;
|
|
|
|
char quoteState = 0;
|
|
|
|
while (*s) {
|
|
|
|
if (escapeState) {
|
|
|
|
escapeState = false;
|
|
|
|
buf.push_back(*s);
|
|
|
|
} else if (quoteState) {
|
|
|
|
if (*s == quoteState) {
|
|
|
|
quoteState = 0;
|
|
|
|
fields.push_back(buf);
|
|
|
|
buf.clear();
|
|
|
|
} else buf.push_back(*s);
|
|
|
|
} else {
|
|
|
|
const char *quotTmp;
|
|
|
|
if (strchr(esc,*s))
|
|
|
|
escapeState = true;
|
|
|
|
else if ((buf.size() <= 0)&&((quotTmp = strchr(quot,*s))))
|
|
|
|
quoteState = *quotTmp;
|
|
|
|
else if (strchr(sep,*s)) {
|
|
|
|
if (buf.size() > 0) {
|
|
|
|
fields.push_back(buf);
|
|
|
|
buf.clear();
|
|
|
|
} // else skip runs of seperators
|
|
|
|
} else buf.push_back(*s);
|
|
|
|
}
|
|
|
|
++s;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buf.size())
|
|
|
|
fields.push_back(buf);
|
|
|
|
|
|
|
|
return fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Utils::trim(const std::string &s)
|
|
|
|
{
|
2013-08-26 21:22:20 +00:00
|
|
|
unsigned long end = (unsigned long)s.length();
|
2013-07-04 20:56:19 +00:00
|
|
|
while (end) {
|
|
|
|
char c = s[end - 1];
|
|
|
|
if ((c == ' ')||(c == '\r')||(c == '\n')||(!c)||(c == '\t'))
|
|
|
|
--end;
|
|
|
|
else break;
|
|
|
|
}
|
|
|
|
unsigned long start = 0;
|
|
|
|
while (start < end) {
|
|
|
|
char c = s[start];
|
|
|
|
if ((c == ' ')||(c == '\r')||(c == '\n')||(!c)||(c == '\t'))
|
|
|
|
++start;
|
|
|
|
else break;
|
|
|
|
}
|
|
|
|
return s.substr(start,end - start);
|
|
|
|
}
|
|
|
|
|
2013-08-30 21:05:43 +00:00
|
|
|
unsigned int Utils::snprintf(char *buf,unsigned int len,const char *fmt,...)
|
|
|
|
throw(std::length_error)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap,fmt);
|
|
|
|
int n = (int)vsnprintf(buf,len,fmt,ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
if ((n >= (int)len)||(n < 0)) {
|
|
|
|
if (len)
|
|
|
|
buf[len - 1] = (char)0;
|
|
|
|
throw std::length_error("buf[] overflow in Utils::snprintf");
|
|
|
|
}
|
|
|
|
|
|
|
|
return (unsigned int)n;
|
|
|
|
}
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
} // namespace ZeroTier
|