/*************************************************************************
* UrBackup - Client/Server backup system
* Copyright (C) 2011-2016 Martin Raiber
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
**************************************************************************/
#include "Connector.h"
#include "tcpstack.h"
#include "../stringtools.h"
#include "../urbackupcommon/escape.h"
#include "../urbackupcommon/os_functions.h"
#include "../socket_header.h"
#include "json/json.h"
#ifdef _WIN32
#include
#else
#include "../config.h"
#endif
#include
#include
#include
#include
std::string Connector::pw;
bool Connector::error=false;
const size_t conn_retries=4;
bool Connector::busy=false;
std::string Connector::pwfile="pw.txt";
std::string Connector::pwfile_change="pw_change.txt";
std::string Connector::client="127.0.0.1";
std::string Connector::tokens;
namespace
{
bool LookupBlocking(std::string pServer, in_addr *dest)
{
const char* host=pServer.c_str();
unsigned int addr = inet_addr(host);
if (addr != INADDR_NONE)
{
dest->s_addr = addr;
}
else
{
hostent* hp = gethostbyname(host);
if (hp != 0)
{
memcpy(dest, hp->h_addr, hp->h_length);
}
else
{
return false;
}
}
return true;
}
void read_tokens(std::string token_path, std::string& tokens)
{
if(os_directory_exists(os_file_prefix(token_path)))
{
std::vector token_files = getFiles(token_path);
for(size_t i=0;i Connector::getSharedPaths(bool use_change_pw)
{
std::vector ret;
std::string d = getResponse("GET BACKUP DIRS", "", use_change_pw);
if (d.empty())
{
error = true;
return ret;
}
Json::Value root;
Json::Reader reader;
if (!reader.parse(d, root, false))
{
return ret;
}
try
{
Json::Value dirs = root["dirs"];
for (Json::Value::ArrayIndex i = 0; i &res)
{
std::string args="all_virtual_clients=1";
for (size_t i = 0; i toks;
Tokenize(d, toks, "#");
SStatus ret;
ret.pause=false;
if(toks.size()>0)
ret.lastbackupdate=toks[0];
if(toks.size()>1)
ret.status=toks[1];
if(toks.size()>2)
ret.pcdone=toks[2];
if(toks.size()>3)
{
if(toks[3]=="P")
ret.pause=true;
else if(toks[3]=="NP")
ret.pause=false;
}
return ret;
}
int Connector::startBackup(const std::string& virtual_client, bool full)
{
std::string s;
if(full)
s="START BACKUP FULL";
else
s="START BACKUP INCR";
std::string d=getResponse(s, virtual_client.empty() ? "" : "virtual_client="+EscapeParamString(virtual_client),false);
if(d=="RUNNING")
return 2;
else if(d=="NO SERVER")
return 3;
else if(d!="OK")
return 0;
else
return 1;
}
int Connector::startImage(const std::string& virtual_client, bool full)
{
std::string s;
if(full)
s="START IMAGE FULL";
else
s="START IMAGE INCR";
std::string d=getResponse(s, virtual_client.empty() ? "" : "virtual_client=" + EscapeParamString(virtual_client),false);
if(d=="RUNNING")
return 2;
else if(d=="NO SERVER")
return 3;
else if(d!="OK")
return 0;
else
return 1;
}
bool Connector::updateSettings(const std::string &ndata, bool& no_perm)
{
no_perm = false;
std::string data=ndata;
escapeClientMessage(data);
std::string d=getResponse("UPDATE SETTINGS "+data,"", true);
if (d != "OK" && d != "NOSERVER")
{
if (d == "FAILED")
{
no_perm = true;
}
return false;
}
else
{
return true;
}
}
std::vector Connector::getLogEntries(void)
{
std::string d=getResponse("GET LOGPOINTS","", true);
int lc=linecount(d);
std::vector ret;
for(int i=0;i Connector::getLogdata(int logid, int loglevel)
{
std::string d=getResponse("GET LOGDATA","logid="+convert(logid)+"&loglevel="+convert(loglevel), true);
std::vector lines;
Tokenize(d, lines, "\n");
std::vector ret;
for(size_t i=0;i& map_paths, EAccessError& access_error, bool clean_other,
bool ignore_other_fs, bool follow_symlinks)
{
access_error = EAccessError_Ok;
if(!readTokens())
{
access_error = EAccessError_NoTokens;
return std::string();
}
std::string params = "tokens="+tokens;
params+="&path="+EscapeParamString(path);
params+="&backupid="+convert(backupid);
if (!virtual_client.empty())
{
params += "&virtual_client=" + EscapeParamString(virtual_client);
}
for (size_t i = 0; i < map_paths.size(); ++i)
{
params += "&map_path_source"+convert(i)+"=" + EscapeParamString(map_paths[i].source);
params += "&map_path_target" + convert(i) + "=" + EscapeParamString(map_paths[i].target);
}
params += std::string("&clean_other=") + (clean_other ? "1" : "0");
params += std::string("&ignore_other_fs=") + (ignore_other_fs ? "1" : "0");
params += std::string("&follow_symlinks=") + (follow_symlinks ? "1" : "0");
std::string res = getResponse("DOWNLOAD FILES TOKENS",
params, false);
if(!res.empty())
{
if(res[0]!='0')
{
if(res[0]=='1')
{
access_error = EAccessError_NoServer;
}
return std::string();
}
else
{
return res.substr(1);
}
}
else
{
return std::string();
}
}
std::string Connector::getStatusDetailsRaw()
{
return getResponse("STATUS DETAIL", "", false);
}
SStatusDetails Connector::getStatusDetails()
{
std::string d = getResponse("STATUS DETAIL", "", false);
SStatusDetails ret;
ret.ok = false;
Json::Value root;
Json::Reader reader;
if (!reader.parse(d, root, false))
{
return ret;
}
try
{
ret.last_backup_time = root["last_backup_time"].asInt64();
Json::Value json_running_processes = root["running_processes"];
ret.running_processes.resize(json_running_processes.size());
for (unsigned int i = 0; i servers;
Json::Value json_servers = root["servers"];
servers.resize(json_servers.size());
for (unsigned int i = 0; i