2013-11-11 16:20:39 +01:00
|
|
|
// Copyright (c) 2010 Satoshi Nakamoto
|
2015-12-13 14:51:43 +01:00
|
|
|
// Copyright (c) 2009-2015 The Bitcoin Core developers
|
2014-11-04 14:34:04 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2013-11-11 16:20:39 +01:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2012-08-28 23:04:54 +02:00
|
|
|
|
|
|
|
#include "alert.h"
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2017-03-15 12:54:34 +01:00
|
|
|
#include "base58.h"
|
2014-10-29 02:33:23 +01:00
|
|
|
#include "clientversion.h"
|
2012-08-28 23:04:54 +02:00
|
|
|
#include "net.h"
|
2016-11-25 20:01:56 +01:00
|
|
|
#include "netmessagemaker.h"
|
2014-11-04 14:34:04 +01:00
|
|
|
#include "pubkey.h"
|
2014-06-19 15:08:37 +02:00
|
|
|
#include "timedata.h"
|
2012-08-28 23:04:54 +02:00
|
|
|
#include "ui_interface.h"
|
2013-04-13 07:13:08 +02:00
|
|
|
#include "util.h"
|
2015-07-05 14:17:46 +02:00
|
|
|
#include "utilstrencodings.h"
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2014-03-18 22:54:47 +01:00
|
|
|
#include <stdint.h>
|
2013-04-13 07:13:08 +02:00
|
|
|
#include <algorithm>
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
#include <boost/algorithm/string/classification.hpp>
|
|
|
|
#include <boost/algorithm/string/replace.hpp>
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
#include <boost/thread.hpp>
|
2012-08-28 23:04:54 +02:00
|
|
|
|
2018-02-21 20:26:53 +01:00
|
|
|
std::map<uint256, CAlert> mapAlerts;
|
2012-08-28 23:04:54 +02:00
|
|
|
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;
|
2018-02-06 12:09:33 +01:00
|
|
|
for (const auto& n : setCancel)
|
2012-08-28 23:04:54 +02:00
|
|
|
strSetCancel += strprintf("%d ", n);
|
|
|
|
std::string strSetSubVer;
|
2018-02-06 12:09:33 +01:00
|
|
|
for (const auto& str : setSubVer)
|
2012-08-28 23:04:54 +02:00
|
|
|
strSetSubVer += "\"" + str + "\" ";
|
|
|
|
return strprintf(
|
|
|
|
"CAlert(\n"
|
|
|
|
" nVersion = %d\n"
|
2014-02-24 09:08:56 +01:00
|
|
|
" nRelayUntil = %d\n"
|
|
|
|
" nExpiration = %d\n"
|
2012-08-28 23:04:54 +02:00
|
|
|
" 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,
|
2014-01-16 16:15:27 +01:00
|
|
|
strSetCancel,
|
2012-08-28 23:04:54 +02:00
|
|
|
nMinVer,
|
|
|
|
nMaxVer,
|
2014-01-16 16:15:27 +01:00
|
|
|
strSetSubVer,
|
2012-08-28 23:04:54 +02:00
|
|
|
nPriority,
|
2014-01-16 16:15:27 +01:00
|
|
|
strComment,
|
|
|
|
strStatusBar);
|
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));
|
|
|
|
}
|
|
|
|
|
2015-05-31 15:36:44 +02:00
|
|
|
bool CAlert::AppliesTo(int nVersion, const std::string& strSubVerIn) const
|
2012-08-28 23:04:54 +02:00
|
|
|
{
|
|
|
|
// 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>()));
|
|
|
|
}
|
|
|
|
|
2017-07-27 16:28:05 +02:00
|
|
|
bool CAlert::RelayTo(CNode* pnode, CConnman& connman) const
|
2012-08-28 23:04:54 +02:00
|
|
|
{
|
|
|
|
if (!IsInEffect())
|
|
|
|
return false;
|
2014-10-29 16:03:09 +01:00
|
|
|
// don't relay to nodes which haven't sent their version message
|
|
|
|
if (pnode->nVersion == 0)
|
|
|
|
return false;
|
2012-08-28 23:04:54 +02:00
|
|
|
// 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)
|
|
|
|
{
|
2016-11-25 20:01:56 +01:00
|
|
|
connman.PushMessage(pnode, CNetMsgMaker(pnode->GetSendVersion()).Make(NetMsgType::ALERT, *this));
|
2012-08-28 23:04:54 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-15 12:54:34 +01:00
|
|
|
bool CAlert::Sign()
|
|
|
|
{
|
|
|
|
CDataStream sMsg(SER_NETWORK, CLIENT_VERSION);
|
|
|
|
sMsg << *(CUnsignedAlert*)this;
|
|
|
|
vchMsg = std::vector<unsigned char>(sMsg.begin(), sMsg.end());
|
|
|
|
CBitcoinSecret vchSecret;
|
|
|
|
if (!vchSecret.SetString(GetArg("-alertkey", "")))
|
|
|
|
{
|
|
|
|
printf("CAlert::SignAlert() : vchSecret.SetString failed\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
CKey key = vchSecret.GetKey();
|
|
|
|
if (!key.Sign(Hash(vchMsg.begin(), vchMsg.end()), vchSig))
|
|
|
|
{
|
|
|
|
printf("CAlert::SignAlert() : key.Sign failed\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-03 17:42:06 +02:00
|
|
|
bool CAlert::CheckSignature(const std::vector<unsigned char>& alertKey) const
|
2012-08-28 23:04:54 +02:00
|
|
|
{
|
2015-04-03 17:42:06 +02:00
|
|
|
CPubKey key(alertKey);
|
2012-08-28 23:04:54 +02:00
|
|
|
if (!key.Verify(Hash(vchMsg.begin(), vchMsg.end()), vchSig))
|
2015-01-08 11:44:25 +01:00
|
|
|
return error("CAlert::CheckSignature(): verify signature failed");
|
2012-08-28 23:04:54 +02:00
|
|
|
|
|
|
|
// 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);
|
2018-02-21 20:26:53 +01:00
|
|
|
std::map<uint256, CAlert>::iterator mi = mapAlerts.find(hash);
|
2012-08-28 23:04:54 +02:00
|
|
|
if(mi != mapAlerts.end())
|
|
|
|
retval = mi->second;
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2018-02-06 12:09:33 +01:00
|
|
|
bool CAlert::ProcessAlert(const std::vector<unsigned char>& alertKey, bool fThread) const
|
2012-08-28 23:04:54 +02:00
|
|
|
{
|
2015-04-03 17:42:06 +02:00
|
|
|
if (!CheckSignature(alertKey))
|
2012-08-28 23:04:54 +02:00
|
|
|
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
|
2018-02-21 20:26:53 +01:00
|
|
|
for (std::map<uint256, CAlert>::iterator mi = mapAlerts.begin(); mi != mapAlerts.end();)
|
2012-08-28 23:04:54 +02:00
|
|
|
{
|
|
|
|
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
|
2018-02-06 12:09:33 +01:00
|
|
|
for (const auto& item : mapAlerts)
|
2012-08-28 23:04:54 +02:00
|
|
|
{
|
|
|
|
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
|
2018-02-21 20:26:53 +01:00
|
|
|
mapAlerts.insert(std::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);
|
2014-10-07 19:11:48 +02:00
|
|
|
Notify(strStatusBar, fThread);
|
2013-03-19 19:08:21 +01:00
|
|
|
}
|
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;
|
|
|
|
}
|
2014-10-07 19:11:48 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
CAlert::Notify(const std::string& strMessage, bool fThread)
|
|
|
|
{
|
|
|
|
std::string strCmd = GetArg("-alertnotify", "");
|
|
|
|
if (strCmd.empty()) return;
|
|
|
|
|
|
|
|
// 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("'");
|
|
|
|
std::string safeStatus = SanitizeString(strMessage);
|
|
|
|
safeStatus = singleQuote+safeStatus+singleQuote;
|
|
|
|
boost::replace_all(strCmd, "%s", safeStatus);
|
|
|
|
|
|
|
|
if (fThread)
|
|
|
|
boost::thread t(runCommand, strCmd); // thread runs free
|
|
|
|
else
|
|
|
|
runCommand(strCmd);
|
|
|
|
}
|