summaryrefslogtreecommitdiff
path: root/src/analysis/db/cdb.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/analysis/db/cdb.c')
-rw-r--r--src/analysis/db/cdb.c602
1 files changed, 602 insertions, 0 deletions
diff --git a/src/analysis/db/cdb.c b/src/analysis/db/cdb.c
new file mode 100644
index 0000000..67d4916
--- /dev/null
+++ b/src/analysis/db/cdb.c
@@ -0,0 +1,602 @@
+
+/* OpenIDA - Outil d'analyse de fichiers binaires
+ * cdb.h - prototypes pour la manipulation des archives au format CDB
+ *
+ * Copyright (C) 2014 Cyrille Bagard
+ *
+ * This file is part of OpenIDA.
+ *
+ * OpenIDA is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * OpenIDA is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Foobar. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#include "cdb.h"
+
+
+#include <archive.h>
+#include <archive_entry.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <malloc.h>
+#include <stdio.h>
+#include <sqlite3.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/stat.h>
+
+
+#include <config.h>
+
+
+#include "bookmark.h"
+#include "protocol.h"
+#include "../../common/cpp.h"
+#include "../../common/extstr.h"
+#include "../../common/io.h"
+#include "../../common/xdg.h"
+#include "../../common/xml.h"
+
+
+
+/* Fixe le tampon pour la lecture des fichiers à inclure */
+#define ARCHIVE_RBUF_SIZE 2048
+
+
+/* Description d'une archive d'éléments utilisateur (instance) */
+struct _GCdbArchive
+{
+ GObject parent; /* A laisser en premier */
+
+ char hash[65]; /* Empreinte SHA256 */
+
+
+ char *filename; /* Chemin d'accès à l'archive */
+
+ char *xml_desc; /* Fichier de description */
+ char *sql_db; /* Base de données SQLite */
+
+ xmlDocPtr xdoc; /* Document XML à créer */
+ xmlXPathContextPtr context; /* Contexte pour les recherches*/
+
+ sqlite3 *db; /* Base de données à manipuler */
+
+};
+
+/* Description d'une archive d'éléments utilisateur (classe) */
+struct _GCdbArchiveClass
+{
+ GObjectClass parent; /* A laisser en premier */
+
+};
+
+
+/* Initialise la classe des archives d'éléments utilisateur. */
+static void g_cdb_archive_class_init(GCdbArchiveClass *);
+
+/* Initialise une archive d'éléments utilisateur. */
+static void g_cdb_archive_init(GCdbArchive *);
+
+/* Supprime toutes les références externes. */
+static void g_cdb_archive_dispose(GCdbArchive *);
+
+/* Procède à la libération totale de la mémoire. */
+static void g_cdb_archive_finalize(GCdbArchive *);
+
+/* Ouvre une archive avec tous les éléments à conserver. */
+static bool g_cdb_archive_read(GCdbArchive *);
+
+
+
+
+
+/* Crée la description XML correspondant à l'archive. */
+static bool g_cdb_archive_create_xml_desc(GCdbArchive *, const core_db_info *);
+
+
+
+/* ------------------------- ACCES A LA BASE DE DONNEES SQL ------------------------- */
+
+/* Crée la base de données correspondant à l'archive. */
+static bool g_cdb_archive_create_db(const GCdbArchive *, const core_db_info *);
+
+
+
+
+/* Indique le type défini pour une une archive d'éléments utilisateur. */
+G_DEFINE_TYPE(GCdbArchive, g_cdb_archive, G_TYPE_OBJECT);
+
+
+/******************************************************************************
+* *
+* Paramètres : klass = classe à initialiser. *
+* *
+* Description : Initialise la classe des archives d'éléments utilisateur. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static void g_cdb_archive_class_init(GCdbArchiveClass *klass)
+{
+ GObjectClass *object; /* Autre version de la classe */
+
+ object = G_OBJECT_CLASS(klass);
+
+ object->dispose = (GObjectFinalizeFunc/* ! */)g_cdb_archive_dispose;
+ object->finalize = (GObjectFinalizeFunc)g_cdb_archive_finalize;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : archive = instance à initialiser. *
+* *
+* Description : Initialise une archive d'éléments utilisateur. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static void g_cdb_archive_init(GCdbArchive *archive)
+{
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : archive = instance d'objet GLib à traiter. *
+* *
+* Description : Supprime toutes les références externes. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static void g_cdb_archive_dispose(GCdbArchive *archive)
+{
+ G_OBJECT_CLASS(g_cdb_archive_parent_class)->dispose(G_OBJECT(archive));
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : archive = instance d'objet GLib à traiter. *
+* *
+* Description : Procède à la libération totale de la mémoire. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static void g_cdb_archive_finalize(GCdbArchive *archive)
+{
+ //void close_xml_file(xmlDocPtr, xmlXPathContextPtr);
+
+
+
+ G_OBJECT_CLASS(g_cdb_archive_parent_class)->finalize(G_OBJECT(archive));
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : local = indique si l'enregistrement est local ou non. *
+* client = flux ouvert en lecture pour les informations utiles.*
+* info = informations de base associées à la requête. *
+* *
+* Description : Définit ou ouvre une archive d'éléments utilisateur. *
+* *
+* Retour : Structure mise en plae ou NULL en cas d'échec. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+GCdbArchive *g_cdb_archive_new(bool local, GDbClient *client, const core_db_info *info)
+{
+ GCdbArchive *result; /* Adresse à retourner */
+ char *suffix; /* Fin du nom de fichier */
+ struct stat finfo; /* Information sur l'archive */
+ int ret; /* Retour d'un appel */
+
+ result = g_object_new(G_TYPE_CDB_ARCHIVE, NULL);
+
+
+
+ strcpy(result->hash, info->hash);
+
+
+
+
+ /* Chemin de l'archive */
+
+ suffix = strdup("chrysalide" G_DIR_SEPARATOR_S);
+ suffix = stradd(suffix, local ? "local" : "server");
+ suffix = stradd(suffix, G_DIR_SEPARATOR_S);
+ suffix = stradd(suffix, result->hash);
+ suffix = stradd(suffix, ".tar.xz");
+
+ result->filename = get_xdg_config_dir(suffix);
+
+ free(suffix);
+
+ if (!mkpath(result->filename))
+ goto gcan_error;
+
+ /* Chemin des enregistrements temporaires */
+
+ result->xml_desc = strdup(g_get_tmp_dir());
+
+ if (result->xml_desc[strlen(result->xml_desc) - 1] != G_DIR_SEPARATOR)
+ result->xml_desc = stradd(result->xml_desc, G_DIR_SEPARATOR_S);
+
+ result->xml_desc = stradd(result->xml_desc, result->hash);
+ result->xml_desc = stradd(result->xml_desc, "_desc.xml");
+
+ result->sql_db = strdup(g_get_tmp_dir());
+
+ if (result->sql_db[strlen(result->sql_db) - 1] != G_DIR_SEPARATOR)
+ result->sql_db = stradd(result->sql_db, G_DIR_SEPARATOR_S);
+
+ result->sql_db = stradd(result->sql_db, result->hash);
+ result->sql_db = stradd(result->sql_db, "_db.sql");
+
+ /* Création de l'archive si elle n'existe pas */
+
+ ret = stat(result->filename, &finfo);
+
+ if (ret != 0)
+ {
+ /* Le soucis ne vient pas de l'absence du fichier... */
+ if (errno != ENOENT) goto gcan_error;
+
+ g_cdb_archive_create_xml_desc(result, info);
+ g_cdb_archive_create_db(result, info);
+
+ if (!g_cdb_archive_write(result))
+ goto gcan_error;
+
+ }
+ else if (!S_ISREG(finfo.st_mode))
+ goto gcan_error;
+
+ /* Ouverture de l'archive */
+
+ if (!g_cdb_archive_read(result) && 0)
+ goto gcan_error;
+
+ return result;
+
+ gcan_error:
+
+ g_object_unref(G_OBJECT(result));
+
+ return NULL;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : archive = information quant à l'archive à interpréter. *
+* *
+* Description : Ouvre une archive avec tous les éléments à conserver. *
+* *
+* Retour : Bilan de l'opération. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static bool g_cdb_archive_read(GCdbArchive *archive)
+{
+ bool result; /* Conclusion à retourner */
+ struct archive *in; /* Archive à consulter */
+ int ret; /* Bilan d'un appel */
+ int flags; /* Propriétés à extraire */
+ struct archive *out; /* Extracteur générique */
+ struct archive_entry *entry; /* Elément de l'archive */
+ const char *path; /* Désignation d'un fichier */
+
+ result = false;
+
+ in = archive_read_new();
+ archive_read_support_filter_all(in);
+ archive_read_support_format_all(in);
+
+ ret = archive_read_open_filename(in, archive->filename, 10240 /* ?! */);
+ if (ret != ARCHIVE_OK) goto gcar_exit;
+
+ /* Propriétés à restaurer */
+ flags = ARCHIVE_EXTRACT_TIME;
+ flags |= ARCHIVE_EXTRACT_PERM;
+ flags |= ARCHIVE_EXTRACT_ACL;
+ flags |= ARCHIVE_EXTRACT_FFLAGS;
+
+ out = archive_write_disk_new();
+ archive_write_disk_set_options(out, flags);
+ archive_write_disk_set_standard_lookup(out);
+
+ for (ret = archive_read_next_header(in, &entry);
+ ret == ARCHIVE_OK;
+ ret = archive_read_next_header(in, &entry))
+ {
+ bool dump_arch_data(struct archive_entry *ent, struct archive *input, struct archive *output)
+ {
+ const void *buff; /* Tampon de copie */
+ size_t size; /* Quantité copiée */
+ __LA_INT64_T offset; /* Position de lecture */
+
+ ret = archive_write_header(output, entry);
+ if (ret != ARCHIVE_OK) return false;
+
+ for (ret = archive_read_data_block(input, &buff, &size, &offset);
+ ret == ARCHIVE_OK;
+ ret = archive_read_data_block(input, &buff, &size, &offset))
+ {
+ ret = archive_write_data_block(output, buff, size, offset);
+ if (ret != ARCHIVE_OK)
+ return false;
+ }
+
+ if (ret != ARCHIVE_EOF)
+ return false;
+
+ ret = archive_write_finish_entry(output);
+
+ return (ret == ARCHIVE_OK);
+
+ }
+
+ path = archive_entry_pathname(entry);
+
+ if (strcmp(path, "desc.xml") == 0)
+ {
+ archive_entry_set_pathname(entry, archive->xml_desc);
+
+ if (!dump_arch_data(entry, in, out))
+ goto gcar_exit;
+
+ if (!open_xml_file(archive->xml_desc, &archive->xdoc, &archive->context))
+ goto gcar_exit;
+
+ }
+ else if (strcmp(path, "sql.db") == 0)
+ {
+ archive_entry_set_pathname(entry, archive->sql_db);
+
+ if (!dump_arch_data(entry, in, out))
+ goto gcar_exit;
+
+ ret = sqlite3_open(archive->sql_db, &archive->db);
+ if (ret != SQLITE_OK)
+ goto gcar_exit;
+
+ }
+
+ }
+
+ archive_read_close(in);
+ archive_read_free(in);
+
+ archive_write_close(out);
+ archive_write_free(out);
+
+ result = true;
+
+ gcar_exit:
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : archive = information quant à l'archive à créer. *
+* *
+* Description : Enregistre une archive avec tous les éléments à conserver. *
+* *
+* Retour : Bilan de l'opération. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+bool g_cdb_archive_write(const GCdbArchive *archive)
+{
+ bool result; /* Conclusion à retourner */
+ struct archive *out; /* Archive à constituer */
+ int ret; /* Bilan d'un appel */
+
+ result = false;
+
+ out = archive_write_new();
+ archive_write_add_filter_xz(out);
+ archive_write_set_format_gnutar(out);
+
+ ret = archive_write_open_filename(out, archive->filename);
+ if (ret != ARCHIVE_OK) goto gcaw_exit;
+
+ bool add_file_to_archive(struct archive *out, const char *src, const char *path)
+ {
+ struct stat info; /* Informations d'origine */
+ struct archive_entry *entry; /* Elément de l'archive */
+ int fd; /* Flux ouvert en lecture */
+ char buffer[ARCHIVE_RBUF_SIZE]; /* Tampon pour les transferts */
+ ssize_t len; /* Quantité de données lues */
+
+ ret = stat(src, &info);
+ if (ret != 0) return false;
+
+ entry = archive_entry_new();
+
+ archive_entry_copy_stat(entry, &info);
+ archive_entry_set_pathname(entry, path);
+
+ ret = archive_write_header(out, entry);
+ if (ret != 0) goto afta_error;
+
+ fd = open(src, O_RDONLY);
+ if (fd == -1) goto afta_error;
+
+ for (len = safe_read(fd, buffer, ARCHIVE_RBUF_SIZE);
+ len > 0;
+ len = safe_read(fd, buffer, ARCHIVE_RBUF_SIZE))
+ {
+ if (archive_write_data(out, buffer, len) != len)
+ goto afta_error;
+ }
+
+ close(fd);
+
+ archive_entry_free(entry);
+
+ return true;
+
+ afta_error:
+
+ archive_entry_free(entry);
+
+ return false;
+
+ }
+
+ result = add_file_to_archive(out, archive->xml_desc, "desc.xml");
+ result &= add_file_to_archive(out, archive->sql_db, "sql.db");
+
+ gcaw_exit:
+
+ archive_write_free(out);
+
+ return result;
+
+}
+
+
+
+
+
+
+
+
+
+/* --------------------- OPERATIONS DE LECTURE D'UN FICHIER XML --------------------- */
+/* --------------------- OPERATIONS DE LECTURE D'UN FICHIER XML --------------------- */
+
+
+
+
+
+/******************************************************************************
+* *
+* Paramètres : archive = archive à constituer. *
+* info = informations de base associées à la requête. *
+* *
+* Description : Crée la description XML correspondant à l'archive. *
+* *
+* Retour : Bilan de l'opération. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static bool g_cdb_archive_create_xml_desc(GCdbArchive *archive, const core_db_info *info)
+{
+ bool result; /* Bilan à retourner */
+ char tmp[sizeof(STR(ULLONG_MAX))]; /* Stockage temporaire */
+
+ result = create_new_xml_file(&archive->xdoc, &archive->context);
+ if (!result) return false;
+
+ result &= add_content_to_node(archive->xdoc, archive->context,
+ "/ChrysalideBinary/Version", PACKAGE_VERSION);
+
+ result &= add_content_to_node(archive->xdoc, archive->context,
+ "/ChrysalideBinary/Hash", archive->hash);
+
+ result &= add_content_to_node(archive->xdoc, archive->context,
+ "/ChrysalideBinary/Creation/Author", "**me**");
+
+ snprintf(tmp, sizeof(tmp), "%" PRIu64, (uint64_t)10ull);
+
+ result &= add_content_to_node(archive->xdoc, archive->context,
+ "/ChrysalideBinary/Creation/Date", tmp);
+
+ save_xml_file(archive->xdoc, archive->xml_desc);
+
+ return result;
+
+}
+
+
+
+
+
+
+/* ---------------------------------------------------------------------------------- */
+/* ACCES A LA BASE DE DONNEES SQL */
+/* ---------------------------------------------------------------------------------- */
+
+
+/******************************************************************************
+* *
+* Paramètres : archive = archive à constituer. *
+* info = informations de base associées à la requête. *
+* *
+* Description : Crée la base de données correspondant à l'archive. *
+* *
+* Retour : Bilan de l'opération. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static bool g_cdb_archive_create_db(const GCdbArchive *archive, const core_db_info *info)
+{
+ bool result; /* Bilan à retourner */
+ sqlite3 *db; /* Base de données à constituer*/
+ int ret; /* Bilan de la création */
+
+ ret = sqlite3_open(archive->sql_db, &db);
+
+ if (ret != SQLITE_OK)
+ {
+ fprintf(stderr, "sqlite3_open(): %s\n", sqlite3_errmsg(db));
+ return false;
+ }
+
+
+
+ result = create_bookmark_db_table(db);
+
+
+
+ sqlite3_close(db);
+
+ return result;
+
+}