Travaux sur la base de donnée

This commit is contained in:
kitoy 2022-02-28 04:49:08 +01:00
parent 58385e7aa6
commit ab6de25744
6 changed files with 147 additions and 36 deletions

View File

@ -34,7 +34,7 @@
<!-- Markup example 2: input is after label -->
<label for="Login"> Login </label>
<input type="Login" id="Login" name="login" placeholder="Login" value="$login$" required>
<input type="login" name="login" id="login" placeholder="Email address" value="" required>
<label for="Password"> Mot de passe </label>
<input type="Password" id="Password" name="password" placeholder="Password" value="" required>

BIN
kore.core

Binary file not shown.

Binary file not shown.

View File

@ -4,10 +4,15 @@
#include <stdlib.h>
#include <unistd.h>
#include <sqlite3.h>
#include <unistd.h>
#include "assets.h"
#include "sessions.h"
#include "sqlite_utils.h"
#include <pwd.h>
#define SESSION_LEN 30
#define DB_NAME "portal_user.db"
#if defined(__linux__)
#include <kore/seccomp.h>
@ -22,25 +27,37 @@ KORE_SECCOMP_FILTER("sqlite3",
);
#endif
int init(int state);
int portal_user_load(struct http_request *);
int v_password_func(struct http_request *, char *);
int create_user(struct http_request *);
int v_session_validate(struct http_request *, char *);
int v_session_remove(struct http_request *, char *);
hashtable_t *hashtable = NULL;
int init(int state){
hashtable = ht_create( 65536 );
int err=0;
if( hashtable == NULL )
{
kore_log(LOG_ERR, "Can't create hastable sessions");
return (KORE_RESULT_ERROR);
err = 1;
}
return (KORE_RESULT_OK);
if ( access( DB_NAME, F_OK ) == 0 && err == 0 )
{ kore_log(LOG_NOTICE, "Execute check_db()");
err = check_db(DB_NAME);}
else
{
kore_log(LOG_NOTICE, "Create Sqlite Base");
err = init_db(DB_NAME);
}
if (err)
return (KORE_RESULT_ERROR);
else
return (KORE_RESULT_OK);
}
int create_user(struct http_request *req)
@ -49,11 +66,10 @@ int create_user(struct http_request *req)
u_int8_t *d = NULL;
size_t len = 0;
char *salt = NULL, *cryptpwd = NULL;
char *err_msg = NULL;
char *user = NULL, *pwd = NULL, name[10];
sqlite3_stmt *res = NULL;
char *zErrMsg = NULL;
char *user = NULL, *pwd = NULL, name[50];
sqlite3 *db = NULL;
char sql[512];
if (req->method == HTTP_METHOD_GET)
http_populate_get(req);
@ -77,7 +93,7 @@ int create_user(struct http_request *req)
if (req->method == HTTP_METHOD_POST)
{
int rc = sqlite3_open("test.db", &db);
int rc = sqlite3_open(DB_NAME, &db);
if (rc != SQLITE_OK) {
@ -109,26 +125,20 @@ int create_user(struct http_request *req)
kore_log(LOG_NOTICE, "Encrypted called %s", cryptpwd );
char *sql = "INSERT INTO users VALUES(?,?);";
rc = sqlite3_prepare_v2(db, sql, -1, &res, 0);
(void)sprintf(sql,
"INSERT INTO Users (Email, Password, Active) VALUES(\"%s\",\"%s\",\"yes\");",
user, cryptpwd);
if (rc == SQLITE_OK)
{
sqlite3_bind_text(res, 1, user, -1, NULL);
sqlite3_bind_text(res, 2, cryptpwd, -1, NULL);
}
else {
kore_log(LOG_ERR, "Failed to execute statement: %s\n", sqlite3_errmsg(db));
}
sqlite3_step(res);
if (rc != SQLITE_OK ) {
kore_log(LOG_ERR, "SQL error: %s\n", err_msg);
sqlite3_finalize(res);
sqlite3_free(err_msg);
sqlite3_close(db);
return (KORE_RESULT_ERROR);
printf ( "%s",sql);
rc = sqlite3_exec(db, sql, 0, 0, &zErrMsg);
if( rc != SQLITE_OK ){
kore_log(LOG_ERR, "SQL error: %s", zErrMsg);
sqlite3_free(zErrMsg);
} else {
kore_log(LOG_NOTICE, "Database successfully created !");
}
sqlite3_close(db);
@ -149,7 +159,7 @@ int portal_user_load(struct http_request *req)
struct kore_buf *b = NULL;
u_int8_t *d = NULL;
size_t len = 0;
char *login = NULL, *pwd = NULL, name[10];
char *login = NULL, *pwd = NULL, name[70];
char *err_msg = 0;
char salt[29], *cryptpwd = NULL;
int rc = 0;
@ -181,7 +191,7 @@ int portal_user_load(struct http_request *req)
if (req->method == HTTP_METHOD_POST)
{
rc = sqlite3_open("test.db", &db);
rc = sqlite3_open(DB_NAME, &db);
if (rc != SQLITE_OK)
{
@ -191,7 +201,7 @@ int portal_user_load(struct http_request *req)
return (KORE_RESULT_ERROR);
}
char *sql = "SELECT email_address, password FROM users WHERE email_address = ?";
char *sql = "SELECT Email, Password FROM Users WHERE Email = ?";
rc = sqlite3_prepare_v2(db, sql, -1, &res, 0);
@ -237,7 +247,7 @@ int portal_user_load(struct http_request *req)
else
{
kore_log(LOG_NOTICE, "on va faire le set cookie");
session_id = gen_session_id(30);
session_id = gen_session_id(SESSION_LEN);
ht_set(hashtable, session_id, login);
kore_log(LOG_NOTICE, "on a ajouté le sessions dans la hastable");
@ -276,8 +286,23 @@ int v_password_func(struct http_request *req, char *data)
}
int
v_session_validate(struct http_request *req, char *data)
int v_session_remove (struct http_request *req, char *data)
{
char buffer[SESSION_LEN];
kore_log(LOG_NOTICE, "v_session_remove: %s", data);
(void)snprintf(buffer, SESSION_LEN, "%s", data);
if (ht_get(hashtable, buffer) != NULL)
ht_delete(hashtable, buffer);
http_response_header(req, "content-type", "text/html");
http_response_header(req, "set-cookie", "session_id=");
http_response(req, 200, asset_index_html, asset_len_index_html);
return (KORE_RESULT_OK);
}
int v_session_validate(struct http_request *req, char *data)
{
kore_log(LOG_NOTICE, "v_session_validate: %s", data);

View File

@ -158,6 +158,7 @@ static char *ht_get( hashtable_t *hashtable, char *session_id ) {
}
void ht_delete (hashtable_t *hashtable, char *key){
int bin = 0;
@ -193,7 +194,7 @@ char *gen_session_id(int len){
c_tmp = rand() % sizeof(char1) - 1;
session_id[index] = char1[c_tmp];
}
session_id[len] = '\0';
res = strdup(session_id);
return res;

85
src/sqlite_utils.h Normal file
View File

@ -0,0 +1,85 @@
#include <kore/kore.h>
#include <sqlite3.h>
#include <stdio.h>
int init_db(const char *name);
int check_db(const char *name);
int init_db(const char *name)
{
sqlite3 *db = NULL;
char *zErrMsg = 0;
char *sql = "CREATE TABLE Users(" \
"Email CHAR(70) PRIMARY KEY NOT NULL, " \
"Password TEXT NOT NULL," \
"Email_rescue TEXT," \
"Alias TEXT," \
"XMPP TEXT, " \
"Active CHAR(3) NOT NULL, " \
"Token CHAR(30), " \
"Website TEXT, " \
"Date_create BIGINT );";
int rc = sqlite3_open(name, &db);
if (rc != SQLITE_OK) {
kore_log(LOG_ERR, "Cannot create database: %s\n",
sqlite3_errmsg(db));
sqlite3_close(db);
return (KORE_RESULT_ERROR);
}
rc = sqlite3_exec(db, sql, 0, 0, &zErrMsg);
if( rc != SQLITE_OK ){
kore_log(LOG_ERR, "SQL error: %s", zErrMsg);
sqlite3_free(zErrMsg);
} else {
kore_log(LOG_NOTICE, "Database successfully created !");
}
sqlite3_close(db);
return 0;
}
int check_db(const char *name){
sqlite3 *db = NULL;
char *zErrMsg = 0;
char *sql;
int rc = sqlite3_open(name, &db);
if (rc != SQLITE_OK) {
kore_log(LOG_ERR, "Cannot open database: %s\n",
sqlite3_errmsg(db));
sqlite3_close(db);
return (KORE_RESULT_ERROR);
}
sql = "SELECT EXISTS(SELECT 1 FROM Users WHERE Email=''); " \
"SELECT EXISTS(SELECT 1 FROM Users WHERE Password='');" \
"SELECT EXISTS(SELECT 1 FROM Users WHERE Email_rescue='');" \
"SELECT EXISTS(SELECT 1 FROM Users WHERE Alias='');" \
"SELECT EXISTS(SELECT 1 FROM Users WHERE XMPP='');" \
"SELECT EXISTS(SELECT 1 FROM Users WHERE Active='');" \
"SELECT EXISTS(SELECT 1 FROM Users WHERE Token='');" \
"SELECT EXISTS(SELECT 1 FROM Users WHERE Website='');" \
"SELECT EXISTS(SELECT 1 FROM Users WHERE Date_create='');";
rc = sqlite3_exec(db, sql, 0, 0, &zErrMsg);
if( rc != SQLITE_OK ){
kore_log(LOG_ERR, "SQL error: %s", zErrMsg);
sqlite3_free(zErrMsg);
return 1;
}
else
{
kore_log(LOG_NOTICE, "Check database is 0K!");
}
sqlite3_close(db);
return 0;
}