2012-08-28 23:04:54 +02:00
|
|
|
//
|
|
|
|
// Alert system
|
|
|
|
//
|
|
|
|
|
2013-03-19 19:08:21 +01:00
|
|
|
#include <algorithm>
|
|
|
|
#include <boost/algorithm/string/classification.hpp>
|
|
|
|
#include <boost/algorithm/string/replace.hpp>
|
2012-08-28 23:04:54 +02:00
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
#include "alert.h"
|
|
|
|
#include "key.h"
|
|
|
|
#include "net.h"
|
|
|
|
#include "sync.h"
|
|
|
|
#include "ui_interface.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
map<uint256, CAlert> mapAlerts;
|
|
|
|
CCriticalSection cs_mapAlerts;
|
|
|
|
|
|
|
|
void CUnsignedAlert::SetNull()
|
|
|
|
{
|
|
|
|
nVersion = 1;
|
|
|
|
nRelayUntil = 0;
|
|
|
|
nExpiration = 0;
|
|
|
|
nID = 0;
|
|
|
|
nCancel = 0;
|
|
|
|
setCancel.clear();
|
|
|
|
nMinVer = 0;
|
|
|
|
nMaxVer = 0;
|
|
|
|
setSubVer.clear();
|
|
|
|
nPriority = 0;
|
|
|
|
|
|
|
|
strComment.clear();
|
|
|
|
strStatusBar.clear();
|
|
|
|
strReserved.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CUnsignedAlert::ToString() const
|
|
|
|
{
|
|
|
|
std::string strSetCancel;
|
|
|
|
BOOST_FOREACH(int n, setCancel)
|
|
|
|
strSetCancel += strprintf("%d ", n);
|
|
|
|
std::string strSetSubVer;
|
|
|
|
BOOST_FOREACH(std::string str, setSubVer)
|
|
|
|
strSetSubVer += "\"" + str + "\" ";
|
|
|
|
return strprintf(
|
|
|
|
"CAlert(\n"
|
|
|
|
" nVersion = %d\n"
|
|
|
|
" nRelayUntil = %"PRI64d"\n"
|
|
|
|
" nExpiration = %"PRI64d"\n"
|
|
|
|
" nID = %d\n"
|
|
|
|
" nCancel = %d\n"
|
|
|
|
" setCancel = %s\n"
|
|
|
|
" nMinVer = %d\n"
|
|
|
|
" nMaxVer = %d\n"
|
|
|
|
" setSubVer = %s\n"
|
|
|
|
" nPriority = %d\n"
|
|
|
|
" strComment = \"%s\"\n"
|
|
|
|
" strStatusBar = \"%s\"\n"
|
|
|
|
")\n",
|
|
|
|
nVersion,
|
|
|
|
nRelayUntil,
|
|
|
|
nExpiration,
|
|
|
|
nID,
|
|
|
|
nCancel,
|
|
|
|
strSetCancel.c_str(),
|
|
|
|
nMinVer,
|
|
|
|
nMaxVer,
|
|
|
|
strSetSubVer.c_str(),
|
|
|
|
nPriority,
|
|
|
|
strComment.c_str(),
|
|
|
|
strStatusBar.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void CUnsignedAlert::print() const
|
|
|
|
{
|
2013-09-18 12:38:08 +02:00
|
|
|
LogPrintf("%s", ToString().c_str());
|
2012-08-28 23:04:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CAlert::SetNull()
|
|
|
|
{
|
|
|
|
CUnsignedAlert::SetNull();
|
|
|
|
vchMsg.clear();
|
|
|
|
vchSig.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CAlert::IsNull() const
|
|
|
|
{
|
|
|
|
return (nExpiration == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint256 CAlert::GetHash() const
|
|
|
|
{
|
|
|
|
return Hash(this->vchMsg.begin(), this->vchMsg.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CAlert::IsInEffect() const
|
|
|
|
{
|
|
|
|
return (GetAdjustedTime() < nExpiration);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CAlert::Cancels(const CAlert& alert) const
|
|
|
|
{
|
|
|
|
if (!IsInEffect())
|
|
|
|
return false; // this was a no-op before 31403
|
|
|
|
return (alert.nID <= nCancel || setCancel.count(alert.nID));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CAlert::AppliesTo(int nVersion, std::string strSubVerIn) const
|
|
|
|
{
|
|
|
|
// TODO: rework for client-version-embedded-in-strSubVer ?
|
|
|
|
return (IsInEffect() &&
|
|
|
|
nMinVer <= nVersion && nVersion <= nMaxVer &&
|
|
|
|
(setSubVer.empty() || setSubVer.count(strSubVerIn)));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CAlert::AppliesToMe() const
|
|
|
|
{
|
|
|
|
return AppliesTo(PROTOCOL_VERSION, FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, std::vector<std::string>()));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CAlert::RelayTo(CNode* pnode) const
|
|
|
|
{
|
|
|
|
if (!IsInEffect())
|
|
|
|
return false;
|
|
|
|
// returns true if wasn't already contained in the set
|
|
|
|
if (pnode->setKnown.insert(GetHash()).second)
|
|
|
|
{
|
|
|
|
if (AppliesTo(pnode->nVersion, pnode->strSubVer) ||
|
|
|
|
AppliesToMe() ||
|
|
|
|
GetAdjustedTime() < nRelayUntil)
|
|
|
|
{
|
|
|
|
pnode->PushMessage("alert", *this);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CAlert::CheckSignature() const
|
|
|
|
{
|
2013-05-07 15:16:25 +02:00
|
|
|
CPubKey key(Params().AlertKey());
|
2012-08-28 23:04:54 +02:00
|
|
|
if (!key.Verify(Hash(vchMsg.begin(), vchMsg.end()), vchSig))
|
|
|
|
return error("CAlert::CheckSignature() : verify signature failed");
|
|
|
|
|
|
|
|
// Now unserialize the data
|
|
|
|
CDataStream sMsg(vchMsg, SER_NETWORK, PROTOCOL_VERSION);
|
|
|
|
sMsg >> *(CUnsignedAlert*)this;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
CAlert CAlert::getAlertByHash(const uint256 &hash)
|
|
|
|
{
|
|
|
|
CAlert retval;
|
|
|
|
{
|
|
|
|
LOCK(cs_mapAlerts);
|
|
|
|
map<uint256, CAlert>::iterator mi = mapAlerts.find(hash);
|
|
|
|
if(mi != mapAlerts.end())
|
|
|
|
retval = mi->second;
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2013-03-19 19:08:21 +01:00
|
|
|
bool CAlert::ProcessAlert(bool fThread)
|
2012-08-28 23:04:54 +02:00
|
|
|
{
|
|
|
|
if (!CheckSignature())
|
|
|
|
return false;
|
|
|
|
if (!IsInEffect())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// alert.nID=max is reserved for if the alert key is
|
|
|
|
// compromised. It must have a pre-defined message,
|
|
|
|
// must never expire, must apply to all versions,
|
|
|
|
// and must cancel all previous
|
|
|
|
// alerts or it will be ignored (so an attacker can't
|
|
|
|
// send an "everything is OK, don't panic" version that
|
|
|
|
// cannot be overridden):
|
|
|
|
int maxInt = std::numeric_limits<int>::max();
|
|
|
|
if (nID == maxInt)
|
|
|
|
{
|
|
|
|
if (!(
|
|
|
|
nExpiration == maxInt &&
|
|
|
|
nCancel == (maxInt-1) &&
|
|
|
|
nMinVer == 0 &&
|
|
|
|
nMaxVer == maxInt &&
|
|
|
|
setSubVer.empty() &&
|
|
|
|
nPriority == maxInt &&
|
|
|
|
strStatusBar == "URGENT: Alert key compromised, upgrade required"
|
|
|
|
))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
LOCK(cs_mapAlerts);
|
|
|
|
// Cancel previous alerts
|
|
|
|
for (map<uint256, CAlert>::iterator mi = mapAlerts.begin(); mi != mapAlerts.end();)
|
|
|
|
{
|
|
|
|
const CAlert& alert = (*mi).second;
|
|
|
|
if (Cancels(alert))
|
|
|
|
{
|
2013-09-18 12:38:08 +02:00
|
|
|
LogPrint("alert", "cancelling alert %d\n", alert.nID);
|
2012-08-28 23:04:54 +02:00
|
|
|
uiInterface.NotifyAlertChanged((*mi).first, CT_DELETED);
|
|
|
|
mapAlerts.erase(mi++);
|
|
|
|
}
|
|
|
|
else if (!alert.IsInEffect())
|
|
|
|
{
|
2013-09-18 12:38:08 +02:00
|
|
|
LogPrint("alert", "expiring alert %d\n", alert.nID);
|
2012-08-28 23:04:54 +02:00
|
|
|
uiInterface.NotifyAlertChanged((*mi).first, CT_DELETED);
|
|
|
|
mapAlerts.erase(mi++);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
mi++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if this alert has been cancelled
|
|
|
|
BOOST_FOREACH(PAIRTYPE(const uint256, CAlert)& item, mapAlerts)
|
|
|
|
{
|
|
|
|
const CAlert& alert = item.second;
|
|
|
|
if (alert.Cancels(*this))
|
|
|
|
{
|
2013-09-18 12:38:08 +02:00
|
|
|
LogPrint("alert", "alert already cancelled by %d\n", alert.nID);
|
2012-08-28 23:04:54 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add to mapAlerts
|
|
|
|
mapAlerts.insert(make_pair(GetHash(), *this));
|
2013-03-19 19:08:21 +01:00
|
|
|
// Notify UI and -alertnotify if it applies to me
|
2012-08-28 23:04:54 +02:00
|
|
|
if(AppliesToMe())
|
2013-03-19 19:08:21 +01:00
|
|
|
{
|
2012-08-28 23:04:54 +02:00
|
|
|
uiInterface.NotifyAlertChanged(GetHash(), CT_NEW);
|
2013-03-19 19:08:21 +01:00
|
|
|
std::string strCmd = GetArg("-alertnotify", "");
|
|
|
|
if (!strCmd.empty())
|
|
|
|
{
|
|
|
|
// Alert text should be plain ascii coming from a trusted source, but to
|
|
|
|
// be safe we first strip anything not in safeChars, then add single quotes around
|
|
|
|
// the whole string before passing it to the shell:
|
|
|
|
std::string singleQuote("'");
|
|
|
|
// safeChars chosen to allow simple messages/URLs/email addresses, but avoid anything
|
|
|
|
// even possibly remotely dangerous like & or >
|
|
|
|
std::string safeChars("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890 .,;_/:?@");
|
|
|
|
std::string safeStatus;
|
|
|
|
for (std::string::size_type i = 0; i < strStatusBar.size(); i++)
|
|
|
|
{
|
|
|
|
if (safeChars.find(strStatusBar[i]) != std::string::npos)
|
|
|
|
safeStatus.push_back(strStatusBar[i]);
|
|
|
|
}
|
|
|
|
safeStatus = singleQuote+safeStatus+singleQuote;
|
|
|
|
boost::replace_all(strCmd, "%s", safeStatus);
|
|
|
|
|
|
|
|
if (fThread)
|
|
|
|
boost::thread t(runCommand, strCmd); // thread runs free
|
|
|
|
else
|
|
|
|
runCommand(strCmd);
|
|
|
|
}
|
|
|
|
}
|
2012-08-28 23:04:54 +02:00
|
|
|
}
|
|
|
|
|
2013-09-18 12:38:08 +02:00
|
|
|
LogPrint("alert", "accepted alert %d, AppliesToMe()=%d\n", nID, AppliesToMe());
|
2012-08-28 23:04:54 +02:00
|
|
|
return true;
|
|
|
|
}
|