Merge pull request #245 from kanoi/master api.c ensure old mode is always available when not using --api-groups + quit() on param errors
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
diff --git a/api.c b/api.c
index c4a0b9f..c88c915 100644
--- a/api.c
+++ b/api.c
@@ -158,7 +158,7 @@ static char *msg_buffer = NULL;
static SOCKETTYPE sock = INVSOCK;
static const char *UNAVAILABLE = " - API will not be available";
-static const char *GROUPDIS = " - groups will be disabled";
+static const char *INVAPIGROUPS = "Invalid --api-groups parameter";
static const char *BLANK = "";
static const char *COMMA = ",";
@@ -549,8 +549,6 @@ struct APIGROUPS {
char *commands;
} apigroups['Z' - 'A' + 1]; // only A=0 to Z=25 (R: noprivs, W: allprivs)
-static bool groups_enabled = false;
-
static struct IP4ACCESS *ipaccess = NULL;
static int ips = 0;
@@ -2022,7 +2020,7 @@ void notifystatus(int device, struct cgpu_info *cgpu, bool isjson, __maybe_unuse
strcat(io_buffer, buf);
}
-static void notify(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
+static void notify(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, char group)
{
int i;
@@ -2344,6 +2342,7 @@ static void tidyup(__maybe_unused void *arg)
*/
static void setup_groups()
{
+ char *api_groups = opt_api_groups ? opt_api_groups : (char *)BLANK;
char *buf, *ptr, *next, *colon;
char group;
char commands[TMPBUFSIZ];
@@ -2352,11 +2351,11 @@ static void setup_groups()
bool addstar, did;
int i;
- buf = malloc(strlen(opt_api_groups) + 1);
+ buf = malloc(strlen(api_groups) + 1);
if (unlikely(!buf))
quit(1, "Failed to malloc ipgroups buf");
- strcpy(buf, opt_api_groups);
+ strcpy(buf, api_groups);
next = buf;
// for each group defined
@@ -2371,29 +2370,29 @@ static void setup_groups()
colon = strchr(ptr, ':');
if (colon)
*colon = '\0';
- applog(LOG_WARNING, "API invalid group name '%s'%s", ptr, GROUPDIS);
- goto shin;
+ applog(LOG_WARNING, "API invalid group name '%s'", ptr);
+ quit(1, INVAPIGROUPS);
}
group = GROUP(*ptr);
if (!VALIDGROUP(group)) {
- applog(LOG_WARNING, "API invalid group name '%c'%s", *ptr, GROUPDIS);
- goto shin;
+ applog(LOG_WARNING, "API invalid group name '%c'", *ptr);
+ quit(1, INVAPIGROUPS);
}
if (group == PRIVGROUP) {
- applog(LOG_WARNING, "API group name can't be '%c'%s", PRIVGROUP, GROUPDIS);
- goto shin;
+ applog(LOG_WARNING, "API group name can't be '%c'", PRIVGROUP);
+ quit(1, INVAPIGROUPS);
}
if (group == NOPRIVGROUP) {
- applog(LOG_WARNING, "API group name can't be '%c'%s", NOPRIVGROUP, GROUPDIS);
- goto shin;
+ applog(LOG_WARNING, "API group name can't be '%c'", NOPRIVGROUP);
+ quit(1, INVAPIGROUPS);
}
if (apigroups[GROUPOFFSET(group)].commands != NULL) {
- applog(LOG_WARNING, "API duplicate group name '%c'%s", *ptr, GROUPDIS);
- goto shin;
+ applog(LOG_WARNING, "API duplicate group name '%c'", *ptr);
+ quit(1, INVAPIGROUPS);
}
ptr += 2;
@@ -2428,8 +2427,8 @@ static void setup_groups()
*cmd = '\0';
}
} else {
- applog(LOG_WARNING, "API unknown command '%s' in group '%c'%s", ptr, group, GROUPDIS);
- goto shin;
+ applog(LOG_WARNING, "API unknown command '%s' in group '%c'", ptr, group);
+ quit(1, INVAPIGROUPS);
}
}
@@ -2480,8 +2479,6 @@ static void setup_groups()
// W (PRIVGROUP) is handled as a special case since it simply means all commands
- groups_enabled = true;
-shin:
free(buf);
return;
}
@@ -2648,16 +2645,12 @@ void api(int api_thr_id)
pthread_cleanup_push(tidyup, NULL);
my_thr_id = api_thr_id;
- /* This should be done first to ensure curl has already called WSAStartup() in windows */
- sleep(opt_log_interval);
-
if (!opt_api_listen) {
applog(LOG_DEBUG, "API not running%s", UNAVAILABLE);
return;
}
- if (opt_api_groups)
- setup_groups();
+ setup_groups();
if (opt_api_allow) {
setup_ipaccess();
@@ -2668,6 +2661,10 @@ void api(int api_thr_id)
}
}
+ /* This should be done before curl in needed
+ * to ensure curl has already called WSAStartup() in windows */
+ sleep(opt_log_interval);
+
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == INVSOCK) {
applog(LOG_ERR, "API1 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
@@ -2739,14 +2736,13 @@ void api(int api_thr_id)
addrok = false;
group = NOPRIVGROUP;
if (opt_api_allow) {
- if (groups_enabled)
- for (i = 0; i < ips; i++) {
- if ((cli.sin_addr.s_addr & ipaccess[i].mask) == ipaccess[i].ip) {
- addrok = true;
- group = ipaccess[i].group;
- break;
- }
+ for (i = 0; i < ips; i++) {
+ if ((cli.sin_addr.s_addr & ipaccess[i].mask) == ipaccess[i].ip) {
+ addrok = true;
+ group = ipaccess[i].group;
+ break;
}
+ }
} else {
if (opt_api_network)
addrok = true;