From ac509aa98dfac1b3dcb202e13b2193eac99195d0 Mon Sep 17 00:00:00 2001 From: kanoi Date: Thu, 11 Sep 2014 11:04:58 +1000 Subject: [PATCH] ckdb/sql - db v0.9.1 changes --- sql/ckdb.sql | 19 +++++++++++++ sql/tables.sql | 1 + sql/v0.9-v0.9.1.sql | 42 ++++++++++++++++++++++++++++ src/ckdb.c | 68 +++++++++++++++++++++++++++------------------ 4 files changed, 103 insertions(+), 27 deletions(-) create mode 100644 sql/v0.9-v0.9.1.sql diff --git a/sql/ckdb.sql b/sql/ckdb.sql index 9d509506..8c933670 100644 --- a/sql/ckdb.sql +++ b/sql/ckdb.sql @@ -19,6 +19,7 @@ CREATE TABLE users ( joineddate timestamp with time zone NOT NULL, passwordhash character varying(256) NOT NULL, secondaryuserid character varying(64) NOT NULL, + salt character varying(256) DEFAULT ''::character varying NOT NULL, createdate timestamp with time zone NOT NULL, createby character varying(64) DEFAULT ''::character varying NOT NULL, createcode character varying(128) DEFAULT ''::character varying NOT NULL, @@ -29,6 +30,24 @@ CREATE TABLE users ( CREATE UNIQUE INDEX usersusername ON users USING btree (username, expirydate); +CREATE TABLE useratts ( + userid bigint NOT NULL, + attname character varying(64) NOT NULL, + attstr character varying(256) DEFAULT ''::character varying NOT NULL, + attstr2 character varying(256) DEFAULT ''::character varying NOT NULL, + attnum bigint DEFAULT 0 NOT NULL, + attnum2 bigint DEFAULT 0 NOT NULL, + attdate timestamp with time zone DEFAULT '1970-01-01 00:00:00+00', + attdate2 timestamp with time zone DEFAULT '1970-01-01 00:00:00+00', + createdate timestamp with time zone NOT NULL, + createby character varying(64) DEFAULT ''::character varying NOT NULL, + createcode character varying(128) DEFAULT ''::character varying NOT NULL, + createinet character varying(128) DEFAULT ''::character varying NOT NULL, + expirydate timestamp with time zone DEFAULT '6666-06-06 06:06:06+00', + PRIMARY KEY (userid, attname, expirydate) +); + + CREATE TABLE workers ( workerid bigint NOT NULL, -- unique per record userid bigint NOT NULL, diff --git a/sql/tables.sql b/sql/tables.sql index b3e1f529..e9c63491 100644 --- a/sql/tables.sql +++ b/sql/tables.sql @@ -15,6 +15,7 @@ select 'shareerrors' as "shareerrors",count(*) from shareerrors; select 'shares' as "shares",count(*) from shares; select 'sharesummary' as "sharesummary",count(*) from sharesummary; select 'users' as "users",count(*) from users; +select 'useratts' as "useratts",count(*) from useratts; select 'userstats' as "userstats",count(*) from userstats; select 'version' as "version",count(*) from version; select 'workers' as "workers",count(*) from workers; diff --git a/sql/v0.9-v0.9.1.sql b/sql/v0.9-v0.9.1.sql new file mode 100644 index 00000000..6c9352e9 --- /dev/null +++ b/sql/v0.9-v0.9.1.sql @@ -0,0 +1,42 @@ +SET SESSION AUTHORIZATION 'postgres'; + +BEGIN transaction; + +DO $$ +DECLARE ver TEXT; +BEGIN + + UPDATE version set version='0.9.1' where vlock=1 and version='0.9'; + + IF found THEN + RETURN; + END IF; + + SELECT version into ver from version + WHERE vlock=1; + + RAISE EXCEPTION 'Wrong DB version - expect "0.9" - found "%"', ver; + +END $$; + +CREATE TABLE useratts ( + userid bigint NOT NULL, + attname character varying(64) NOT NULL, + attstr character varying(256) DEFAULT ''::character varying NOT NULL, + attstr2 character varying(256) DEFAULT ''::character varying NOT NULL, + attnum bigint DEFAULT 0 NOT NULL, + attnum2 bigint DEFAULT 0 NOT NULL, + attdate timestamp with time zone DEFAULT '1970-01-01 00:00:00+00', + attdate2 timestamp with time zone DEFAULT '1970-01-01 00:00:00+00', + createdate timestamp with time zone NOT NULL, + createby character varying(64) DEFAULT ''::character varying NOT NULL, + createcode character varying(128) DEFAULT ''::character varying NOT NULL, + createinet character varying(128) DEFAULT ''::character varying NOT NULL, + expirydate timestamp with time zone DEFAULT '6666-06-06 06:06:06+00', + PRIMARY KEY (userid, attname, expirydate) +); + +ALTER TABLE ONLY users + ADD COLUMN salt character varying(256) DEFAULT ''::character varying NOT NULL; + +END transaction; diff --git a/src/ckdb.c b/src/ckdb.c index 26e16ac2..cf1e57ed 100644 --- a/src/ckdb.c +++ b/src/ckdb.c @@ -46,8 +46,8 @@ */ #define DB_VLOCK "1" -#define DB_VERSION "0.9" -#define CKDB_VERSION DB_VERSION"-0.276" +#define DB_VERSION "0.9.1" +#define CKDB_VERSION DB_VERSION"-0.280" #define WHERE_FFL " - from %s %s() line %d" #define WHERE_FFL_HERE __FILE__, __func__, __LINE__ @@ -838,8 +838,6 @@ typedef struct logqueue { static K_LIST *logqueue_free; static K_STORE *logqueue_store; -static pthread_mutex_t wq_waitlock; -static pthread_cond_t wq_waitcond; // WORKQUEUE typedef struct workqueue { @@ -865,6 +863,8 @@ typedef struct workqueue { static K_LIST *workqueue_free; static K_STORE *workqueue_store; +static pthread_mutex_t wq_waitlock; +static pthread_cond_t wq_waitcond; // TRANSFER #define NAME_SIZE 63 @@ -954,6 +954,7 @@ typedef struct users { tv_t joineddate; char passwordhash[TXT_BIG+1]; char secondaryuserid[TXT_SML+1]; + char salt[TXT_BIG+1]; HISTORYDATECONTROLFIELDS; } USERS; @@ -968,23 +969,28 @@ static K_TREE *userid_root; static K_LIST *users_free; static K_STORE *users_store; -/* TODO: for account settings - but do we want manual/auto payouts? -// USERACCOUNTS -typedef struct useraccounts { +/* TODO: +// USERATTS +typedef struct useratts { int64_t userid; - int64_t payoutlimit; - char autopayout[TXT_FLG+1]; + char attstr[TXT_BIG+1]; + char attstr2[TXT_BIG+1]; + int64_t attnum; + int64_t attnum2; + tv_t attdate; + tv_t attdate2; HISTORYDATECONTROLFIELDS; -} USERACCOUNTS; +} USERATTS; -#define ALLOC_USERACCOUNTS 1024 -#define LIMIT_USERACCOUNTS 0 -#define INIT_USERACCOUNTS(_item) INIT_GENERIC(_item, useraccounts) -#define DATA_USERACCOUNTS(_var, _item) DATA_GENERIC(_var, _item, useraccounts, true) +#define ALLOC_USERATTS 1024 +#define LIMIT_USERATTS 0 +#define INIT_USERATTS(_item) INIT_GENERIC(_item, useratts) +#define DATA_USERATTS(_var, _item) DATA_GENERIC(_var, _item, useratts, true) +#define DATA_USERATTS_NULL(_var, _item) DATA_GENERIC(_var, _item, useratts, true) -static K_TREE *useraccounts_root; -static K_LIST *useraccounts_free; -static K_STORE *useraccounts_store; +static K_TREE *useratts_root; +static K_LIST *useratts_free; +static K_STORE *useratts_store; */ // WORKERS @@ -2202,8 +2208,8 @@ static PGconn *dbconnect() snprintf(conninfo, sizeof(conninfo), "host=127.0.0.1 dbname=%s user=%s%s%s", - db_name, - db_user, db_pass ? " password=" : "", + db_name, db_user, + db_pass ? " password=" : "", db_pass ? db_pass : ""); conn = PQconnectdb(conninfo); @@ -2730,9 +2736,9 @@ static bool users_pass_email(PGconn *conn, K_ITEM *u_item, char *oldhash, ins = "insert into users " "(userid,username,emailaddress,joineddate,passwordhash," - "secondaryuserid" + "secondaryuserid,salt" HISTORYDATECONTROL ") select " - "userid,username,$3,joineddate,$4,secondaryuserid," + "userid,username,$3,joineddate,$4,secondaryuserid,salt," "$5,$6,$7,$8,$9 from users where " "userid=$1 and expirydate=$2"; @@ -2788,7 +2794,7 @@ static K_ITEM *users_add(PGconn *conn, char *username, char *emailaddress, uint64_t hash; __maybe_unused uint64_t tmp; bool ok = false; - char *params[6 + HISTORYDATECOUNT]; + char *params[7 + HISTORYDATECOUNT]; int par; LOGDEBUG("%s(): add", __func__); @@ -2814,6 +2820,8 @@ static K_ITEM *users_add(PGconn *conn, char *username, char *emailaddress, HASH_BER(tohash, strlen(tohash), 1, hash, tmp); __bin2hex(row->secondaryuserid, (void *)(&hash), sizeof(hash)); + row->salt[0] = '\0'; + HISTORYDATEINIT(row, cd, by, code, inet); HISTORYDATETRANSFER(trf_root, row); @@ -2828,13 +2836,14 @@ static K_ITEM *users_add(PGconn *conn, char *username, char *emailaddress, params[par++] = tv_to_buf(&(row->joineddate), NULL, 0); params[par++] = str_to_buf(row->passwordhash, NULL, 0); params[par++] = str_to_buf(row->secondaryuserid, NULL, 0); + params[par++] = str_to_buf(row->salt, NULL, 0); HISTORYDATEPARAMS(params, par, row); PARCHK(par, params); ins = "insert into users " "(userid,username,emailaddress,joineddate,passwordhash," - "secondaryuserid" - HISTORYDATECONTROL ") values (" PQPARAM11 ")"; + "secondaryuserid,salt" + HISTORYDATECONTROL ") values (" PQPARAM12 ")"; if (!conn) { conn = dbconnect(); @@ -2881,14 +2890,14 @@ static bool users_fill(PGconn *conn) USERS *row; char *field; char *sel; - int fields = 6; + int fields = 7; bool ok; LOGDEBUG("%s(): select", __func__); sel = "select " "userid,username,emailaddress,joineddate,passwordhash," - "secondaryuserid" + "secondaryuserid,salt" HISTORYDATECONTROL " from users"; res = PQexec(conn, sel, CKPQ_READ); @@ -2950,6 +2959,11 @@ static bool users_fill(PGconn *conn) break; TXT_TO_STR("secondaryuserid", field, row->secondaryuserid); + PQ_GET_FLD(res, i, "salt", field, ok); + if (!ok) + break; + TXT_TO_STR("salt", field, row->salt); + HISTORYDATEFLDS(res, i, row, ok); if (!ok) break; @@ -3585,7 +3599,7 @@ unitem: if (!ok) k_add_head(paymentaddresses_free, item); else { - // Remove old (unneeded) records + // Remove from ram, old (unneeded) records pa.userid = userid; pa.expirydate.tv_sec = 0L; pa.payaddress[0] = '\0';