updates CMake configuration for SQLite; sqlite: open or create table function.
This commit is contained in:
+2
-1
@@ -10,11 +10,12 @@ target_sources(zbbs-server
|
||||
)
|
||||
|
||||
add_subdirectory(libs)
|
||||
add_subdirectory(db)
|
||||
|
||||
target_link_libraries(zbbs-server
|
||||
PRIVATE
|
||||
db
|
||||
murmur3
|
||||
PostgreSQL::PostgreSQL
|
||||
OpenSSL::Crypto
|
||||
)
|
||||
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
add_library(db STATIC)
|
||||
|
||||
if (USE_POSTGRESQL)
|
||||
target_link_libraries(db
|
||||
PRIVATE
|
||||
PostgreSQL::PostgreSQL
|
||||
)
|
||||
else()
|
||||
target_link_libraries(db
|
||||
PRIVATE
|
||||
sqlite
|
||||
)
|
||||
endif()
|
||||
|
||||
target_sources(db
|
||||
PRIVATE
|
||||
"db.c"
|
||||
PUBLIC
|
||||
FILE_SET HEADERS
|
||||
FILES
|
||||
"db.h"
|
||||
)
|
||||
@@ -0,0 +1,5 @@
|
||||
#ifdef USE_POSTGRESQL
|
||||
#include "db_postgresql.c"
|
||||
#else
|
||||
#include "db_sqlite.c"
|
||||
#endif
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
#ifndef DB_H
|
||||
#define DB_H
|
||||
#include "main.h"
|
||||
#include "../main.h"
|
||||
#include <stdint.h>
|
||||
|
||||
/* Auth */
|
||||
@@ -1,8 +1,8 @@
|
||||
/* SPDX-License-Identifier: MIT */
|
||||
/* Copyright (c) 2026 Oleksandr Zhylin */
|
||||
|
||||
#include "db.h"
|
||||
#include "main.h"
|
||||
#include <db.h>
|
||||
#include "../main.h"
|
||||
#include <endian.h>
|
||||
#include <fcntl.h>
|
||||
#include <libpq-fe.h>
|
||||
@@ -24,9 +24,7 @@
|
||||
static PGconn *conn = NULL;
|
||||
static PGresult *res = NULL;
|
||||
|
||||
// #define USR_NOT_FOUND "User with such credentials is not found!"
|
||||
|
||||
static void clearRes() {
|
||||
static void clearRes(void) {
|
||||
PQclear(res);
|
||||
res = NULL;
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
/* SPDX-License-Identifier: MIT */
|
||||
/* Copyright (c) 2026 Oleksandr Zhylin */
|
||||
|
||||
#include "../main.h"
|
||||
#include <db.h>
|
||||
#include <endian.h>
|
||||
#include <openssl/sha.h>
|
||||
#include <sqlite3.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define UNUSED(x) (void)(x)
|
||||
#define Q_LEN 128
|
||||
#define BIN 1
|
||||
#define TEXT 0
|
||||
#define ENCR_SIZE 2048
|
||||
|
||||
static sqlite3 *db;
|
||||
|
||||
void print_err(char **err) {
|
||||
if (*err != NULL) {
|
||||
printf("%s\n", *err);
|
||||
sqlite3_free(*err);
|
||||
*err = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
int check_and_create_tables() {
|
||||
int res = 0;
|
||||
char *err = NULL;
|
||||
|
||||
res = sqlite3_exec(db,
|
||||
"CREATE TABLE IF NOT EXISTS 'users' ("
|
||||
" 'id' SERIAL,"
|
||||
" 'username' VARCHAR(50) NOT NULL,"
|
||||
" 'password' CHARACTER(64) NOT NULL,"
|
||||
" 'email' VARCHAR(255) NOT NULL,"
|
||||
" 'created_at' TIMESTAMP NOT NULL,"
|
||||
" 'last_login' TIMESTAMP NULL,"
|
||||
" 'privileges' SMALLINT NOT NULL,"
|
||||
" CONSTRAINT 'users_pkey' PRIMARY KEY ('id'),"
|
||||
" CONSTRAINT 'users_email_key' UNIQUE ('email'),"
|
||||
" CONSTRAINT 'users_username_key' UNIQUE ('username')"
|
||||
"); ",
|
||||
NULL, NULL, &err);
|
||||
|
||||
print_err(&err);
|
||||
res = sqlite3_exec(db,
|
||||
"CREATE UNIQUE INDEX IF NOT EXISTS 'username_idx'"
|
||||
" ON 'users' ("
|
||||
" 'username' ASC"
|
||||
" );",
|
||||
NULL, NULL, &err);
|
||||
print_err(&err);
|
||||
res = sqlite3_exec(db,
|
||||
"CREATE UNIQUE INDEX IF NOT EXISTS 'email_idx' "
|
||||
"ON 'users' ("
|
||||
"'email' ASC"
|
||||
");",
|
||||
NULL, NULL, &err);
|
||||
print_err(&err);
|
||||
res = sqlite3_exec(db,
|
||||
"CREATE TABLE IF NOT EXISTS 'files' ("
|
||||
" 'id' SERIAL,"
|
||||
" 'user_id' INTEGER NULL,"
|
||||
" 'name' VARCHAR(255) NOT NULL,"
|
||||
" 'size' BIGINT NOT NULL,"
|
||||
" 'created_at' DATE NOT NULL,"
|
||||
" 'description' TEXT NULL,"
|
||||
" 'permissions' INTEGER NULL,"
|
||||
" 'hash' INTEGER NULL,"
|
||||
" CONSTRAINT 'files_pkey' PRIMARY KEY ('id'),"
|
||||
" CONSTRAINT 'fk_user' FOREIGN KEY ('user_id')"
|
||||
" REFERENCES 'users' ('id') ON DELETE NO ACTION"
|
||||
" ON UPDATE NO ACTION"
|
||||
");",
|
||||
NULL, NULL, &err);
|
||||
print_err(&err);
|
||||
return res;
|
||||
}
|
||||
|
||||
int init_db_connection(void) {
|
||||
int res = sqlite3_open("db.sql", &db);
|
||||
char *err = NULL;
|
||||
if (!res) {
|
||||
res = check_and_create_tables();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void SHA256_raw_to_string(const unsigned char *passHashed, char *restrict out) {
|
||||
int i;
|
||||
for (i = 0; i < 4; i++) {
|
||||
uint64_t *num_pointer = (uint64_t *)(passHashed + i * 8);
|
||||
sprintf(out + 16 * i, "%016lx", htobe64(*num_pointer));
|
||||
}
|
||||
}
|
||||
|
||||
void string_to_SHA256(const char *str, char *restrict out) {
|
||||
unsigned char md[SHA256_DIGEST_LENGTH];
|
||||
unsigned char *passHashed = SHA256((unsigned char *)str, strlen(str), md);
|
||||
SHA256_raw_to_string(passHashed, out);
|
||||
}
|
||||
|
||||
int32_t db_user_auth(i_auth_t *c, o_auth_t *r) { return 0; }
|
||||
|
||||
int32_t db_user_create(i_db_user_create *args) { return 0; }
|
||||
|
||||
int32_t db_save_file(session *s) { return 0; }
|
||||
|
||||
// TODO: get file
|
||||
s_file_t *db_get_file(i_get_file_db *arg) {
|
||||
s_file_t *sf = NULL;
|
||||
|
||||
return sf;
|
||||
}
|
||||
|
||||
uint64_t db_get_files_data(i_get_files_db *arg, fl_t **fl_start,
|
||||
uint64_t *full_count) {
|
||||
fl_t *fl_current = NULL;
|
||||
return 0;
|
||||
}
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
/* Copyright (c) 2026 Oleksandr Zhylin */
|
||||
|
||||
#include "file_p.h"
|
||||
#include "db.h"
|
||||
#include <db.h>
|
||||
#include "main.h"
|
||||
#include "session.h"
|
||||
#include <arpa/inet.h>
|
||||
|
||||
+2
-1
@@ -3,7 +3,7 @@
|
||||
|
||||
#include "main.h"
|
||||
#include "server.h"
|
||||
#include "db.h"
|
||||
#include <db.h>
|
||||
#include <stdio.h>
|
||||
|
||||
/* *** MAIN *** */
|
||||
@@ -12,6 +12,7 @@ int main(int argc, char *argv[]) {
|
||||
prepare_start(argc, argv);
|
||||
server_data.welcome_message = get_welcome_mes();
|
||||
init_db_connection();
|
||||
return 0;
|
||||
server_data.ls = start_server();
|
||||
/* TODO: display server port */
|
||||
printf("Server is started!\n");
|
||||
|
||||
+1
-1
@@ -3,7 +3,7 @@
|
||||
|
||||
#include "session.h"
|
||||
#include "client.h"
|
||||
#include "db.h"
|
||||
#include <db.h>
|
||||
#include "file_p.h"
|
||||
#include "main.h"
|
||||
#include "user.h"
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
/* SPDX-License-Identifier: MIT */
|
||||
/* Copyright (c) 2026 Oleksandr Zhylin */
|
||||
|
||||
#include "db.h"
|
||||
#include <db.h>
|
||||
#include "main.h"
|
||||
#include "session.h"
|
||||
#include <stdint.h>
|
||||
|
||||
Reference in New Issue
Block a user