1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
8 #include "clientversion.h"
12 #include "ui_interface.h"
19 #include <boost/algorithm/string/classification.hpp>
20 #include <boost/algorithm/string/replace.hpp>
21 #include <boost/foreach.hpp>
22 #include <boost/thread.hpp>
26 map<uint256, CAlert> mapAlerts;
27 CCriticalSection cs_mapAlerts;
29 void CUnsignedAlert::SetNull()
47 std::string CUnsignedAlert::ToString() const
49 std::string strSetCancel;
50 BOOST_FOREACH(int n, setCancel)
51 strSetCancel += strprintf("%d ", n);
52 std::string strSetSubVer;
53 BOOST_FOREACH(std::string str, setSubVer)
54 strSetSubVer += "\"" + str + "\" ";
67 " strComment = \"%s\"\n"
68 " strStatusBar = \"%s\"\n"
69 " strRPCError = \"%s\"\n"
86 void CAlert::SetNull()
88 CUnsignedAlert::SetNull();
93 bool CAlert::IsNull() const
95 return (nExpiration == 0);
98 uint256 CAlert::GetHash() const
100 return Hash(this->vchMsg.begin(), this->vchMsg.end());
103 bool CAlert::IsInEffect() const
105 return (GetAdjustedTime() < nExpiration);
108 bool CAlert::Cancels(const CAlert& alert) const
111 return false; // this was a no-op before 31403
112 return (alert.nID <= nCancel || setCancel.count(alert.nID));
115 bool CAlert::AppliesTo(int nVersion, std::string strSubVerIn) const
117 // TODO: rework for client-version-embedded-in-strSubVer ?
118 return (IsInEffect() &&
119 nMinVer <= nVersion && nVersion <= nMaxVer &&
120 (setSubVer.empty() || setSubVer.count(strSubVerIn)));
123 bool CAlert::AppliesToMe() const
125 return AppliesTo(PROTOCOL_VERSION, FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, std::vector<std::string>()));
128 bool CAlert::RelayTo(CNode* pnode) const
132 // don't relay to nodes which haven't sent their version message
133 if (pnode->nVersion == 0)
135 // returns true if wasn't already contained in the set
136 if (pnode->setKnown.insert(GetHash()).second)
138 if (AppliesTo(pnode->nVersion, pnode->strSubVer) ||
140 GetAdjustedTime() < nRelayUntil)
142 pnode->PushMessage("alert", *this);
149 bool CAlert::CheckSignature(const std::vector<unsigned char>& alertKey) const
151 CPubKey key(alertKey);
152 if (!key.Verify(Hash(vchMsg.begin(), vchMsg.end()), vchSig))
153 return error("CAlert::CheckSignature(): verify signature failed");
155 // Now unserialize the data
156 CDataStream sMsg(vchMsg, SER_NETWORK, PROTOCOL_VERSION);
157 sMsg >> *(CUnsignedAlert*)this;
161 CAlert CAlert::getAlertByHash(const uint256 &hash)
166 map<uint256, CAlert>::iterator mi = mapAlerts.find(hash);
167 if(mi != mapAlerts.end())
173 bool CAlert::ProcessAlert(const std::vector<unsigned char>& alertKey, bool fThread)
175 if (!CheckSignature(alertKey))
180 // alert.nID=max is reserved for if the alert key is
181 // compromised. It must have a pre-defined message,
182 // must never expire, must apply to all versions,
183 // and must cancel all previous
184 // alerts or it will be ignored (so an attacker can't
185 // send an "everything is OK, don't panic" version that
186 // cannot be overridden):
187 int maxInt = std::numeric_limits<int>::max();
191 nExpiration == maxInt &&
192 nCancel == (maxInt-1) &&
196 nPriority == maxInt &&
197 strStatusBar == "URGENT: Alert key compromised, upgrade required"
204 // Cancel previous alerts
205 for (map<uint256, CAlert>::iterator mi = mapAlerts.begin(); mi != mapAlerts.end();)
207 const CAlert& alert = (*mi).second;
210 LogPrint("alert", "cancelling alert %d\n", alert.nID);
211 uiInterface.NotifyAlertChanged((*mi).first, CT_DELETED);
212 mapAlerts.erase(mi++);
214 else if (!alert.IsInEffect())
216 LogPrint("alert", "expiring alert %d\n", alert.nID);
217 uiInterface.NotifyAlertChanged((*mi).first, CT_DELETED);
218 mapAlerts.erase(mi++);
224 // Check if this alert has been cancelled
225 BOOST_FOREACH(PAIRTYPE(const uint256, CAlert)& item, mapAlerts)
227 const CAlert& alert = item.second;
228 if (alert.Cancels(*this))
230 LogPrint("alert", "alert already cancelled by %d\n", alert.nID);
236 mapAlerts.insert(make_pair(GetHash(), *this));
237 // Notify UI and -alertnotify if it applies to me
240 uiInterface.NotifyAlertChanged(GetHash(), CT_NEW);
241 Notify(strStatusBar, fThread);
245 LogPrint("alert", "accepted alert %d, AppliesToMe()=%d\n", nID, AppliesToMe());
250 CAlert::Notify(const std::string& strMessage, bool fThread)
252 std::string strCmd = GetArg("-alertnotify", "");
253 if (strCmd.empty()) return;
255 // Alert text should be plain ascii coming from a trusted source, but to
256 // be safe we first strip anything not in safeChars, then add single quotes around
257 // the whole string before passing it to the shell:
258 std::string singleQuote("'");
259 std::string safeStatus = SanitizeString(strMessage);
260 safeStatus = singleQuote+safeStatus+singleQuote;
261 boost::replace_all(strCmd, "%s", safeStatus);
264 boost::thread t(runCommand, strCmd); // thread runs free