From 1e578f4e89b4ca217fb6a2ce11c5e26ad312b663 Mon Sep 17 00:00:00 2001 From: kanoi Date: Fri, 28 Nov 2014 14:38:48 +1100 Subject: [PATCH] ckdb - optioncontrol AutoAddUser to auth/add unknown users automatically --- src/ckdb.h | 8 +++++++- src/ckdb_cmd.c | 22 ++++++++++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/ckdb.h b/src/ckdb.h index 39a26dc7..e7c0187a 100644 --- a/src/ckdb.h +++ b/src/ckdb.h @@ -52,7 +52,7 @@ #define DB_VLOCK "1" #define DB_VERSION "0.9.6" -#define CKDB_VERSION DB_VERSION"-0.666" +#define CKDB_VERSION DB_VERSION"-0.667" #define WHERE_FFL " - from %s %s() line %d" #define WHERE_FFL_HERE __FILE__, __func__, __LINE__ @@ -857,6 +857,12 @@ typedef struct optioncontrol { #error "START_POOL_HEIGHT must = (OPTIONCONTROL_HEIGHT+1)" #endif +/* If set, then cmd_auth() will create unknown users + * It will use the optionvalue as the hex sha256 password hash + * A blank or random/invalid hash will mean the accounts created + * are password locked, like an address account is */ +#define OPTIONCONTROL_AUTOADDUSER "AutoAddUser" + extern K_TREE *optioncontrol_root; extern K_LIST *optioncontrol_free; extern K_STORE *optioncontrol_store; diff --git a/src/ckdb_cmd.c b/src/ckdb_cmd.c index 1e9ac011..bcf74968 100644 --- a/src/ckdb_cmd.c +++ b/src/ckdb_cmd.c @@ -1801,9 +1801,11 @@ static char *cmd_auth_do(PGconn *conn, char *cmd, char *id, char *by, char reply[1024] = ""; size_t siz = sizeof(reply); K_ITEM *i_poolinstance, *i_username, *i_workername, *i_clientid; - K_ITEM *i_enonce1, *i_useragent, *i_preauth; + K_ITEM *i_enonce1, *i_useragent, *i_preauth, *u_item, *oc_item; USERS *users = NULL; + char *username; WORKERS *workers = NULL; + OPTIONCONTROL *optioncontrol; bool ok; LOGDEBUG("%s(): cmd '%s'", __func__, cmd); @@ -1816,6 +1818,7 @@ static char *cmd_auth_do(PGconn *conn, char *cmd, char *id, char *by, i_username = require_name(trf_root, "username", 1, NULL, reply, siz); if (!i_username) return strdup(reply); + username = transfer_data(i_username); i_workername = require_name(trf_root, "workername", 1, NULL, reply, siz); if (!i_workername) @@ -1837,8 +1840,23 @@ static char *cmd_auth_do(PGconn *conn, char *cmd, char *id, char *by, if (!i_preauth) i_preauth = &auth_preauth; + K_RLOCK(optioncontrol_free); + oc_item = find_optioncontrol(OPTIONCONTROL_AUTOADDUSER, cd); + K_RUNLOCK(optioncontrol_free); + if (oc_item) { + K_RLOCK(users_free); + u_item = find_users(username); + K_RUNLOCK(users_free); + if (!u_item) { + DATA_OPTIONCONTROL(optioncontrol, oc_item); + u_item = users_add(conn, username, EMPTY, + optioncontrol->optionvalue, + by, code, inet, cd, trf_root); + } + } + ok = auths_add(conn, transfer_data(i_poolinstance), - transfer_data(i_username), + username, transfer_data(i_workername), transfer_data(i_clientid), transfer_data(i_enonce1),