diff --git a/src/ckdb.h b/src/ckdb.h index ecddeebc..830a7e9d 100644 --- a/src/ckdb.h +++ b/src/ckdb.h @@ -55,7 +55,7 @@ #define DB_VLOCK "1" #define DB_VERSION "1.0.0" -#define CKDB_VERSION DB_VERSION"-1.023" +#define CKDB_VERSION DB_VERSION"-1.030" #define WHERE_FFL " - from %s %s() line %d" #define WHERE_FFL_HERE __FILE__, __func__, __LINE__ @@ -1726,6 +1726,7 @@ extern cmp_t cmp_useratts(K_ITEM *a, K_ITEM *b); extern K_ITEM *find_useratts(int64_t userid, char *attname); extern cmp_t cmp_workers(K_ITEM *a, K_ITEM *b); extern K_ITEM *find_workers(int64_t userid, char *workername); +extern K_ITEM *first_workers(int64_t userid, K_TREE_CTX *ctx); extern K_ITEM *new_worker(PGconn *conn, bool update, int64_t userid, char *workername, char *diffdef, char *idlenotificationenabled, char *idlenotificationtime, char *by, diff --git a/src/ckdb_cmd.c b/src/ckdb_cmd.c index 80b1f9d1..3e73553c 100644 --- a/src/ckdb_cmd.c +++ b/src/ckdb_cmd.c @@ -2308,15 +2308,18 @@ static char *cmd_auth_do(PGconn *conn, char *cmd, char *id, char *by, char *code, char *inet, tv_t *cd, K_TREE *trf_root) { + K_TREE_CTX ctx[1]; 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, *u_item, *oc_item; + K_ITEM *i_enonce1, *i_useragent, *i_preauth, *u_item, *oc_item, *w_item; USERS *users = NULL; char *username; WORKERS *workers = NULL; OPTIONCONTROL *optioncontrol; - bool ok; + size_t len, off; + char *buf; + bool ok, first; LOGDEBUG("%s(): cmd '%s'", __func__, cmd); @@ -2378,18 +2381,42 @@ static char *cmd_auth_do(PGconn *conn, char *cmd, char *id, char *by, if (!ok) { LOGDEBUG("%s() %s.failed.DBE", __func__, id); return strdup("failed.DBE"); - } else { - // Only flag a successful auth - ck_wlock(&last_lock); - setnow(&last_auth); - ck_wunlock(&last_lock); } + + // Only flag a successful auth + ck_wlock(&last_lock); + setnow(&last_auth); + ck_wunlock(&last_lock); + + APPEND_REALLOC_INIT(buf, off, len); snprintf(reply, siz, "ok.authorise={\"secondaryuserid\":\"%s\"," - "\"difficultydefault\":%d}", - users->secondaryuserid, workers->difficultydefault); - LOGDEBUG("%s.%s", id, reply); - return strdup(reply); + "\"workers\":[", + users->secondaryuserid); + APPEND_REALLOC(buf, off, len, reply); + first = true; + K_RLOCK(workers_free); + w_item = first_workers(users->userid, ctx); + DATA_WORKERS_NULL(workers, w_item); + while (w_item && workers->userid == users->userid) { + if (CURRENT(&(workers->expirydate))) { + snprintf(reply, siz, + "%s{\"workername\":\"%s\"," + "\"difficultydefault\":%"PRId32"}", + first ? EMPTY : ",", + workers->workername, + workers->difficultydefault); + APPEND_REALLOC(buf, off, len, reply); + first = false; + } + w_item = next_in_ktree(ctx); + DATA_WORKERS_NULL(workers, w_item); + } + K_RUNLOCK(workers_free); + APPEND_REALLOC(buf, off, len, "]}"); + + LOGDEBUG("%s.%s", id, buf); + return buf; } static char *cmd_auth(PGconn *conn, char *cmd, char *id, @@ -2404,13 +2431,16 @@ static char *cmd_addrauth_do(PGconn *conn, char *cmd, char *id, char *by, char *code, char *inet, tv_t *cd, K_TREE *trf_root) { + K_TREE_CTX ctx[1]; 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, *w_item; USERS *users = NULL; WORKERS *workers = NULL; - bool ok; + size_t len, off; + char *buf; + bool ok, first; LOGDEBUG("%s(): cmd '%s'", __func__, cmd); @@ -2456,18 +2486,42 @@ static char *cmd_addrauth_do(PGconn *conn, char *cmd, char *id, char *by, if (!ok) { LOGDEBUG("%s() %s.failed.DBE", __func__, id); return strdup("failed.DBE"); - } else { - // Only flag a successful auth - ck_wlock(&last_lock); - setnow(&last_auth); - ck_wunlock(&last_lock); } + + // Only flag a successful auth + ck_wlock(&last_lock); + setnow(&last_auth); + ck_wunlock(&last_lock); + + APPEND_REALLOC_INIT(buf, off, len); snprintf(reply, siz, "ok.addrauth={\"secondaryuserid\":\"%s\"," - "\"difficultydefault\":%d}", - users->secondaryuserid, workers->difficultydefault); - LOGDEBUG("%s.%s", id, reply); - return strdup(reply); + "\"workers\":[", + users->secondaryuserid); + APPEND_REALLOC(buf, off, len, reply); + first = true; + K_RLOCK(workers_free); + w_item = first_workers(users->userid, ctx); + DATA_WORKERS_NULL(workers, w_item); + while (w_item && workers->userid == users->userid) { + if (CURRENT(&(workers->expirydate))) { + snprintf(reply, siz, + "%s{\"workername\":\"%s\"," + "\"difficultydefault\":%"PRId32"}", + first ? EMPTY : ",", + workers->workername, + workers->difficultydefault); + APPEND_REALLOC(buf, off, len, reply); + first = false; + } + w_item = next_in_ktree(ctx); + DATA_WORKERS_NULL(workers, w_item); + } + K_RUNLOCK(workers_free); + APPEND_REALLOC(buf, off, len, "]}"); + + LOGDEBUG("%s.%s", id, buf); + return buf; } static char *cmd_addrauth(PGconn *conn, char *cmd, char *id, diff --git a/src/ckdb_data.c b/src/ckdb_data.c index a4227e7e..f1303265 100644 --- a/src/ckdb_data.c +++ b/src/ckdb_data.c @@ -1113,6 +1113,26 @@ K_ITEM *find_workers(int64_t userid, char *workername) return find_in_ktree(workers_root, &look, cmp_workers, ctx); } +K_ITEM *first_workers(int64_t userid, K_TREE_CTX *ctx) +{ + WORKERS workers; + K_TREE_CTX ctx0[1]; + K_ITEM look; + + if (ctx == NULL) + ctx = ctx0; + + workers.userid = userid; + workers.workername[0] = '\0'; + workers.expirydate.tv_sec = 0L; + workers.expirydate.tv_usec = 0L; + + INIT_WORKERS(&look); + look.data = (void *)(&workers); + // Caller needs to check userid/expirydate if the result != NULL + return find_after_in_ktree(workers_root, &look, cmp_workers, ctx); +} + K_ITEM *new_worker(PGconn *conn, bool update, int64_t userid, char *workername, char *diffdef, char *idlenotificationenabled, char *idlenotificationtime, char *by,