fbc5cd4a586de3fe45dc2d6b9046b8f18c35a314 galt Thu Feb 27 22:21:14 2014 -0800 adding support for auto-upgrade of userDb and sessionDb tables when sessionKey field is missing diff --git src/hg/lib/cartDb.c src/hg/lib/cartDb.c index 92e6039..5a3a0d2 100644 --- src/hg/lib/cartDb.c +++ src/hg/lib/cartDb.c @@ -11,36 +11,41 @@ * alter table userDb add column sessionKey varchar(255) NOT NULL default ''; * alter table sessionDb add column sessionKey varchar(255) NOT NULL default ''; * * find and modify the .as and .sql corresponding to cartDb * * */ #include "common.h" #include "linefile.h" #include "dystring.h" #include "jksql.h" #include "hgConfig.h" #include "base64.h" #include "cartDb.h" +#include "cart.h" + +extern DbConnector cartDefaultConnector; +extern DbDisconnect cartDefaultDisconnector; + +static boolean userDbInitialized = FALSE; +static boolean sessionDbInitialized = FALSE; boolean cartDbHasSessionKey(struct sqlConnection *conn, char *table) /* Check to see if the table has the sessionKey field */ { -static boolean userDbInitialized = FALSE; -static boolean sessionDbInitialized = FALSE; static boolean userDbHasSessionKey = FALSE; static boolean sessionDbHasSessionKey = FALSE; if (sameString(table, "userDb")) { if (!userDbInitialized) { userDbInitialized = TRUE; if (sqlFieldIndex(conn, table, "sessionKey") >= 0) { userDbHasSessionKey = TRUE; } } return userDbHasSessionKey; } else if (sameString(table, "sessionDb")) @@ -58,56 +63,75 @@ else errAbort("Unknown table %s", table); return FALSE; } boolean cartDbUseSessionKey() /* Check settings and and state to determine if sessionKey is in use */ { static boolean initialized = FALSE; static boolean useSessionKey = FALSE; if (!initialized) { initialized = TRUE; char *sessionKey = cfgOption2("browser", "sessionKey"); if (!sessionKey) - sessionKey = "off"; // DEFAULT + sessionKey = "off"; // DEFAULT but this might change to another value if (sameString(sessionKey, "on")) + { useSessionKey = TRUE; + struct sqlConnection *conn = cartDefaultConnector(); + boolean userDbHasSessionKey = cartDbHasSessionKey(conn, "userDb"); + boolean sessionDbHasSessionKey = cartDbHasSessionKey(conn, "sessionDb"); + if ( ! (userDbHasSessionKey && sessionDbHasSessionKey) ) + { + //errAbort("brower.sessionKey=on but userDb and sesionDb are missing the sessionKey field."); + // AUTO-UPGRADE tables to add missing sessionKey field here. + if (!userDbHasSessionKey) + sqlUpdate(conn, "NOSQLINJ alter table userDb add column sessionKey varchar(255) NOT NULL default ''"); + if (!sessionDbHasSessionKey) + sqlUpdate(conn, "NOSQLINJ alter table sessionDb add column sessionKey varchar(255) NOT NULL default ''"); + userDbInitialized = FALSE; + sessionDbInitialized = FALSE; + } + cartDefaultDisconnector(&conn); + } else if (sameString(sessionKey, "off")) + { useSessionKey = FALSE; + } else if (sameString(sessionKey, "autodetect")) { errAbort("brower.sessionKey=autodetect has not implemented yet."); // TODO } } return useSessionKey; } char *cartDbMakeRandomKey(int numBits) /* Generate base64 encoding of a random key of at least size numBits returning string to be freed when done */ { int numBytes = (numBits + 7) / 8; // round up to nearest whole byte. numBytes = ((numBytes+2)/3)*3; // round up to the nearest multiple of 3 to avoid equals-char padding in base64 output FILE *f = mustOpen("/dev/urandom", "r"); // open random system device for read-only access. char *binaryString = needMem(numBytes); mustRead(f, binaryString, numBytes); carefulClose(&f); char * result = base64Encode(binaryString, numBytes); // converts 3 binary bytes into 4 printable characters int len = strlen(result); memSwapChar(result, len, '+', '-'); // replace + and / with characters that are URL-friendly. -memSwapChar(result, len, '/', '@'); +memSwapChar(result, len, '/', '*'); freeMem(binaryString); return result; } void cartDbSecureId(char *buf, int bufSize, struct cartDb *cartDb) /* Return combined string of session id plus sessionKey in buf if turned on.*/ { if (cartDbUseSessionKey() && !sameString(cartDb->sessionKey,"")) safef(buf, bufSize, "%d_%s", cartDb->id, cartDb->sessionKey); else safef(buf, bufSize, "%d", cartDb->id); } unsigned int cartDbParseId(char *id, char **pSessionKey)