Merge pull request #107 from kanoi/master Allow API to restrict access by IP address + other commits
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
diff --git a/README b/README
index 47e0c78..b592c4b 100644
--- a/README
+++ b/README
@@ -119,6 +119,8 @@ Usage instructions: Run "cgminer --help" to see options:
Usage: . [-atDdGCgIKklmpPQqrRsTouvwOchnV]
Options for both config file and command line:
+--api-allow Allow API access (if enabled) only to the given list of IP[/Prefix] address[/subnets]
+ This overrides --api-network and you must specify 127.0.0.1 if it is required
--api-description Description placed in the API status header (default: cgminer version)
--api-listen Listen for API requests (default: disabled)
--api-network Allow API (if enabled) to listen on/for any address (default: only 127.0.0.1)
@@ -526,15 +528,24 @@ cgminer shuts down because of this.
---
-API
+RPC API
If you start cgminer with the "--api-listen" option, it will listen on a
simple TCP/IP socket for single string API requests from the same machine
running cgminer and reply with a string and then close the socket each time
-Also, if you add the "--api-network" option, it will accept API requests
-from any network attached computer.
+If you add the "--api-network" option, it will accept API requests from any
+network attached computer.
-The request can be either simple text or JSON.
+You can specify IP addresses/prefixes that are only allowed to access the API
+with the "--api-access" option e.g. --api-access 192.168.0.1,10.0.0/24
+will allow 192.168.0.1 or any address matching 10.0.0.*, but nothing else
+IP addresses are automatically padded with extra '.0's as needed
+Without a /prefix is the same as specifying /32
+Using the "--api-access" option overides the "--api-network" option if they
+are both specified
+With "--api-access", 127.0.0.1 is not by default given access unless specified
+
+The RPC API request can be either simple text or JSON.
If the request is JSON (starts with '{'), it will reply with a JSON formatted
response, otherwise it replies with text formatted as described further below.
@@ -544,7 +555,7 @@ The JSON request format required is '{"command":"CMD","parameter":"PARAM"}'
where "CMD" is from the "Request" column below and "PARAM" would be e.g.
the CPU/GPU number if required.
-An example request in both formats:
+An example request in both formats to set GPU 0 fan to 80%:
gpufan|0,80
{"command":"gpufan","parameter":"0,80"}
@@ -652,6 +663,9 @@ The list of requests and replies are:
When you enable, disable or restart a GPU, you will also get Thread messages in
the cgminer status window
+When you switch to a different pool to the current one, you will get a
+'Switching to URL' message in the cgminer status windows
+
Obviously, the JSON format is simply just the names as given before the '='
with the values after the '='
diff --git a/api.c b/api.c
index 12f9d93..caab7b1 100644
--- a/api.c
+++ b/api.c
@@ -126,6 +126,10 @@
#ifndef SHUT_RDWR
#define SHUT_RDWR SD_BOTH
#endif
+
+ #ifndef in_addr_t
+ #define in_addr_t uint32_t
+ #endif
#endif
// Big enough for largest API request
@@ -343,6 +347,14 @@ struct CODES {
static int bye = 0;
static bool ping = true;
+struct IP4ACCESS {
+ in_addr_t ip;
+ in_addr_t mask;
+};
+
+static struct IP4ACCESS *ipaccess = NULL;
+static int ips = 0;
+
// All replies (except BYE) start with a message
// thus for JSON, message() inserts JSON_START at the front
// and send_result() adds JSON_END at the end
@@ -1205,6 +1217,11 @@ static void tidyup()
sock = INVSOCK;
}
+ if (ipaccess != NULL) {
+ free(ipaccess);
+ ipaccess = NULL;
+ }
+
if (msg_buffer != NULL) {
free(msg_buffer);
msg_buffer = NULL;
@@ -1216,6 +1233,89 @@ static void tidyup()
}
}
+/*
+ * Interpret IP[/Prefix][,IP2[/Prefix2][,...]] --api-allow option
+ *
+ * N.B. IP4 addresses are by Definition 32bit big endian on all platforms
+ */
+static void setup_ipaccess()
+{
+ char *buf, *ptr, *comma, *slash, *dot;
+ int ipcount, mask, octet, i;
+
+ buf = malloc(strlen(opt_api_allow) + 1);
+ if (unlikely(!buf))
+ quit(1, "Failed to malloc ipaccess buf");
+
+ strcpy(buf, opt_api_allow);
+
+ ipcount = 1;
+ ptr = buf;
+ while (*ptr)
+ if (*(ptr++) == ',')
+ ipcount++;
+
+ // possibly more than needed, but never less
+ ipaccess = calloc(ipcount, sizeof(struct IP4ACCESS));
+ if (unlikely(!ipaccess))
+ quit(1, "Failed to calloc ipaccess");
+
+ ips = 0;
+ ptr = buf;
+ while (ptr && *ptr) {
+ while (*ptr == ' ' || *ptr == '\t')
+ ptr++;
+
+ if (*ptr == ',') {
+ ptr++;
+ continue;
+ }
+
+ comma = strchr(ptr, ',');
+ if (comma)
+ *(comma++) = '\0';
+
+ slash = strchr(ptr, '/');
+ if (!slash)
+ ipaccess[ips].mask = 0xffffffff;
+ else {
+ *(slash++) = '\0';
+ mask = atoi(slash);
+ if (mask < 1 || mask > 32)
+ goto popipo; // skip invalid/zero
+
+ ipaccess[ips].mask = 0;
+ while (mask-- >= 0) {
+ octet = 1 << (mask % 8);
+ ipaccess[ips].mask |= (octet << (8 * (mask >> 3)));
+ }
+ }
+
+ ipaccess[ips].ip = 0; // missing default to '.0'
+ for (i = 0; ptr && (i < 4); i++) {
+ dot = strchr(ptr, '.');
+ if (dot)
+ *(dot++) = '\0';
+
+ octet = atoi(ptr);
+ if (octet < 0 || octet > 0xff)
+ goto popipo; // skip invalid
+
+ ipaccess[ips].ip |= (octet << (i * 8));
+
+ ptr = dot;
+ }
+
+ ipaccess[ips].ip &= ipaccess[ips].mask;
+
+ ips++;
+popipo:
+ ptr = comma;
+ }
+
+ free(buf);
+}
+
void api(void)
{
char buf[BUFSIZ];
@@ -1248,6 +1348,15 @@ void api(void)
return;
}
+ if (opt_api_allow) {
+ setup_ipaccess();
+
+ if (ips == 0) {
+ applog(LOG_WARNING, "API not running (no valid IPs specified)%s", UNAVAILABLE);
+ return;
+ }
+ }
+
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == INVSOCK) {
applog(LOG_ERR, "API1 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
@@ -1258,9 +1367,9 @@ void api(void)
serv.sin_family = AF_INET;
- if (!opt_api_network) {
+ if (!opt_api_allow && !opt_api_network) {
serv.sin_addr.s_addr = inet_addr(localaddr);
- if (serv.sin_addr.s_addr == INVINETADDR) {
+ if (serv.sin_addr.s_addr == (in_addr_t)INVINETADDR) {
applog(LOG_ERR, "API2 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
return;
}
@@ -1296,10 +1405,14 @@ void api(void)
return;
}
- if (opt_api_network)
- applog(LOG_WARNING, "API running in UNRESTRICTED access mode");
- else
- applog(LOG_WARNING, "API running in restricted access mode");
+ if (opt_api_allow)
+ applog(LOG_WARNING, "API running in IP access mode");
+ else {
+ if (opt_api_network)
+ applog(LOG_WARNING, "API running in UNRESTRICTED access mode");
+ else
+ applog(LOG_WARNING, "API running in local access mode");
+ }
io_buffer = malloc(MYBUFSIZ+1);
msg_buffer = malloc(MYBUFSIZ+1);
@@ -1311,11 +1424,21 @@ void api(void)
goto die;
}
- if (opt_api_network)
- addrok = true;
- else {
- connectaddr = inet_ntoa(cli.sin_addr);
- addrok = (strcmp(connectaddr, localaddr) == 0);
+ addrok = false;
+ if (opt_api_allow) {
+ for (i = 0; i < ips; i++) {
+ if ((cli.sin_addr.s_addr & ipaccess[i].mask) == ipaccess[i].ip) {
+ addrok = true;
+ break;
+ }
+ }
+ } else {
+ if (opt_api_network)
+ addrok = true;
+ else {
+ connectaddr = inet_ntoa(cli.sin_addr);
+ addrok = (strcmp(connectaddr, localaddr) == 0);
+ }
}
if (opt_debug) {
diff --git a/cgminer.c b/cgminer.c
index a1ebb55..26a1580 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -127,6 +127,7 @@ static bool opt_fail_only;
bool opt_autofan;
bool opt_autoengine;
bool opt_noadl;
+char *opt_api_allow = NULL;
char *opt_api_description = PACKAGE_STRING;
int opt_api_port = 4028;
bool opt_api_listen = false;
@@ -536,6 +537,13 @@ static char *set_schedtime(const char *arg, struct schedtime *st)
return NULL;
}
+static char *set_api_allow(const char *arg)
+{
+ opt_set_charp(arg, &opt_api_allow);
+
+ return NULL;
+}
+
static char *set_api_description(const char *arg)
{
opt_set_charp(arg, &opt_api_description);
@@ -575,6 +583,9 @@ static struct opt_table opt_config_table[] = {
#endif
),
#endif
+ OPT_WITH_ARG("--api-allow",
+ set_api_allow, NULL, NULL,
+ "Allow API access only to the given list of IP[/Prefix] addresses[/subnets]"),
OPT_WITH_ARG("--api-description",
set_api_description, NULL, NULL,
"Description placed in the API status header, default: cgminer version"),
@@ -2301,6 +2312,8 @@ void write_config(FILE *fcfg)
for (i = 0; i < nDevs; i++)
if (gpus[i].enabled)
fprintf(fcfg, ",\n\"device\" : \"%d\"", i);
+ if (opt_api_allow != NULL)
+ fprintf(fcfg, ",\n\"api-allow\" : \"%s\"", opt_api_allow);
if (strcmp(opt_api_description, PACKAGE_STRING) != 0)
fprintf(fcfg, ",\n\"api-description\" : \"%s\"", opt_api_description);
fputs("\n}", fcfg);
diff --git a/device-cpu.c b/device-cpu.c
index d1eb4eb..be658d2 100644
--- a/device-cpu.c
+++ b/device-cpu.c
@@ -23,9 +23,9 @@
#include <sys/stat.h>
#include <sys/types.h>
-#include <sys/wait.h>
#ifndef WIN32
+#include <sys/wait.h>
#include <sys/resource.h>
#endif
#include <libgen.h>
diff --git a/miner.h b/miner.h
index 42c0a16..63765a3 100644
--- a/miner.h
+++ b/miner.h
@@ -446,6 +446,7 @@ extern char *cgminer_path;
extern bool opt_autofan;
extern bool opt_autoengine;
extern bool use_curses;
+extern char *opt_api_allow;
extern char *opt_api_description;
extern int opt_api_port;
extern bool opt_api_listen;
diff --git a/miner.php b/miner.php
index 8ba2b4e..b7d2f19 100644
--- a/miner.php
+++ b/miner.php
@@ -17,8 +17,9 @@ td.sta { color:green; font-family:verdana,arial,sans; font-size:14pt; }
</head><body bgcolor=#ecffff>
<script type='text/javascript'>
function pr(a,m){if(m!=null){if(!confirm(m+'?'))return}window.location="<? echo $here ?>"+a}
-function prs(a){var c=a.substr(3);var z=c.split('|',2);var m=z[0].substr(0,1).toUpperCase()+z[0].substr(1)+' GPU '+z[1];pr('?arg='+a,m)}
-function prs2(a,n){var v=document.getElementById('gi'+n).value;var c=a.substr(3);var z=c.split('|',2);var m='Set GPU '+z[1]+' '+z[0].substr(0,1).toUpperCase()+z[0].substr(1)+' to '+v;pr('?arg='+a+','+v,m)}
+function prc(a,m){pr('?arg='+a,m)}
+function prs(a){var c=a.substr(3);var z=c.split('|',2);var m=z[0].substr(0,1).toUpperCase()+z[0].substr(1)+' GPU '+z[1];prc(a,m)}
+function prs2(a,n){var v=document.getElementById('gi'+n).value;var c=a.substr(3);var z=c.split('|',2);var m='Set GPU '+z[1]+' '+z[0].substr(0,1).toUpperCase()+z[0].substr(1)+' to '+v;prc(a+','+v,m)}
</script>
<table width=100% height=100% border=0 cellpadding=0 cellspacing=0 summary='Mine'>
<tr><td align=center valign=top>
@@ -201,7 +202,7 @@ function fmt($section, $name, $value)
return $value;
}
#
-function details($list)
+function details($cmd, $list)
{
$stas = array('S' => 'Success', 'W' => 'Warning', 'I' => 'Informational', 'E' => 'Error', 'F' => 'Fatal');
@@ -243,6 +244,9 @@ function details($list)
echo "<td valign=bottom class=h>$name</td>";
}
+ if ($cmd == 'pools')
+ echo "<td valign=bottom class=h>Switch</td>";
+
echo '</tr>';
break;
@@ -257,6 +261,23 @@ function details($list)
foreach ($values as $name => $value)
echo '<td>'.fmt($section, $name, $value).'</td>';
+ if ($cmd == 'pools')
+ {
+ echo '<td>';
+
+ reset($values);
+ $pool = current($values);
+ if ($pool === false)
+ echo ' ';
+ else
+ {
+ echo "<input type=button value='Pool $pool'";
+ echo " onclick='prc(\"switchpool|$pool\",\"Switch to Pool $pool\")'>";
+ }
+
+ echo '</td>';
+ }
+
echo '</tr>';
}
echo $te;
@@ -369,7 +390,7 @@ function process($cmds, $rd, $ro)
}
else
{
- details($process);
+ details($cmd, $process);
echo '<tr><td><br><br></td></tr>';
if ($cmd == 'devs')
$devs = $process;
@@ -389,7 +410,7 @@ function display()
echo "<tr><td><table cellpadding=0 cellspacing=0 border=0><tr><td>";
echo "<input type=button value='Refresh' onclick='pr(\"\",null)'>";
echo "</td><td width=100%> </td><td>";
- echo "<input type=button value='Quit' onclick='pr(\"?arg=quit\",\"Quit CGMiner\")'>";
+ echo "<input type=button value='Quit' onclick='prc(\"quit\",\"Quit CGMiner\")'>";
echo "</td></tr></table></td></tr>";
$arg = trim(getparam('arg', true));