Skip to content

Commit 9bfb2f6

Browse files
committed
Merge remote-tracking branch 'github-pull/46/head'
2 parents 44f0366 + 2b7442b commit 9bfb2f6

File tree

6 files changed

+83
-80
lines changed

6 files changed

+83
-80
lines changed

src/datum_blocktemplates.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,6 @@ T_DATUM_TEMPLATE_DATA *datum_gbt_parser(json_t *gbt) {
334334

335335
void *datum_gateway_fallback_notifier(void *args) {
336336
CURL *tcurl = NULL;
337-
char userpass[512];
338337
char req[512];
339338
char p1[72];
340339
p1[0] = 0;
@@ -348,11 +347,9 @@ void *datum_gateway_fallback_notifier(void *args) {
348347
}
349348
DLOG_DEBUG("Fallback notifier thread ready.");
350349

351-
sprintf(userpass, "%s:%s", datum_config.bitcoind_rpcuser, datum_config.bitcoind_rpcpassword);
352-
353350
while(1) {
354-
sprintf(req, "{\"jsonrpc\":\"1.0\",\"id\":\"%"PRIu64"\",\"method\":\"getbestblockhash\",\"params\":[]}", current_time_millis());
355-
gbbh = json_rpc_call(tcurl, datum_config.bitcoind_rpcurl, userpass, req);
351+
snprintf(req, sizeof(req), "{\"jsonrpc\":\"1.0\",\"id\":\"%"PRIu64"\",\"method\":\"getbestblockhash\",\"params\":[]}", current_time_millis());
352+
gbbh = json_rpc_call(tcurl, datum_config.bitcoind_rpcurl, datum_config.bitcoind_rpcuserpass, req);
356353
if (gbbh) {
357354
res_val = json_object_get(gbbh, "result");
358355
if (!res_val) {
@@ -386,7 +383,6 @@ void *datum_gateway_template_thread(void *args) {
386383
uint64_t i = 0;
387384
char gbt_req[1024];
388385
int j;
389-
char userpass[512];
390386
T_DATUM_TEMPLATE_DATA *t;
391387
bool was_notified = false;
392388
int wnc = 0;
@@ -414,14 +410,12 @@ void *datum_gateway_template_thread(void *args) {
414410
char p1[72];
415411
p1[0] = 0;
416412

417-
sprintf(userpass, "%s:%s", datum_config.bitcoind_rpcuser, datum_config.bitcoind_rpcpassword);
418-
419413
while(1) {
420414
i++;
421415

422416
// fetch latest template
423-
sprintf(gbt_req, "{\"method\":\"getblocktemplate\",\"params\":[{\"rules\":[\"segwit\"]}],\"id\":%"PRIu64"}",(uint64_t)((uint64_t)time(NULL)<<(uint64_t)8)|(uint64_t)(i&255));
424-
gbt = json_rpc_call(tcurl, datum_config.bitcoind_rpcurl, userpass, gbt_req);
417+
snprintf(gbt_req, sizeof(gbt_req), "{\"method\":\"getblocktemplate\",\"params\":[{\"rules\":[\"segwit\"]}],\"id\":%"PRIu64"}",(uint64_t)((uint64_t)time(NULL)<<(uint64_t)8)|(uint64_t)(i&255));
418+
gbt = json_rpc_call(tcurl, datum_config.bitcoind_rpcurl, datum_config.bitcoind_rpcuserpass, gbt_req);
425419

426420
if (!gbt) {
427421
DLOG_ERROR("Could not fetch new template from %s!", datum_config.bitcoind_rpcurl);

src/datum_conf.c

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ const char *datum_conf_var_type_text[] = { "N/A", "boolean", "integer", "string"
5353
const T_DATUM_CONFIG_ITEM datum_config_options[] = {
5454
// Bitcoind configs
5555
{ .var_type = DATUM_CONF_STRING, .category = "bitcoind", .name = "rpcuser", .description = "RPC username for communication with local bitcoind.",
56-
.required = true, .ptr = datum_config.bitcoind_rpcuser, .max_string_len = 128 },
56+
.required = true, .ptr = datum_config.bitcoind_rpcuser, .max_string_len = sizeof(datum_config.bitcoind_rpcuser) },
5757
{ .var_type = DATUM_CONF_STRING, .category = "bitcoind", .name = "rpcpassword", .description = "RPC password for communication with local bitcoind.",
58-
.required = true, .ptr = datum_config.bitcoind_rpcpassword, .max_string_len = 128 },
58+
.required = true, .ptr = datum_config.bitcoind_rpcpassword, .max_string_len = sizeof(datum_config.bitcoind_rpcpassword) },
5959
{ .var_type = DATUM_CONF_STRING, .category = "bitcoind", .name = "rpcurl", .description = "RPC URL for communication with local bitcoind. (GBT Template Source)",
60-
.required = true, .ptr = datum_config.bitcoind_rpcurl, .max_string_len = 128 },
60+
.required = true, .ptr = datum_config.bitcoind_rpcurl, .max_string_len = sizeof(datum_config.bitcoind_rpcurl) },
6161
{ .var_type = DATUM_CONF_INT, .category = "bitcoind", .name = "work_update_seconds", .description = "How many seconds between normal work updates? (5-120, 40 suggested)",
6262
.required = false, .ptr = &datum_config.bitcoind_work_update_seconds, .default_int = 40 },
6363
{ .var_type = DATUM_CONF_BOOL, .category = "bitcoind", .name = "notify_fallback", .description = "Fall back to less efficient methods for new block notifications. Can disable if you use blocknotify.",
@@ -93,23 +93,23 @@ const T_DATUM_CONFIG_ITEM datum_config_options[] = {
9393

9494
// mining settings
9595
{ .var_type = DATUM_CONF_STRING, .category = "mining", .name = "pool_address", .description = "Bitcoin address used for mining rewards.",
96-
.required = true, .ptr = datum_config.mining_pool_address, .max_string_len = 128 },
96+
.required = true, .ptr = datum_config.mining_pool_address, .max_string_len = sizeof(datum_config.mining_pool_address) },
9797
{ .var_type = DATUM_CONF_STRING, .category = "mining", .name = "coinbase_tag_primary", .description = "Text to have in the primary coinbase tag when not using pool (overridden by DATUM Pool)",
98-
.required = false, .ptr = datum_config.mining_coinbase_tag_primary, .default_string[0] = "DATUM Gateway", .max_string_len = 64 },
98+
.required = false, .ptr = datum_config.mining_coinbase_tag_primary, .default_string[0] = "DATUM Gateway", .max_string_len = sizeof(datum_config.mining_coinbase_tag_primary) },
9999
{ .var_type = DATUM_CONF_STRING, .category = "mining", .name = "coinbase_tag_secondary", .description = "Text to have in the secondary coinbase tag (Short name/identifier)",
100-
.required = false, .ptr = datum_config.mining_coinbase_tag_secondary, .default_string[0] = "DATUM User", .max_string_len = 64 },
100+
.required = false, .ptr = datum_config.mining_coinbase_tag_secondary, .default_string[0] = "DATUM User", .max_string_len = sizeof(datum_config.mining_coinbase_tag_secondary) },
101101
{ .var_type = DATUM_CONF_INT, .category = "mining", .name = "coinbase_unique_id", .description = "A unique ID between 1 and 65535. This is appended to the coinbase. Make unique per instance of datum with the same coinbase tags.",
102102
.required = false, .ptr = &datum_config.coinbase_unique_id, .default_int = 4242 },
103103
{ .var_type = DATUM_CONF_STRING, .category = "mining", .name = "save_submitblocks_dir", .description = "Directory to save all submitted blocks to as submitblock JSON files",
104-
.required = false, .ptr = datum_config.mining_save_submitblocks_dir, .default_string[0] = "", .max_string_len = 256 },
104+
.required = false, .ptr = datum_config.mining_save_submitblocks_dir, .default_string[0] = "", .max_string_len = sizeof(datum_config.mining_save_submitblocks_dir) },
105105

106106
// API/dashboard
107107
{ .var_type = DATUM_CONF_INT, .category = "api", .name = "listen_port", .description = "Port to listen for API/dashboard requests (0=disabled)",
108108
.required = false, .ptr = &datum_config.api_listen_port, .default_int = 0 },
109109

110110
// extra block submissions list
111111
{ .var_type = DATUM_CONF_STRING_ARRAY, .category = "extra_block_submissions", .name = "urls", .description = "Array of bitcoind RPC URLs to submit our blocks to directly. Include auth info: http://user:pass@IP",
112-
.required = false, .ptr = datum_config.extra_block_submissions_urls[0], .max_string_len = 512 },
112+
.required = false, .ptr = datum_config.extra_block_submissions_urls[0], .max_string_len = sizeof(*datum_config.extra_block_submissions_urls) },
113113

114114
// logger
115115
{ .var_type = DATUM_CONF_BOOL, .category = "logger", .name = "log_to_console", .description = "Enable logging of messages to the console",
@@ -119,7 +119,7 @@ const T_DATUM_CONFIG_ITEM datum_config_options[] = {
119119
{ .var_type = DATUM_CONF_BOOL, .category = "logger", .name = "log_to_file", .description = "Enable logging of messages to a file",
120120
.required = false, .ptr = &datum_config.clog_to_file, .default_bool = false },
121121
{ .var_type = DATUM_CONF_STRING, .category = "logger", .name = "log_file", .description = "Path to file to write log messages, when enabled",
122-
.required = false, .ptr = datum_config.clog_file, .default_string[0] = "", .max_string_len = 1023 },
122+
.required = false, .ptr = datum_config.clog_file, .default_string[0] = "", .max_string_len = sizeof(datum_config.clog_file) },
123123
{ .var_type = DATUM_CONF_BOOL, .category = "logger", .name = "log_rotate_daily", .description = "Rotate the message log file at midnight",
124124
.required = false, .ptr = &datum_config.clog_rotate_daily, .default_bool = true },
125125
{ .var_type = DATUM_CONF_BOOL, .category = "logger", .name = "log_calling_function", .description = "Log the name of the calling function when logging",
@@ -131,11 +131,11 @@ const T_DATUM_CONFIG_ITEM datum_config_options[] = {
131131

132132
// datum options
133133
{ .var_type = DATUM_CONF_STRING, .category = "datum", .name = "pool_host", .description = "Remote DATUM server host/ip to use for decentralized pooled mining (set to \"\" to disable pooled mining)",
134-
.required = false, .ptr = datum_config.datum_pool_host, .default_string[0] = "datum-beta1.mine.ocean.xyz", .max_string_len = 1023 },
134+
.required = false, .ptr = datum_config.datum_pool_host, .default_string[0] = "datum-beta1.mine.ocean.xyz", .max_string_len = sizeof(datum_config.datum_pool_host) },
135135
{ .var_type = DATUM_CONF_INT, .category = "datum", .name = "pool_port", .description = "Remote DATUM server port",
136136
.required = false, .ptr = &datum_config.datum_pool_port, .default_int = 28915 },
137137
{ .var_type = DATUM_CONF_STRING, .category = "datum", .name = "pool_pubkey", .description = "Public key of the DATUM server for initiating encrypted connection. Get from secure location, or set to empty to auto-fetch.",
138-
.required = false, .ptr = datum_config.datum_pool_pubkey, .default_string[0] = "f21f2f0ef0aa1970468f22bad9bb7f4535146f8e4a8f646bebc93da3d89b1406f40d032f09a417d94dc068055df654937922d2c89522e3e8f6f0e649de473003", .max_string_len = 1023 },
138+
.required = false, .ptr = datum_config.datum_pool_pubkey, .default_string[0] = "f21f2f0ef0aa1970468f22bad9bb7f4535146f8e4a8f646bebc93da3d89b1406f40d032f09a417d94dc068055df654937922d2c89522e3e8f6f0e649de473003", .max_string_len = sizeof(datum_config.datum_pool_pubkey) },
139139
{ .var_type = DATUM_CONF_BOOL, .category = "datum", .name = "pool_pass_workers", .description = "Pass stratum miner usernames as sub-worker names to the pool (pool_username.miner's username)",
140140
.required = false, .ptr = &datum_config.datum_pool_pass_workers, .default_bool = true },
141141
{ .var_type = DATUM_CONF_BOOL, .category = "datum", .name = "pool_pass_full_users", .description = "Pass stratum miner usernames as raw usernames to the pool (use if putting multiple payout addresses on miners behind this gateway)",
@@ -221,8 +221,11 @@ int datum_config_parse_value(const T_DATUM_CONFIG_ITEM *c, json_t *item) {
221221
return 1;
222222
}
223223
if (!json_is_string(item)) return -1;
224-
strncpy((char *)c->ptr, json_string_value(item), c->max_string_len-1);
225-
((char *)c->ptr)[c->max_string_len-1] = 0;
224+
// check for overflow
225+
int written = snprintf((char *)c->ptr, c->max_string_len, "%s", json_string_value(item));
226+
if (written >= c->max_string_len) {
227+
return -2;
228+
}
226229
return 1;
227230
}
228231

@@ -286,15 +289,22 @@ int datum_read_config(const char *conffile) {
286289

287290
// item might be valid
288291
j = datum_config_parse_value(&datum_config_options[i], item);
289-
if (j != 1) {
292+
if (j == -1) {
290293
DLOG_ERROR("Could not parse configuration option %s.%s. Type should be %s", datum_config_options[i].category, datum_config_options[i].name, (datum_config_options[i].var_type<DATUM_CONF_TYPES)?datum_conf_var_type_text[datum_config_options[i].var_type]:"UNKNOWN");
294+
return -1;
295+
} else if (j == -2) {
296+
DLOG_ERROR("Configuration option %s.%s exceeds maximum length of %d", datum_config_options[i].category, datum_config_options[i].name, datum_config_options[i].max_string_len);
297+
return -1;
291298
}
292299
}
293300

294301
if (config) {
295302
json_decref(config);
296303
}
297304

305+
// populate userpass for further reuse
306+
snprintf(datum_config.bitcoind_rpcuserpass, sizeof(datum_config.bitcoind_rpcuserpass), "%s:%s", datum_config.bitcoind_rpcuser, datum_config.bitcoind_rpcpassword);
307+
298308
// pass configuration options to the logger
299309
datum_logger_config(datum_config.clog_to_file, datum_config.clog_to_console, datum_config.clog_level_console, datum_config.clog_level_file, datum_config.clog_calling_function, datum_config.clog_to_stderr, datum_config.clog_rotate_daily, datum_config.clog_file);
300310

src/datum_conf.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
#define DATUM_CONFIG_MAX_ARRAY_ENTRIES 32
4040
#define DATUM_MAX_BLOCK_SUBMITS DATUM_CONFIG_MAX_ARRAY_ENTRIES
41-
#define DATUM_CONFIG_MAX_STRING_ARRAY_LEN 1024
41+
#define DATUM_MAX_SUBMIT_URL_LEN 512
4242

4343
#include <stdbool.h>
4444
#include <stdint.h>
@@ -67,8 +67,9 @@ typedef struct {
6767

6868
// Globally accessable config options
6969
typedef struct {
70-
char bitcoind_rpcuser[256];
71-
char bitcoind_rpcpassword[256];
70+
char bitcoind_rpcuserpass[256];
71+
char bitcoind_rpcuser[128];
72+
char bitcoind_rpcpassword[128];
7273
char bitcoind_rpcurl[256];
7374
int bitcoind_work_update_seconds;
7475
bool bitcoind_notify_fallback;
@@ -89,15 +90,15 @@ typedef struct {
8990
int stratum_v1_idle_timeout_max_last_work;
9091

9192
char mining_pool_address[256];
92-
char mining_coinbase_tag_primary[256];
93-
char mining_coinbase_tag_secondary[256];
93+
char mining_coinbase_tag_primary[64];
94+
char mining_coinbase_tag_secondary[64];
9495
char mining_save_submitblocks_dir[256];
9596
int coinbase_unique_id;
9697

9798
int api_listen_port;
9899

99100
int extra_block_submissions_count;
100-
char extra_block_submissions_urls[DATUM_MAX_BLOCK_SUBMITS][DATUM_CONFIG_MAX_STRING_ARRAY_LEN];
101+
char extra_block_submissions_urls[DATUM_MAX_BLOCK_SUBMITS][DATUM_MAX_SUBMIT_URL_LEN];
101102

102103
bool clog_to_file;
103104
bool clog_to_console;

src/datum_logger.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ int datum_logger_queue_msg(const char *func, int level, const char *format, ...)
118118
va_list args;
119119
struct timeval tv;
120120
struct tm tm_info;
121-
char time_buffer[40];
121+
char time_buffer[20];
122122

123123
if ((level < log_level_console) && (level < log_level_file)) {
124124
return 0;
@@ -202,7 +202,12 @@ int datum_logger_queue_msg(const char *func, int level, const char *format, ...)
202202
msg->msg = &msg_buffer[buffer_id][msg_buf_idx[buffer_id]];
203203
va_start(args, format);
204204
i = vsnprintf(msg->msg, 1023, format, args);
205-
msg->msg[i] = 0;
205+
206+
// clamp i to actual written value in order not to waste buffer space
207+
if (i >= 1023) {
208+
i = 1022;
209+
}
210+
206211
va_end(args);
207212

208213
if (((msg_buf_idx[buffer_id]+i+2) > msg_buf_maxsz) || (dlog_queue_next[buffer_id] >= dlog_queue_max_entries)) {
@@ -251,7 +256,7 @@ void * datum_logger_thread(void *ptr) {
251256
struct tm tm_info_storage;
252257
struct tm *tm_info;
253258
DLOG_MSG *msg;
254-
char time_buffer[40];
259+
char time_buffer[20];
255260
char log_line[1200];
256261
FILE *log_handle = NULL;
257262
time_t next_log_rotate = get_midnight_timestamp();
@@ -348,9 +353,9 @@ void * datum_logger_thread(void *ptr) {
348353
tm_info = localtime_r(&seconds, &tm_info_storage);
349354
strftime(time_buffer, sizeof(time_buffer), "%Y-%m-%d %H:%M:%S", tm_info);
350355
if (log_calling_function) {
351-
j = snprintf(log_line, 1199, "%s.%03d [%44s] %s: %s\n", time_buffer, millis, msg->calling_function, level_text[msg->level], msg->msg);
356+
j = snprintf(log_line, sizeof(log_line), "%s.%03d [%44s] %s: %s\n", time_buffer, millis, msg->calling_function, level_text[msg->level], msg->msg);
352357
} else {
353-
j = snprintf(log_line, 1199, "%s.%03d %s: %s\n", time_buffer, millis, level_text[msg->level], msg->msg);
358+
j = snprintf(log_line, sizeof(log_line), "%s.%03d %s: %s\n", time_buffer, millis, level_text[msg->level], msg->msg);
354359
}
355360
log_line[1199] = 0;
356361

@@ -390,8 +395,7 @@ void * datum_logger_thread(void *ptr) {
390395

391396
tm_info = localtime_r(&log_file_opened, &tm_info_storage);
392397
strftime(time_buffer, sizeof(time_buffer), "%Y-%m-%d", tm_info);
393-
snprintf(log_line, 1199, "%s.%s", log_file, time_buffer);
394-
log_line[1199] = 0;
398+
snprintf(log_line, sizeof(log_line), "%s.%s", log_file, time_buffer);
395399

396400
fclose(log_handle);
397401

0 commit comments

Comments
 (0)