aboutsummaryrefslogtreecommitdiff
path: root/rpc.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'rpc.cpp')
-rw-r--r--rpc.cpp81
1 files changed, 57 insertions, 24 deletions
diff --git a/rpc.cpp b/rpc.cpp
index ecc3d6a5ff..57738b89aa 100644
--- a/rpc.cpp
+++ b/rpc.cpp
@@ -21,6 +21,8 @@ void ThreadRPCServer2(void* parg);
typedef Value(*rpcfn_type)(const Array& params, bool fHelp);
extern map<string, rpcfn_type> mapCallTable;
+static string strRPCPassword;
+
@@ -37,16 +39,24 @@ Value help(const Array& params, bool fHelp)
{
if (fHelp || params.size() != 0)
throw runtime_error(
- "help\n"
+ "help <pw>\n"
"List commands.");
string strRet;
+ set<rpcfn_type> setDone;
for (map<string, rpcfn_type>::iterator mi = mapCallTable.begin(); mi != mapCallTable.end(); ++mi)
{
+ string strMethod = (*mi).first;
+ // We already filter duplicates, but these deprecated screw up the sort order
+ if (strMethod == "getamountreceived" ||
+ strMethod == "getallreceived")
+ continue;
try
{
Array params;
- (*(*mi).second)(params, true);
+ rpcfn_type pfn = (*mi).second;
+ if (setDone.insert(pfn).second)
+ (*pfn)(params, true);
}
catch (std::exception& e)
{
@@ -66,7 +76,7 @@ Value stop(const Array& params, bool fHelp)
{
if (fHelp || params.size() != 0)
throw runtime_error(
- "stop\n"
+ "stop <pw>\n"
"Stop bitcoin server.");
// Shutdown will take long enough that the response should get back
@@ -79,7 +89,7 @@ Value getblockcount(const Array& params, bool fHelp)
{
if (fHelp || params.size() != 0)
throw runtime_error(
- "getblockcount\n"
+ "getblockcount <pw>\n"
"Returns the number of blocks in the longest block chain.");
return nBestHeight + 1;
@@ -90,7 +100,7 @@ Value getblocknumber(const Array& params, bool fHelp)
{
if (fHelp || params.size() != 0)
throw runtime_error(
- "getblocknumber\n"
+ "getblocknumber <pw>\n"
"Returns the block number of the latest block in the longest block chain.");
return nBestHeight;
@@ -101,7 +111,7 @@ Value getconnectioncount(const Array& params, bool fHelp)
{
if (fHelp || params.size() != 0)
throw runtime_error(
- "getconnectioncount\n"
+ "getconnectioncount <pw>\n"
"Returns the number of connections to other nodes.");
return (int)vNodes.size();
@@ -124,7 +134,7 @@ Value getdifficulty(const Array& params, bool fHelp)
{
if (fHelp || params.size() != 0)
throw runtime_error(
- "getdifficulty\n"
+ "getdifficulty <pw>\n"
"Returns the proof-of-work difficulty as a multiple of the minimum difficulty.");
return GetDifficulty();
@@ -135,7 +145,7 @@ Value getbalance(const Array& params, bool fHelp)
{
if (fHelp || params.size() != 0)
throw runtime_error(
- "getbalance\n"
+ "getbalance <pw>\n"
"Returns the server's available balance.");
return ((double)GetBalance() / (double)COIN);
@@ -146,7 +156,7 @@ Value getgenerate(const Array& params, bool fHelp)
{
if (fHelp || params.size() != 0)
throw runtime_error(
- "getgenerate\n"
+ "getgenerate <pw>\n"
"Returns true or false.");
return (bool)fGenerateBitcoins;
@@ -157,7 +167,7 @@ Value setgenerate(const Array& params, bool fHelp)
{
if (fHelp || params.size() < 1 || params.size() > 2)
throw runtime_error(
- "setgenerate <generate> [genproclimit]\n"
+ "setgenerate <pw> <generate> [genproclimit]\n"
"<generate> is true or false to turn generation on or off.\n"
"Generation is limited to [genproclimit] processors, -1 is unlimited.");
@@ -183,7 +193,7 @@ Value getinfo(const Array& params, bool fHelp)
{
if (fHelp || params.size() != 0)
throw runtime_error(
- "getinfo");
+ "getinfo <pw>");
Object obj;
obj.push_back(Pair("balance", (double)GetBalance() / (double)COIN));
@@ -201,7 +211,7 @@ Value getnewaddress(const Array& params, bool fHelp)
{
if (fHelp || params.size() > 1)
throw runtime_error(
- "getnewaddress [label]\n"
+ "getnewaddress <pw> [label]\n"
"Returns a new bitcoin address for receiving payments. "
"If [label] is specified (recommended), it is added to the address book "
"so payments received with the address will be labeled.");
@@ -223,7 +233,7 @@ Value setlabel(const Array& params, bool fHelp)
{
if (fHelp || params.size() < 1 || params.size() > 2)
throw runtime_error(
- "setlabel <bitcoinaddress> <label>\n"
+ "setlabel <pw> <bitcoinaddress> <label>\n"
"Sets the label associated with the given address.");
string strAddress = params[0].get_str();
@@ -240,7 +250,7 @@ Value getlabel(const Array& params, bool fHelp)
{
if (fHelp || params.size() != 1)
throw runtime_error(
- "getlabel <bitcoinaddress>\n"
+ "getlabel <pw> <bitcoinaddress>\n"
"Returns the label associated with the given address.");
string strAddress = params[0].get_str();
@@ -260,7 +270,7 @@ Value getaddressesbylabel(const Array& params, bool fHelp)
{
if (fHelp || params.size() != 1)
throw runtime_error(
- "getaddressesbylabel <label>\n"
+ "getaddressesbylabel <pw> <label>\n"
"Returns the list of addresses with the given label.");
string strLabel = params[0].get_str();
@@ -290,7 +300,7 @@ Value sendtoaddress(const Array& params, bool fHelp)
{
if (fHelp || params.size() < 2 || params.size() > 4)
throw runtime_error(
- "sendtoaddress <bitcoinaddress> <amount> [comment] [comment-to]\n"
+ "sendtoaddress <pw> <bitcoinaddress> <amount> [comment] [comment-to]\n"
"<amount> is a real and is rounded to the nearest 0.01");
string strAddress = params[0].get_str();
@@ -318,7 +328,7 @@ Value listtransactions(const Array& params, bool fHelp)
{
if (fHelp || params.size() > 2)
throw runtime_error(
- "listtransactions [count=10] [includegenerated=false]\n"
+ "listtransactions <pw> [count=10] [includegenerated=false]\n"
"Returns up to [count] most recent transactions.");
int64 nCount = 10;
@@ -339,7 +349,7 @@ Value getreceivedbyaddress(const Array& params, bool fHelp)
{
if (fHelp || params.size() < 1 || params.size() > 2)
throw runtime_error(
- "getreceivedbyaddress <bitcoinaddress> [minconf=1]\n"
+ "getreceivedbyaddress <pw> <bitcoinaddress> [minconf=1]\n"
"Returns the total amount received by <bitcoinaddress> in transactions with at least [minconf] confirmations.");
// Bitcoin address
@@ -380,7 +390,7 @@ Value getreceivedbylabel(const Array& params, bool fHelp)
{
if (fHelp || params.size() < 1 || params.size() > 2)
throw runtime_error(
- "getreceivedbylabel <label> [minconf=1]\n"
+ "getreceivedbylabel <pw> <label> [minconf=1]\n"
"Returns the total amount received by addresses with <label> in transactions with at least [minconf] confirmations.");
// Get the set of pub keys that have the label
@@ -543,7 +553,7 @@ Value listreceivedbyaddress(const Array& params, bool fHelp)
{
if (fHelp || params.size() > 2)
throw runtime_error(
- "listreceivedbyaddress [minconf=1] [includeempty=false]\n"
+ "listreceivedbyaddress <pw> [minconf=1] [includeempty=false]\n"
"[minconf] is the minimum number of confirmations before payments are included.\n"
"[includeempty] whether to include addresses that haven't received any payments.\n"
"Returns an array of objects containing:\n"
@@ -559,7 +569,7 @@ Value listreceivedbylabel(const Array& params, bool fHelp)
{
if (fHelp || params.size() > 2)
throw runtime_error(
- "listreceivedbylabel [minconf=1] [includeempty=false]\n"
+ "listreceivedbylabel <pw> [minconf=1] [includeempty=false]\n"
"[minconf] is the minimum number of confirmations before payments are included.\n"
"[includeempty] whether to include labels that haven't received any payments.\n"
"Returns an array of objects containing:\n"
@@ -603,7 +613,6 @@ pair<string, rpcfn_type> pCallTable[] =
make_pair("getlabel", &getlabel),
make_pair("getaddressesbylabel", &getaddressesbylabel),
make_pair("sendtoaddress", &sendtoaddress),
- make_pair("listtransactions", &listtransactions),
make_pair("getamountreceived", &getreceivedbyaddress), // deprecated, renamed to getreceivedbyaddress
make_pair("getallreceived", &listreceivedbyaddress), // deprecated, renamed to listreceivedbyaddress
make_pair("getreceivedbyaddress", &getreceivedbyaddress),
@@ -742,6 +751,17 @@ void ThreadRPCServer2(void* parg)
{
printf("ThreadRPCServer started\n");
+ if (mapArgs.count("-rpcpw"))
+ strRPCPassword = mapArgs["-rpcpw"];
+ if (strRPCPassword == "")
+ {
+#if defined(__WXMSW__) && wxUSE_GUI
+ MyMessageBox("Warning: rpc password is blank, use -rpcpw=<password>\n", "Bitcoin", wxOK | wxICON_EXCLAMATION);
+#else
+ fprintf(stdout, "Warning: rpc password is blank, use -rpcpw=<password>\n");
+#endif
+ }
+
// Bind to loopback 127.0.0.1 so the socket can only be accessed locally
boost::asio::io_service io_service;
tcp::endpoint endpoint(boost::asio::ip::address_v4::loopback(), 8332);
@@ -764,7 +784,6 @@ void ThreadRPCServer2(void* parg)
// Receive request
string strRequest = ReadHTTP(stream);
- printf("ThreadRPCServer request=%s", strRequest.c_str());
// Handle multiple invocations per request
string::iterator begin = strRequest.begin();
@@ -787,11 +806,25 @@ void ThreadRPCServer2(void* parg)
const Array& params = find_value(request, "params").get_array();
id = find_value(request, "id");
+ printf("ThreadRPCServer method=%s\n", strMethod.c_str());
+
+ // Check password
+ if (params.size() < 1 || params[0].type() != str_type)
+ throw runtime_error("First parameter must be the password.");
+ if (params[0].get_str() != strRPCPassword)
+ {
+ if (strRPCPassword.size() < 15)
+ Sleep(50);
+ begin = strRequest.end();
+ printf("ThreadRPCServer incorrect password attempt\n");
+ throw runtime_error("Incorrect password.");
+ }
+
// Execute
map<string, rpcfn_type>::iterator mi = mapCallTable.find(strMethod);
if (mi == mapCallTable.end())
throw runtime_error("Method not found.");
- Value result = (*(*mi).second)(params, false);
+ Value result = (*(*mi).second)(Array(params.begin()+1, params.end()), false);
// Send reply
string strReply = JSONRPCReply(result, Value::null, id);