From bab658e03eabd955ee911898e5d2252f562fbd69 Mon Sep 17 00:00:00 2001 From: kanoi Date: Fri, 21 Aug 2015 15:05:28 +1000 Subject: [PATCH] ckdb - allow changing the btc server via the socket --- src/ckdb.c | 4 ++++ src/ckdb.h | 5 ++++- src/ckdb_btc.c | 2 ++ src/ckdb_cmd.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 1 deletion(-) diff --git a/src/ckdb.c b/src/ckdb.c index df788315..50eaac49 100644 --- a/src/ckdb.c +++ b/src/ckdb.c @@ -278,6 +278,7 @@ static sem_t socketer_sem; char *btc_server = "http://127.0.0.1:8330"; char *btc_auth; int btc_timeout = 5; +cklock_t btc_lock; char *by_default = "code"; char *inet_default = "127.0.0.1"; @@ -3935,6 +3936,7 @@ static void *socketer(__maybe_unused void *arg) case CMD_USERSTATUS: case CMD_SHSTA: case CMD_USERINFO: + case CMD_BTCSET: ans = ckdb_cmds[msgline->which_cmds].func(NULL, msgline->cmd, msgline->id, @@ -4274,6 +4276,7 @@ static void reload_line(PGconn *conn, char *filename, uint64_t count, char *buf) case CMD_PSHIFT: case CMD_SHSTA: case CMD_USERINFO: + case CMD_BTCSET: LOGERR("%s() INVALID message line %"PRIu64 " ignored '%.42s...", __func__, count, @@ -5587,6 +5590,7 @@ int main(int argc, char **argv) ckp.main.processname = strdup("main"); cklock_init(&last_lock); + cklock_init(&btc_lock); cklock_init(&seq_lock); cklock_init(&process_pplns_lock); diff --git a/src/ckdb.h b/src/ckdb.h index a1f713bc..a6f94404 100644 --- a/src/ckdb.h +++ b/src/ckdb.h @@ -55,7 +55,7 @@ #define DB_VLOCK "1" #define DB_VERSION "1.0.2" -#define CKDB_VERSION DB_VERSION"-1.222" +#define CKDB_VERSION DB_VERSION"-1.223" #define WHERE_FFL " - from %s %s() line %d" #define WHERE_FFL_HERE __FILE__, __func__, __LINE__ @@ -335,6 +335,8 @@ extern cklock_t last_lock; extern char *btc_server; extern char *btc_auth; extern int btc_timeout; +// Lock access to the above variables so they can be changed +extern cklock_t btc_lock; #define EDDB "expirydate" #define CDDB "createdate" @@ -401,6 +403,7 @@ enum cmd_values { CMD_PSHIFT, CMD_SHSTA, CMD_USERINFO, + CMD_BTCSET, CMD_END }; diff --git a/src/ckdb_btc.c b/src/ckdb_btc.c index 44105190..93759bc2 100644 --- a/src/ckdb_btc.c +++ b/src/ckdb_btc.c @@ -32,9 +32,11 @@ static char *btc_data(char *json, size_t *len) APPEND_REALLOC_INIT(buf, off, *len); APPEND_REALLOC(buf, off, *len, "POST / HTTP/1.1\n"); + ck_wlock(&btc_lock); snprintf(tmp, sizeof(tmp), "Authorization: Basic %s\n", btc_auth); APPEND_REALLOC(buf, off, *len, tmp); snprintf(tmp, sizeof(tmp), "Host: %s/\n", btc_server); + ck_wunlock(&btc_lock); APPEND_REALLOC(buf, off, *len, tmp); APPEND_REALLOC(buf, off, *len, "Content-Type: application/json\n"); snprintf(tmp, sizeof(tmp), "Content-Length: %d\n\n", (int)strlen(json)); diff --git a/src/ckdb_cmd.c b/src/ckdb_cmd.c index 774a018c..64008c6e 100644 --- a/src/ckdb_cmd.c +++ b/src/ckdb_cmd.c @@ -6300,6 +6300,60 @@ static char *cmd_userinfo(__maybe_unused PGconn *conn, char *cmd, char *id, return buf; } +/* Set/show the BTC server settings + * You must supply the btcserver to change anything + * The format for userpass is username:password + * If you don't supply the btcserver it will simply report the current server + * If supply btcserver but not the userpass it will use the current userpass + * The reply will ONLY contain the URL, not the user/pass */ +static char *cmd_btcset(__maybe_unused PGconn *conn, char *cmd, char *id, + __maybe_unused tv_t *now, __maybe_unused char *by, + __maybe_unused char *code, __maybe_unused char *inet, + __maybe_unused tv_t *notcd, K_TREE *trf_root) +{ + K_ITEM *i_btcserver, *i_userpass; + char *btcserver = NULL, *userpass = NULL, *tmp; + char reply[1024] = ""; + size_t siz = sizeof(reply); + char buf[256]; + + i_btcserver = optional_name(trf_root, "btcserver", 1, NULL, reply, siz); + if (i_btcserver) { + btcserver = strdup(transfer_data(i_btcserver)); + i_userpass = optional_name(trf_root, "userpass", 0, NULL, reply, siz); + if (i_userpass) + userpass = transfer_data(i_userpass); + + ck_wlock(&btc_lock); + btc_server = btcserver; + btcserver = NULL; + if (userpass) { + if (btc_auth) { + tmp = btc_auth; + while (*tmp) + *(tmp++) = '\0'; + } + FREENULL(btc_auth); + btc_auth = http_base64(userpass); + } + ck_wunlock(&btc_lock); + + if (userpass) { + tmp = userpass; + while (*tmp) + *(tmp++) = '\0'; + } + } + + FREENULL(btcserver); + + ck_wlock(&btc_lock); + snprintf(buf, sizeof(buf), "ok.btcserver=%s", btc_server); + ck_wunlock(&btc_lock); + LOGDEBUG("%s.%s.%s", id, cmd, buf); + return strdup(buf); +} + // TODO: limit access by having seperate sockets for each #define ACCESS_POOL "p" #define ACCESS_SYSTEM "s" @@ -6414,5 +6468,6 @@ struct CMDS ckdb_cmds[] = { { CMD_PSHIFT, "pshift", false, false, cmd_pshift, SEQ_NONE, ACCESS_SYSTEM ACCESS_WEB }, { CMD_SHSTA, "shsta", true, false, cmd_shsta, SEQ_NONE, ACCESS_SYSTEM }, { CMD_USERINFO, "userinfo", false, false, cmd_userinfo, SEQ_NONE, ACCESS_WEB }, + { CMD_BTCSET, "btcset", false, false, cmd_btcset, SEQ_NONE, ACCESS_SYSTEM }, { CMD_END, NULL, false, false, NULL, SEQ_NONE, NULL } };