summaryrefslogtreecommitdiff
path: root/src/analysis/db/server.c
diff options
context:
space:
mode:
authorCyrille Bagard <nocbos@gmail.com>2016-04-11 21:46:03 (GMT)
committerCyrille Bagard <nocbos@gmail.com>2016-04-11 21:46:03 (GMT)
commita4f2f3ec4b4cf7b894d6976c884fbc446396cd00 (patch)
tree0ef60529d585eb0e90b2df7eae60bdf4b1e327d3 /src/analysis/db/server.c
parent5ad85cf30b2355ca727904d1a0d25240283813b3 (diff)
Distinguished the internal server from the remote one using Unix sockets.
Diffstat (limited to 'src/analysis/db/server.c')
-rw-r--r--src/analysis/db/server.c226
1 files changed, 161 insertions, 65 deletions
diff --git a/src/analysis/db/server.c b/src/analysis/db/server.c
index 360b266..48126e8 100644
--- a/src/analysis/db/server.c
+++ b/src/analysis/db/server.c
@@ -32,6 +32,7 @@
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
+#include <sys/un.h>
#include "cdb.h"
@@ -39,6 +40,7 @@
#include "protocol.h"
#include "misc/rlestr.h"
#include "../../common/io.h"
+#include "../../common/xdg.h"
#include "../../core/params.h"
#include "../../gui/panels/log.h"
@@ -52,17 +54,17 @@ typedef struct _registered_user
} registered_user;
-/* Informations relatives à un client */
-typedef struct _cdb_client
+/* Format générique des adresses de connexion */
+typedef union _gen_sockaddr_t
{
- GDbServer *server; /* Accès facile en mémoire */
+ struct sockaddr_in inet_addr; /* Adresse d'écoute IPv4 */
+ struct sockaddr_un unix_addr; /* Adresse d'écoute Unix */
- int fd; /* Canal de communication */
- struct sockaddr_in peer; /* Adresse distante */
-
- GThread *thread; /* Procédure de traitement */
+} gen_sockaddr_t;
-} cdb_client;
+#ifndef UNIX_PATH_MAX
+# define UNIX_PATH_MAX 108
+#endif
/* Description de serveur à l'écoute (instance) */
struct _GDbServer
@@ -73,15 +75,16 @@ struct _GDbServer
size_t users_count; /* Nombre d'enregistrés */
int fd; /* Canal de communication */
- char *hostname; /* Désignation humaine */
+ int domain; /* Domaine du canal */
+ gen_sockaddr_t addr; /* Adresse d'écoute générique */
+ socklen_t sock_len; /* Taille de cette adresse */
char *desc; /* Désignation du serveur */
- struct sockaddr_in addr; /* Adresse d'écoute */
+
+ char *basedir; /* Répertoire de stockage */
GThread *listener; /* Procédure de traitement */
GList *archives; /* Liste des binaires ouverts */
- cdb_client **clients; /* Connexions en place */
- size_t count; /* Quantité de clients */
GMutex mutex; /* Verrou pour l'accès */
};
@@ -181,7 +184,7 @@ static void g_db_server_finalize(GDbServer *server)
{
g_db_server_unregister_all_user(server);
- free(server->hostname);
+ free(server->desc);
G_OBJECT_CLASS(g_db_server_parent_class)->finalize(G_OBJECT(server));
@@ -192,10 +195,8 @@ static void g_db_server_finalize(GDbServer *server)
* *
* Paramètres : author = utilisateur à représenter via le client. *
* kfile = clef menant à sa clef publique. *
-* host = hôte à représenter pour le service. *
-* port = port de connexion pour les clients. *
* *
-* Description : Prépare un serveur de BD pour les clients. *
+* Description : Prépare un serveur de BD pour les clients internes. *
* *
* Retour : Structure mise en place ou NULL en cas d'échec. *
* *
@@ -203,18 +204,97 @@ static void g_db_server_finalize(GDbServer *server)
* *
******************************************************************************/
-GDbServer *g_db_server_new(const char *author, char *kfile, const char *host, short port)
+GDbServer *g_db_server_new_internal(const char *author, char *kfile)
{
GDbServer *result; /* Adresse à retourner */
+ bool ret; /* Bilan d'un appel */
+ char *suffix; /* Suffixe pour un fichier */
+ char *sock_path; /* Chemin vers le canal UNIX */
+
+ result = g_object_new(G_TYPE_DB_SERVER, NULL);
+
+ /* Chargement du profil */
+
+ ret = g_db_server_register_user(result, author, kfile);
+ if (!ret) goto gdsni_error;
+
+ /* Détermination du point d'écoute */
+
+ result->domain = AF_UNIX;
+
+ asprintf(&suffix, "chrysalide" G_DIR_SEPARATOR_S ".internal_server.%d", getpid());
+ sock_path = get_xdg_config_dir(suffix);
+ free(suffix);
+
+ memset(&result->addr, 0, sizeof(struct sockaddr_un));
+
+ result->addr.unix_addr.sun_family = AF_UNIX;
+ strncpy(result->addr.unix_addr.sun_path, sock_path, UNIX_PATH_MAX - 1);
+
+ free(sock_path);
+
+ result->sock_len = sizeof(struct sockaddr_un);
+
+ /* Désignation humaine */
+
+ asprintf(&result->desc, "unix://.internal_server.%d", getpid());
+
+ /* Répertoire de stockage */
+
+ result->basedir = get_xdg_config_dir("chrysalide" G_DIR_SEPARATOR_S "cdbs");
+
+ return result;
+
+ gdsni_error:
+
+ g_object_unref(G_OBJECT(result));
+
+ return NULL;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : conf = fichier de configuration à interpréter. *
+* *
+* Description : Prépare un serveur de BD pour les clients distants. *
+* *
+* Retour : Structure mise en place ou NULL en cas d'échec. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+GDbServer *g_db_server_new_remote(const char *conf)
+{
+ GDbServer *result; /* Adresse à retourner */
+
+ char *host;
+ short port;
+
+
struct hostent *hp; /* Informations sur l'hôte */
size_t desclen; /* Taille de désignation */
const char *ip; /* Adresse IPv4 ou IPv6 */
result = g_object_new(G_TYPE_DB_SERVER, NULL);
- /* ... =*/g_db_server_register_user(result, author, kfile);
+ /* Chargement des profils */
- result->hostname = strdup(host);
+
+ /*
+ ret = g_db_server_register_user(result, author, kfile);
+ if (!ret) goto gdsni_error;
+ */
+
+ result->domain = AF_INET;
+
+ host = "localhost";
+ port = 9999;
+
+
+ /* Détermination du point d'écoute */
hp = gethostbyname(host);
if (hp == NULL)
@@ -223,23 +303,33 @@ GDbServer *g_db_server_new(const char *author, char *kfile, const char *host, sh
goto gdsn_error;
}
- result->addr.sin_family = hp->h_addrtype;
- memcpy(&result->addr.sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));
+ result->addr.inet_addr.sin_family = hp->h_addrtype;
+ memcpy(&result->addr.inet_addr.sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));
+
+ result->addr.inet_addr.sin_port = htons(port);
- result->addr.sin_port = htons(port);
+ result->sock_len = sizeof(struct sockaddr_in);
desclen = INET6_ADDRSTRLEN + 1 + 5 + 1;
result->desc = (char *)calloc(desclen, sizeof(char));
- ip = inet_ntop(AF_INET, &result->addr.sin_addr, result->desc, INET6_ADDRSTRLEN);
+ ip = inet_ntop(AF_INET, &result->addr.inet_addr.sin_addr, result->desc, INET6_ADDRSTRLEN);
if (ip == NULL)
{
perror("inet_ntop");
goto gdsn_error;
}
+ /* Désignation humaine */
+
snprintf(result->desc + strlen(ip), 1 + 5, ":%hu", port);
+ /* Répertoire de stockage */
+
+
+ result->basedir = strdup("/tmp/"); /* TODO */
+
+
return result;
gdsn_error:
@@ -382,10 +472,10 @@ static void *g_db_server_listener(GDbServer *server)
{
struct pollfd fds; /* Surveillance des flux */
int ret; /* Bilan d'un appel */
- struct sockaddr_in peer; /* Adresse cliente */
+ gen_sockaddr_t peer; /* Adresse cliente */
int fd; /* Canal établi vers un client */
- char source[INET6_ADDRSTRLEN]; /* Adresse du client (IPv4/6) */
const char *ip; /* Statut de la conversion */
+ char *peer_name; /* Désignation du correspondant*/
DBError error; /* Validation de la connexion */
GCdbArchive *archive; /* Destinataire final du client*/
uint32_t cmd; /* Commande initiale lue */
@@ -409,18 +499,31 @@ static void *g_db_server_listener(GDbServer *server)
if (fds.revents & (POLLIN | POLLPRI))
{
- fd = accept(server->fd, &peer, (socklen_t []) { sizeof(struct sockaddr_in) });
+ fd = accept(server->fd, (struct sockaddr *)&peer, (socklen_t []) { sizeof(gen_sockaddr_t) });
if (fd == -1)
{
perror("accept");
continue;
}
- ip = inet_ntop(AF_INET, &peer.sin_addr, source, sizeof(source));
- if (ip == NULL)
+ /* Construction d'une représentation */
+
+ if (*((sa_family_t *)&peer) == AF_UNIX)
+ peer_name = strdup(server->desc);
+
+ else if (*((sa_family_t *)&peer) == AF_INET)
{
- perror("inet_ntop");
- goto gdsl_error;
+ peer_name = (char *)calloc(INET6_ADDRSTRLEN + 1 + 5 + 1, sizeof(char));
+
+ ip = inet_ntop(AF_INET, &peer.inet_addr.sin_addr, peer_name, INET6_ADDRSTRLEN);
+ if (ip == NULL)
+ {
+ perror("inet_ntop");
+ goto gdsl_error;
+ }
+
+ snprintf(peer_name + strlen(ip), 1 + 5, ":%hu", ntohs(peer.inet_addr.sin_port));
+
}
error = DBE_NONE;
@@ -439,72 +542,72 @@ static void *g_db_server_listener(GDbServer *server)
if (!safe_recv(fd, &cmd, sizeof(uint32_t), 0))
{
- log_variadic_message(LMT_ERROR, _("Error while getting the initial command from '%s:%hu'..."),
- source, ntohs(peer.sin_port));
+ log_variadic_message(LMT_ERROR, _("Error while getting the initial command from '%s'..."),
+ peer_name);
error = DBE_BAD_EXCHANGE;
goto gdsl_error_sending;
}
if (!safe_recv(fd, &version, sizeof(uint32_t), 0))
{
- log_variadic_message(LMT_ERROR, _("Error while getting the protocol version from '%s:%hu'..."),
- source, ntohs(peer.sin_port));
+ log_variadic_message(LMT_ERROR, _("Error while getting the protocol version from '%s'..."),
+ peer_name);
error = DBE_BAD_EXCHANGE;
goto gdsl_error_sending;
}
if (!recv_rle_string(&hash, fd, 0))
{
- log_variadic_message(LMT_ERROR, _("Error while getting the binary hash from '%s:%hu'..."),
- source, ntohs(peer.sin_port));
+ log_variadic_message(LMT_ERROR, _("Error while getting the binary hash from '%s'..."),
+ peer_name);
error = DBE_BAD_EXCHANGE;
goto gdsl_error_sending;
}
if (!recv_rle_string(&user, fd, 0))
{
- log_variadic_message(LMT_ERROR, _("Error while getting the user name from '%s:%hu'..."),
- source, ntohs(peer.sin_port));
+ log_variadic_message(LMT_ERROR, _("Error while getting the user name from '%s'..."),
+ peer_name);
error = DBE_BAD_EXCHANGE;
goto gdsl_error_sending;
}
if (!safe_recv(fd, sig, RSA_USED_SIZE, 0))
{
- log_variadic_message(LMT_ERROR, _("Error while getting the signature from '%s:%hu'..."),
- source, ntohs(peer.sin_port));
+ log_variadic_message(LMT_ERROR, _("Error while getting the signature from '%s'..."),
+ peer_name);
error = DBE_BAD_EXCHANGE;
goto gdsl_error_sending;
}
if (be32toh(cmd) != DBC_HELO)
{
- log_variadic_message(LMT_ERROR, _("The client from '%s:%hu' did not introduce itself!"),
- source, ntohs(peer.sin_port));
+ log_variadic_message(LMT_ERROR, _("The client from '%s' did not introduce itself!"),
+ peer_name);
error = DBE_BAD_EXCHANGE;
goto gdsl_error_sending;
}
if (be32toh(version) != CDB_PROTOCOL_VERSION)
{
- log_variadic_message(LMT_ERROR, _("The client from '%s:%hu' does not use the same protocol: 0x%08x vs 0x%08x..."),
- source, ntohs(peer.sin_port), be32toh(version), CDB_PROTOCOL_VERSION);
+ log_variadic_message(LMT_ERROR, _("The client from '%s' does not use the same protocol: 0x%08x vs 0x%08x..."),
+ peer_name, be32toh(version), CDB_PROTOCOL_VERSION);
error = DBE_WRONG_VERSION;
goto gdsl_error_sending;
}
if (is_rle_string_empty(&hash))
{
- log_variadic_message(LMT_ERROR, _("The submitted binary hash from '%s:%hu' is empty!"),
- source, ntohs(peer.sin_port));
+ log_variadic_message(LMT_ERROR, _("The submitted binary hash from '%s' is empty!"),
+ peer_name);
error = DBE_BAD_EXCHANGE;
goto gdsl_error_sending;
}
if (is_rle_string_empty(&user))
{
- log_variadic_message(LMT_ERROR, _("No user is associated with the client from '%s:%hu'..."),
- source, ntohs(peer.sin_port));
+ log_variadic_message(LMT_ERROR, _("No user is associated with the client from '%s'..."),
+ peer_name);
error = DBE_BAD_EXCHANGE;
goto gdsl_error_sending;
}
@@ -533,7 +636,7 @@ static void *g_db_server_listener(GDbServer *server)
}
if (iter == NULL)
- archive = g_cdb_archive_new(server->desc, &hash, &user, &error);
+ archive = g_cdb_archive_new(server->basedir, &hash, &user, &error);
/**
* Le serveur doit répondre pour un message type :
@@ -563,6 +666,8 @@ static void *g_db_server_listener(GDbServer *server)
server->archives = g_list_append(server->archives, archive);
error = g_cdb_archive_add_client(archive, fd, &user);
+ free(peer_name);
+
exit_rle_string(&hash);
exit_rle_string(&user);
@@ -574,6 +679,8 @@ static void *g_db_server_listener(GDbServer *server)
gdsl_error:
+ free(peer_name);
+
exit_rle_string(&hash);
exit_rle_string(&user);
@@ -605,7 +712,7 @@ bool g_db_server_start(GDbServer *server)
int ret; /* Bilan d'un appel */
int backlog; /* Nombre de connexions maximal*/
- server->fd = socket(AF_INET, SOCK_STREAM, 0);
+ server->fd = socket(server->domain, SOCK_STREAM, 0);
if (server->fd == -1)
{
perror("socket");
@@ -616,10 +723,11 @@ bool g_db_server_start(GDbServer *server)
if (ret == -1)
{
perror("setsockopt");
+ exit(0);
goto gdss_error;
}
- ret = bind(server->fd, (struct sockaddr *)&server->addr, sizeof(struct sockaddr_in));
+ ret = bind(server->fd, (struct sockaddr *)&server->addr, server->sock_len);
if (ret == -1)
{
perror("bind");
@@ -638,8 +746,7 @@ bool g_db_server_start(GDbServer *server)
server->listener = g_thread_new("cdb_listener", (GThreadFunc)g_db_server_listener, server);
- log_variadic_message(LMT_PROCESS, _("Server started and listening at %s:%hu"),
- server->hostname, ntohs(server->addr.sin_port));
+ log_variadic_message(LMT_PROCESS, _("Server started and listening at %s"), server->desc);
return true;
@@ -667,9 +774,6 @@ bool g_db_server_start(GDbServer *server)
void g_db_server_stop(GDbServer *server)
{
- size_t i; /* Boucle de parcours */
- GThread *thread; /* Procédure de traitement */
-
if (server->fd != -1)
return;
@@ -678,14 +782,6 @@ void g_db_server_stop(GDbServer *server)
g_thread_join(server->listener);
- for (i = 0; i < server->count; i++)
- {
- /* Sauvegarde de la référene, qui peut disparaître */
- thread = server->clients[i]->thread;
-
- close(server->clients[i]->fd);
- g_thread_join(thread);
-
- }
+ /* TODO : s'occuper des archives ouvertes */
}