rpg-x2/game/g_sql.c

272 lines
5.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-16 10:43:16 +00:00
/*
===============
G_Sql_Md5
===============
*/
char *G_Sql_Md5(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);
if(res) {
G_Printf(S_COLOR_RED "SQL ERROR: Could not open user database\n");
return qfalse;
}
2011-12-16 10:43:16 +00:00
res = sqlite3_prepare_v2(user_db, SQL_ENABLE_FOREIGN_KEY_CONSTRAINTS, -1, &stmt, 0);
if(res) {
G_Printf(S_COLOR_RED "SQL ERROR: Enabling foreign key constraints failed\n");
sqlite3_close(user_db);
sql_ready = qfalse;
return qfalse;
}
2011-12-16 20:09:23 +00:00
sqlite3_free(stmt);
res = sqlite3_prepare_v2(user_db, SQL_USER_CREATEUSERTABLE, -1, &stmt, 0);
if(res) {
G_Printf(S_COLOR_RED "SQL ERROR: Query failed\n");
sqlite3_close(user_db);
sql_ready = qfalse;
return qfalse;
}
sqlite3_free(stmt);
res = sqlite3_prepare_v2(user_db, SQL_USER_CREATERIGHTSTABLE, -1, &stmt, 0);
if(res) {
G_Printf(S_COLOR_RED "SQL ERROR: Query failed\n");
sqlite3_close(user_db);
sql_ready = qfalse;
return qfalse;
}
sqlite3_free(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-16 20:41:27 +00:00
int res, i, id = -1;
2011-12-16 10:43:16 +00:00
2011-12-16 20:09:23 +00:00
res = sqlite3_prepare_v2(user_db, SQL_USER_GETUID(uName), -1, &stmt, 0);
2011-12-16 10:43:16 +00:00
if(res) {
G_Printf(S_COLOR_RED "SQL ERROR: Query failed\n");
return qfalse;
}
2011-12-16 20:41:27 +00:00
res = sqlite3_step(stmt);
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;
} else {
G_Printf(S_COLOR_RED "SQL ERROR: An error occured getting results from a row\n");
return qfalse;
}
2011-12-16 10:43:16 +00:00
sqlite3_free(stmt);
if(id == -1) {
G_Printf(S_COLOR_RED "SQL ERROR: no user %s found\n", uName);
return qfalse;
}
res = sqlite3_prepare_v2(user_db, SQL_USER_DELTE_RIGHTS(id), -1, &stmt, 0);
if(res) {
G_Printf(S_COLOR_RED "SQL ERROR: Query failed\n");
return qfalse;
}
sqlite3_free(stmt);
res = sqlite3_prepare_v2(user_db, SQL_USER_DELETE(uName), -1, &stmt, 0);
if(res) {
G_Printf(S_COLOR_RED "SQL ERROR: Query failed\n");
return qfalse;
}
sqlite3_free(stmt);
2011-06-01 12:20:56 +00:00
return qtrue;
}
/*
===============
2011-12-16 10:43:16 +00:00
G_Sql_UserDB_Mod
2011-06-01 12:20:56 +00:00
===============
*/
2011-12-16 20:09:23 +00:00
qboolean G_Sql_UserDB_Mod(const char *uName, const char *right, int value) {
2011-06-01 12:20:56 +00:00
return qfalse;
}
/*
===============
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;
res = sqlite3_prepare_v2(user_db, SQL_USER_GETUID(uName), -1, &stmt, 0);
if(res) {
G_Printf(S_COLOR_RED "SQL ERROR: Query failed\n");
return qfalse;
}
res = sqlite3_step(stmt);
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");
sqlite3_free(stmt);
return qfalse;
}
sqlite3_free(stmt);
res = sqlite3_prepare_v2(user_db, SQL_BEGIN_TRANSACTION, -1, &stmt, 0);
if(res) {
G_Printf(S_COLOR_RED "SQL ERROR: Query failed\n");
return qfalse;
}
sqlite3_free(stmt);
hashedpw = G_Sql_Md5((const char*)password);
res = sqlite3_prepare_v2(user_db, SQL_USER_ADD(uName, hashedpw), -1, &stmt, 0);
free(hashedpw);
if(res) {
G_Printf("SQL ERROR: User add query failed\n");
return qfalse;
}
res = sqlite3_prepare_v2(user_db, SQL_USER_GETUID(uName), -1, &stmt, 0);
if(res) {
G_Printf(S_COLOR_RED "SQL ERROR: Query failed\n");
return qfalse;
}
res = sqlite3_step(stmt);
if(res == SQLITE_ROW) {
id = sqlite3_column_int(stmt, 0);
} else {
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");
sqlite3_free(stmt);
return qfalse;
}
sqlite3_free(stmt);
res = sqlite3_prepare_v2(user_db, SQL_USER_ADD_RIGHTS(id), -1, &stmt, 0);
if(res) {
G_Printf("SQL ERROR: Add user rights query failed\n");
res = sqlite3_prepare_v2(user_db, SQL_ROLLBACK_TRANSACTION, -1, &stmt, 0);
if(res) {
G_Printf(S_COLOR_RED "SQL ERROR: Query failed\n");
return qfalse;
}
return qfalse;
}
sqlite3_free(stmt);
res = sqlite3_prepare_v2(user_db, SQL_COMMIT_TRANSACTION, -1, &stmt, 0);
if(res) {
G_Printf(S_COLOR_RED "SQL ERROR: Query failed\n");
return qfalse;
}
sqlite3_free(stmt);
2011-06-01 12:20:56 +00:00
return qfalse;
}
/*
===============
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) {
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) {
2011-06-01 12:20:56 +00:00
return qfalse;
}
#endif //SQL