rpg-x2/game/g_sql.c

437 lines
8.9 KiB
C
Raw Normal View History

2011-06-01 12:20:56 +00:00
#ifdef SQL
#if (defined(_MSC_VER) | defined(__WIN32__))
#include "windows.h"
#endif
#include "sqlite3.h"
2011-06-01 12:20:56 +00:00
//#include "q_shared.h"
#include "g_sql.h"
#include "g_local.h"
#include "md5.h"
2011-06-01 12:20:56 +00:00
extern vmCvar_t sql_dbName;
extern vmCvar_t sql_use;
extern vmCvar_t sql_server;
extern vmCvar_t sql_user;
extern vmCvar_t sql_password;
extern vmCvar_t sql_port;
extern void QDECL G_Printf( const char *fmt, ... );
extern void QDECL G_PrintfClient( gentity_t *ent, const char *fmt, ...);
2011-12-16 09:24:25 +00:00
sqlite3 *user_db;
2011-12-16 10:43:16 +00:00
qboolean sql_ready = qfalse;
2011-12-16 09:24:25 +00:00
2011-12-27 11:28:54 +00:00
/*
===============
G_Sql_Check_StepReturn
===============
*/
static qboolean G_Sql_Check_StepReturn(int r) {
switch(r) {
case SQLITE_ROW:
case SQLITE_DONE:
return qfalse;
default:
G_Printf(S_COLOR_RED "SQL ERROR: %s\n", sqlite3_errmsg(user_db));
break;
}
return qtrue;
}
/*
===============
G_Sql_Check_PrepareReturn
===============
*/
static qboolean G_Sql_Check_PrepareReturn(int r) {
switch(r) {
case SQLITE_OK:
return qfalse;
default:
G_Printf(S_COLOR_RED "SQL ERROR: %s\n", sqlite3_errmsg(user_db));
break;
}
return qtrue;
}
/*
===============
G_Sql_Check_BindReturn
===============
*/
static qboolean G_Sql_Check_BindReturn(int r) {
switch(r) {
case SQLITE_OK:
return qfalse;
default:
G_Printf(S_COLOR_RED "SQL ERROR: %s\n", sqlite3_errmsg(user_db));
break;
}
return qtrue;
}
2011-12-16 10:43:16 +00:00
/*
===============
G_Sql_Md5
===============
*/
2011-12-27 11:28:54 +00:00
static char *G_Sql_Md5(const char *s) {
char *res;
unsigned char sig[16];
struct MD5Context md5c;
MD5Init(&md5c);
MD5Update(&md5c, s, strlen(s));
MD5Final(sig, &md5c);
res = (char *)malloc(sizeof(char)*MAX_QPATH);
if(!res) {
G_Printf(S_COLOR_RED "SQL ERROR: was unable to allocate %u byte\n", sizeof(char)*(strlen(s)+1));
return NULL;
}
sprintf(res, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
sig[0], sig[1], sig[2], sig[3], sig[4], sig[5], sig[6], sig[7],
sig[8], sig[9], sig[10], sig[11], sig[12], sig[13], sig[14], sig[15] );
return res;
}
2011-12-15 23:41:23 +00:00
/*
===============
G_SqlInit
===============
*/
qboolean G_Sql_Init(void) {
2011-12-16 09:24:25 +00:00
int res;
2011-12-16 10:43:16 +00:00
sqlite3_stmt *stmt;
2011-12-16 09:24:25 +00:00
if(!sql_use.integer) return qtrue;
res = sqlite3_open("db/users.db", &user_db);
2011-12-27 11:28:54 +00:00
if(res != SQLITE_OK) {
G_Printf(S_COLOR_RED "SQL ERROR: %s\n", sqlite3_errmsg(user_db));
2011-12-16 09:24:25 +00:00
return qfalse;
}
2011-12-16 10:43:16 +00:00
res = sqlite3_prepare_v2(user_db, SQL_ENABLE_FOREIGN_KEY_CONSTRAINTS, -1, &stmt, 0);
2011-12-27 11:28:54 +00:00
if(G_Sql_Check_PrepareReturn(res)) {
2011-12-16 10:43:16 +00:00
sqlite3_close(user_db);
sql_ready = qfalse;
return qfalse;
}
2011-12-27 11:28:54 +00:00
res = sqlite3_step(stmt);
if(G_Sql_Check_StepReturn(res)) {
sqlite3_close(user_db);
sql_ready = qfalse;
return qfalse;
}
sqlite3_finalize(stmt);
2011-12-16 20:09:23 +00:00
res = sqlite3_prepare_v2(user_db, SQL_USER_CREATEUSERTABLE, -1, &stmt, 0);
2011-12-27 11:28:54 +00:00
if(G_Sql_Check_PrepareReturn(res)) {
2011-12-16 20:09:23 +00:00
sqlite3_close(user_db);
sql_ready = qfalse;
return qfalse;
}
2011-12-27 11:28:54 +00:00
res = sqlite3_step(stmt);
if(G_Sql_Check_StepReturn(res)) {
sqlite3_close(user_db);
sql_ready = qfalse;
return qfalse;
}
sqlite3_finalize(stmt);
2011-12-16 20:09:23 +00:00
res = sqlite3_prepare_v2(user_db, SQL_USER_CREATERIGHTSTABLE, -1, &stmt, 0);
2011-12-27 11:28:54 +00:00
if(G_Sql_Check_PrepareReturn(res)) {
sqlite3_close(user_db);
sql_ready = qfalse;
return qfalse;
}
res = sqlite3_step(stmt);
if(G_Sql_Check_StepReturn(res)) {
2011-12-16 20:09:23 +00:00
sqlite3_close(user_db);
sql_ready = qfalse;
return qfalse;
}
2011-12-27 11:28:54 +00:00
sqlite3_finalize(stmt);
2011-12-16 10:43:16 +00:00
sql_ready = qtrue;
2011-12-16 09:24:25 +00:00
return qtrue;
2011-12-15 23:41:23 +00:00
}
2011-06-01 12:20:56 +00:00
/*
===============
2011-12-16 09:24:25 +00:00
G_Sql_Shutdown
2011-06-01 12:20:56 +00:00
===============
*/
2011-12-16 09:24:25 +00:00
void G_Sql_Shutdown(void) {
if(!sql_use.integer) return;
2011-12-16 10:43:16 +00:00
sql_ready = qfalse;
2011-12-16 09:24:25 +00:00
sqlite3_close(user_db);
2011-06-01 12:20:56 +00:00
}
/*
===============
2011-12-16 10:43:16 +00:00
G_Sql_UserDB_Del
2011-06-01 12:20:56 +00:00
===============
*/
2011-12-16 20:09:23 +00:00
qboolean G_Sql_UserDB_Del(const char *uName) {
2011-12-16 10:43:16 +00:00
sqlite3_stmt *stmt;
2011-12-27 11:28:54 +00:00
int res, id = -1;
2011-12-16 10:43:16 +00:00
2011-12-27 11:28:54 +00:00
res = sqlite3_prepare_v2(user_db, "SELECT id FROM rpgx_users WHERE username = :UNAME", -1, &stmt, 0);
if(G_Sql_Check_PrepareReturn(res)) {
return qfalse;
}
res = sqlite3_bind_text(stmt, 1, uName, sizeof(uName), SQLITE_STATIC);
if(G_Sql_Check_BindReturn(res)) {
return qfalse;
}
res = sqlite3_step(stmt);
if(G_Sql_Check_StepReturn(res)) {
2011-12-16 10:43:16 +00:00
return qfalse;
}
2011-12-16 20:41:27 +00:00
res = sqlite3_step(stmt);
2011-12-27 11:28:54 +00:00
if(G_Sql_Check_StepReturn(res)) {
return qfalse;
}
2011-12-16 20:41:27 +00:00
if(res == SQLITE_ROW) {
id = sqlite3_column_int(stmt, 0);
} else if(res == SQLITE_DONE) {
G_Printf("SQL: User \'%s\' not found\n", uName);
return qfalse;
2011-12-27 11:28:54 +00:00
}
sqlite3_finalize(stmt);
2011-12-16 10:43:16 +00:00
if(id == -1) {
G_Printf(S_COLOR_RED "SQL ERROR: no user %s found\n", uName);
return qfalse;
}
2011-12-27 11:28:54 +00:00
res = sqlite3_prepare_v2(user_db, SQL_USER_DELTE_RIGHTS, -1, &stmt, 0);
if(G_Sql_Check_PrepareReturn(res)) {
return qfalse;
}
res = sqlite3_bind_int(stmt, 1, id);
if(G_Sql_Check_BindReturn(res)) {
2011-12-16 10:43:16 +00:00
return qfalse;
}
2011-12-27 11:28:54 +00:00
res = sqlite3_step(stmt);
if(G_Sql_Check_StepReturn(res)) {
return qfalse;
}
sqlite3_finalize(stmt);
2011-12-16 10:43:16 +00:00
2011-12-27 11:28:54 +00:00
res = sqlite3_prepare_v2(user_db, SQL_USER_DELETE, -1, &stmt, 0);
if(G_Sql_Check_PrepareReturn(res)) {
return qfalse;
}
res = sqlite3_bind_text(stmt, 1, uName, sizeof(uName), SQLITE_STATIC);
if(G_Sql_Check_BindReturn(res)) {
return qfalse;
}
res = sqlite3_step(stmt);
if(G_Sql_Check_StepReturn(res)) {
2011-12-16 10:43:16 +00:00
return qfalse;
}
2011-12-27 11:28:54 +00:00
sqlite3_finalize(stmt);
2011-12-16 10:43:16 +00:00
2011-06-01 12:20:56 +00:00
return qtrue;
}
/*
===============
G_Sql_UserAdd
===============
*/
2011-12-16 20:09:23 +00:00
qboolean Do_Sql_UserAdd(const char *uName, const char *password) {
2011-12-16 20:41:27 +00:00
sqlite3_stmt *stmt;
int res, id;
char *hashedpw;
2011-12-27 11:28:54 +00:00
res = sqlite3_prepare_v2(user_db, SQL_USER_GET_UID, -1, &stmt, 0);
if(G_Sql_Check_PrepareReturn(res)) {
return qfalse;
}
res = sqlite3_bind_text(stmt, 1, uName, sizeof(uName), SQLITE_STATIC);
if(G_Sql_Check_BindReturn(res)) {
2011-12-16 20:41:27 +00:00
return qfalse;
}
res = sqlite3_step(stmt);
2011-12-27 11:28:54 +00:00
if(G_Sql_Check_StepReturn(res)) {
return qfalse;
}
2011-12-16 20:41:27 +00:00
if(res == SQLITE_ROW) {
G_Printf("SQL: There already exists a user with username \'%s\'\n", uName);
G_Printf("SQL: If you lost your password please contact an admin with access to the database.\n");
2011-12-27 11:28:54 +00:00
sqlite3_finalize(stmt);
2011-12-16 20:41:27 +00:00
return qfalse;
}
2011-12-27 11:28:54 +00:00
sqlite3_finalize(stmt);
2011-12-16 20:41:27 +00:00
2011-12-27 11:28:54 +00:00
hashedpw = G_Sql_Md5(password);
res = sqlite3_prepare_v2(user_db, SQL_USER_ADD, -1, &stmt, 0);
if(G_Sql_Check_PrepareReturn(res)) {
2011-12-16 20:41:27 +00:00
return qfalse;
}
2011-12-27 11:28:54 +00:00
res = sqlite3_bind_text(stmt, 1, uName, sizeof(uName), SQLITE_STATIC);
if(G_Sql_Check_BindReturn(res)) {
return qfalse;
}
res = sqlite3_bind_text(stmt, 2, hashedpw, sizeof(hashedpw), free);
if(G_Sql_Check_BindReturn(res)) {
return qfalse;
}
res = sqlite3_step(stmt);
if(G_Sql_Check_StepReturn(res)) {
2011-12-16 20:41:27 +00:00
return qfalse;
}
2011-12-27 11:28:54 +00:00
res = sqlite3_prepare_v2(user_db, SQL_USER_GET_UID, -1, &stmt, 0);
if(G_Sql_Check_PrepareReturn(res)) {
return qfalse;
}
res = sqlite3_bind_text(stmt, 1, uName, sizeof(uName), SQLITE_STATIC);
if(G_Sql_Check_BindReturn(res)) {
2011-12-16 20:41:27 +00:00
return qfalse;
}
res = sqlite3_step(stmt);
2011-12-27 11:28:54 +00:00
if(G_Sql_Check_StepReturn(res)) {
return qfalse;
}
2011-12-16 20:41:27 +00:00
if(res == SQLITE_ROW) {
id = sqlite3_column_int(stmt, 0);
} else {
G_Printf("SQL: There already exists a user with username \'%s\'\n", uName);
2011-12-27 11:28:54 +00:00
sqlite3_finalize(stmt);
2011-12-16 20:41:27 +00:00
return qfalse;
}
2011-12-27 11:28:54 +00:00
sqlite3_finalize(stmt);
2011-12-16 20:41:27 +00:00
2011-12-27 11:28:54 +00:00
res = sqlite3_prepare_v2(user_db, SQL_USER_ADD_RIGHTS, -1, &stmt, 0);
if(G_Sql_Check_PrepareReturn(res)) {
2011-12-16 20:41:27 +00:00
return qfalse;
}
2011-12-27 11:28:54 +00:00
res = sqlite3_bind_int(stmt, 1, id);
if(G_Sql_Check_BindReturn(res)) {
2011-12-16 20:41:27 +00:00
return qfalse;
}
2011-12-27 11:28:54 +00:00
res = sqlite3_step(stmt);
if(G_Sql_Check_StepReturn(res)) {
return qfalse;
}
sqlite3_finalize(stmt);
2011-12-16 20:41:27 +00:00
2011-12-27 11:28:54 +00:00
return qtrue;
2011-06-01 12:20:56 +00:00
}
/*
===============
2011-12-16 10:43:16 +00:00
G_Sql_UserDB_login
2011-06-01 12:20:56 +00:00
===============
*/
2011-12-16 20:09:23 +00:00
qboolean G_Sql_UserDB_login(const char *uName, const char *pwd, int clientnum) {
2012-03-19 22:13:06 +00:00
sqlite3_stmt *stmt;
int res, id;
char *hashedpw;
res = sqlite3_prepare_v2(user_db, SQL_USER_LOGIN, -1, &stmt, 0);
if(G_Sql_Check_PrepareReturn(res)) {
return qfalse;
}
res = sqlite3_bind_text(stmt, 1, uName, sizeof(uName), SQLITE_STATIC);
if(G_Sql_Check_BindReturn(res)) {
return qfalse;
}
hashedpw = G_Sql_Md5(pwd);
res = sqlite3_bind_text(stmt, 2, hashedpw, sizeof(hashedpw), SQLITE_STATIC);
if(G_Sql_Check_BindReturn(res)) {
return qfalse;
}
res = sqlite3_step(stmt);
if(G_Sql_Check_StepReturn(res)) {
return qfalse;
}
if(res == SQLITE_ROW) {
id = sqlite3_column_int(stmt, 0);
level.clients[clientnum].uid = id;
return qtrue;
} else {
return qfalse;
}
2011-12-27 11:28:54 +00:00
2011-06-01 12:20:56 +00:00
return qfalse;
}
/*
===============
2011-12-16 10:43:16 +00:00
G_Sql_UserDB_CheckRight
2011-06-01 12:20:56 +00:00
===============
*/
2011-12-16 20:09:23 +00:00
qboolean G_Sql_UserDB_CheckRight(int uid, int right) {
2012-03-19 22:13:06 +00:00
sqlite3_stmt *stmt;
int res;
long rights;
res = sqlite3_prepare_v2(user_db, SQL_USER_GET_RIGHTS, -1, &stmt, 0);
if(G_Sql_Check_PrepareReturn(res)) {
return qfalse;
}
res = sqlite3_bind_int(stmt, 1, uid);
if(G_Sql_Check_BindReturn(res)) {
return qfalse;
}
res = sqlite3_step(stmt);
if(G_Sql_Check_StepReturn(res)) {
return qfalse;
}
if(res == SQLITE_ROW) {
rights = (long)sqlite3_column_int64;
if(right & right) {
return qtrue;
} else {
return qfalse;
}
} else {
return qfalse;
}
2011-06-01 12:20:56 +00:00
return qfalse;
}
2011-12-27 11:28:54 +00:00
/*
===============
G_Sql_UserDB_AddRight
===============
*/
qboolean G_Sql_UserDB_AddRight(int uid, int right) {
}
/*
===============
G_Sql_UserDB_RemoveRight
===============
*/
qboolean G_Sql_UserDB_RemoveRight(int uid, int right) {
}
2011-06-01 12:20:56 +00:00
#endif //SQL