Skip to content

Commit

Permalink
Use btcd cookie file for authentication
Browse files Browse the repository at this point in the history
Alternatively use the btcd cookie file instead of username and password auth.
This need btcd.cookie to be set as cookie path in ckpool config file (see sample config).

If defined, the cookie auth has precedence but falls back to user auth in case of errors
(wrong path, wrong or invalid value).
  • Loading branch information
golden-guy committed Dec 26, 2023
1 parent 6dc7561 commit 3df23a6
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
1 change: 1 addition & 0 deletions ckpool.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"url" : "localhost:8332",
"auth" : "user",
"pass" : "pass",
"cookie": "/home/user/.bitcoin/.cookie",
"notify" : true
},
{
Expand Down
3 changes: 3 additions & 0 deletions src/ckpool.c
Original file line number Diff line number Diff line change
Expand Up @@ -1250,12 +1250,14 @@ static void parse_btcds(ckpool_t *ckp, const json_t *arr_val, const int arr_size
ckp->btcdurl = ckzalloc(sizeof(char *) * arr_size);
ckp->btcdauth = ckzalloc(sizeof(char *) * arr_size);
ckp->btcdpass = ckzalloc(sizeof(char *) * arr_size);
ckp->btcdcookie = ckzalloc(sizeof(char *) * arr_size);
ckp->btcdnotify = ckzalloc(sizeof(bool *) * arr_size);
for (i = 0; i < arr_size; i++) {
val = json_array_get(arr_val, i);
json_get_string(&ckp->btcdurl[i], val, "url");
json_get_string(&ckp->btcdauth[i], val, "auth");
json_get_string(&ckp->btcdpass[i], val, "pass");
json_get_string(&ckp->btcdcookie[i], val, "cookie");
json_get_bool(&ckp->btcdnotify[i], val, "notify");
}
}
Expand Down Expand Up @@ -1724,6 +1726,7 @@ int main(int argc, char **argv)
ckp.btcdurl = ckzalloc(sizeof(char *));
ckp.btcdauth = ckzalloc(sizeof(char *));
ckp.btcdpass = ckzalloc(sizeof(char *));
ckp.btcdcookie = ckzalloc(sizeof(char *));
ckp.btcdnotify = ckzalloc(sizeof(bool));
}
for (i = 0; i < ckp.btcds; i++) {
Expand Down
2 changes: 2 additions & 0 deletions src/ckpool.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ struct server_instance {
char *url;
char *auth;
char *pass;
char *cookie;
bool notify;
bool alive;
connsock_t cs;
Expand Down Expand Up @@ -225,6 +226,7 @@ struct ckpool_instance {
char **btcdurl;
char **btcdauth;
char **btcdpass;
char **btcdcookie;
bool *btcdnotify;
int blockpoll; // How frequently in ms to poll bitcoind for block updates
int nonce1length; // Extranonce1 length
Expand Down
43 changes: 40 additions & 3 deletions src/generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,15 @@ static bool server_alive(ckpool_t *ckp, server_instance_t *si, bool pinging)
LOGWARNING("Failed to extract address from %s", si->url);
return ret;
}
userpass = strdup(si->auth);
realloc_strcat(&userpass, ":");
realloc_strcat(&userpass, si->pass);
/* Use btcd cookie file for authentication */
if (si->cookie) {
LOGDEBUG("Using cookie auth: %s", si->cookie);
userpass = strdup(si->cookie);
} else {
userpass = strdup(si->auth);
realloc_strcat(&userpass, ":");
realloc_strcat(&userpass, si->pass);
}
dealloc(cs->auth);
cs->auth = http_base64(userpass);
if (!cs->auth) {
Expand Down Expand Up @@ -3257,6 +3263,34 @@ static void proxy_loop(proc_instance_t *pi)
return;
}

/* Read btcd cookie file from path, check if the path exists and
* return the cookie value. Otherwise, bail out with an error. */
static char *read_cookie(const char *path)
{
FILE *fp;
char buf[76];
char *cookie = NULL;

if(!path) {
return NULL;
}

fp = fopen(path, "r");
if (!fp) {
LOGERR("Failed to open cookie file %s", path);
return NULL;
}
cookie = fgets(buf, sizeof(buf), fp);
fclose(fp);

/* Check for valid cookie string */
if(!strstr(cookie, "__cookie__:")) {
return NULL;
}

return cookie;
}

/* Check which servers are alive, maintaining a connection with them and
* reconnect if a higher priority one is available. */
static void *server_watchdog(void *arg)
Expand Down Expand Up @@ -3297,12 +3331,15 @@ static void setup_servers(ckpool_t *ckp)
for (i = 0; i < ckp->btcds; i++) {
server_instance_t *si;
connsock_t *cs;
char *btcdcookie = NULL;

ckp->servers[i] = ckzalloc(sizeof(server_instance_t));
si = ckp->servers[i];
si->url = ckp->btcdurl[i];
si->auth = ckp->btcdauth[i];
si->pass = ckp->btcdpass[i];
if(btcdcookie = read_cookie(ckp->btcdcookie[i]))
si->cookie = strdup(btcdcookie);
si->notify = ckp->btcdnotify[i];
si->id = i;
cs = &si->cs;
Expand Down

0 comments on commit 3df23a6

Please sign in to comment.