Skip to content

Commit 9fc8abf

Browse files
committed
stratum: password authentication
1 parent 4c00de9 commit 9fc8abf

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

src/datum_conf.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,10 @@ const T_DATUM_CONFIG_ITEM datum_config_options[] = {
110110
.required = false, .ptr = &datum_config.stratum_v1_idle_timeout_no_share, .default_int = 7200 },
111111
{ .var_type = DATUM_CONF_INT, .category = "stratum", .name = "idle_timeout_max_last_work", .description = "Seconds we allow a subscribed connection to be idle since its last accepted share? (0 disables)",
112112
.required = false, .ptr = &datum_config.stratum_v1_idle_timeout_max_last_work, .default_int = 0 },
113-
{ .var_type = DATUM_CONF_USERNAME_MODS, .category = "stratum", .name = "username_modifiers", .description = "Modifiers to redirect some portion of shares to alternate usernames", .required = false, .ptr = &datum_config.stratum_username_mod, },
113+
{ .var_type = DATUM_CONF_STRING, .category = "stratum", .name = "password", .description = "Authentication password (disabled if blank)",
114+
.required = false, .ptr = datum_config.stratum_v1_password, .default_string[0] = "", .max_string_len = sizeof(datum_config.stratum_v1_password) },
115+
{ .var_type = DATUM_CONF_USERNAME_MODS, .category = "stratum", .name = "username_modifiers", .description = "Modifiers to redirect some portion of shares to alternate usernames",
116+
.required = false, .ptr = &datum_config.stratum_username_mod, },
114117

115118
// mining settings
116119
{ .var_type = DATUM_CONF_STRING, .category = "mining", .name = "pool_address", .description = "Bitcoin address used for mining rewards.",
@@ -588,6 +591,8 @@ int datum_read_config(const char *conffile) {
588591
return 0;
589592
}
590593

594+
datum_config.stratum_v1_password_len = strlen(datum_config.stratum_v1_password);
595+
591596
if (datum_config.datum_protocol_global_timeout < (datum_config.bitcoind_work_update_seconds+5)) {
592597
DLOG_FATAL("DATUM protocol global timeout must be at least the work update interval plus 5 seconds.");
593598
return 0;

src/datum_conf.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ typedef struct {
121121
int stratum_v1_idle_timeout_no_subscribe;
122122
int stratum_v1_idle_timeout_no_share;
123123
int stratum_v1_idle_timeout_max_last_work;
124+
char stratum_v1_password[72];
125+
size_t stratum_v1_password_len;
124126

125127
void *stratum_username_mod;
126128

src/datum_stratum.c

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,7 +1444,9 @@ int client_mining_configure(T_DATUM_CLIENT_DATA *c, uint64_t id, json_t *params_
14441444
int client_mining_authorize(T_DATUM_CLIENT_DATA *c, uint64_t id, json_t *params_obj) {
14451445
char s[256];
14461446
const char *username_s;
1447+
const char *password_s;
14471448
json_t *username;
1449+
json_t *password;
14481450

14491451
T_DATUM_MINER_DATA * const m = c->app_client_data;
14501452

@@ -1461,10 +1463,28 @@ int client_mining_authorize(T_DATUM_CLIENT_DATA *c, uint64_t id, json_t *params_
14611463
strncpy(m->last_auth_username, username_s, sizeof(m->last_auth_username) - 1);
14621464
m->last_auth_username[sizeof(m->last_auth_username)-1] = 0;
14631465

1464-
snprintf(s, sizeof(s), "{\"error\":null,\"id\":%"PRIu64",\"result\":true}\n", id);
1465-
datum_socket_send_string_to_client(c, s);
1466+
password = json_array_get(params_obj, 1);
1467+
if (!password) {
1468+
password_s = (const char *)"x";
1469+
} else {
1470+
password_s = json_string_value(password);
1471+
if (!password_s) {
1472+
password_s = (const char *)"x";
1473+
}
1474+
}
14661475

1467-
m->authorized = true;
1476+
if (datum_config.stratum_v1_password_len > 0 && !datum_secure_strequals(datum_config.stratum_v1_password, datum_config.stratum_v1_password_len, password_s)) {
1477+
DLOG_DEBUG("Kicking client %d/%d (%s) due to unsuccessful authentication attempt", c->datum_thread->thread_id, c->cid, c->rem_host);
1478+
snprintf(s, sizeof(s), "{\"error\":null,\"id\":%"PRIu64",\"result\":false}\n", id);
1479+
c->datum_thread->has_client_kill_request = true;
1480+
c->kill_request = true;
1481+
m->authorized = false;
1482+
} else {
1483+
snprintf(s, sizeof(s), "{\"error\":null,\"id\":%"PRIu64",\"result\":true}\n", id);
1484+
m->authorized = true;
1485+
}
1486+
1487+
datum_socket_send_string_to_client(c, s);
14681488

14691489
return 0;
14701490
}

0 commit comments

Comments
 (0)