diff options
author | Cyrille Bagard <nocbos@gmail.com> | 2021-05-30 23:40:39 (GMT) |
---|---|---|
committer | Cyrille Bagard <nocbos@gmail.com> | 2021-05-30 23:40:39 (GMT) |
commit | 1865cce4d51b9c7a6fb718f4e2c034a57365ad1b (patch) | |
tree | c82b56f65504596c1ab3e3ef426cca55bca40a62 /src/analysis/db/controller.c | |
parent | 3a9cf58a75c87b9046c1646a97b798508619fd62 (diff) |
Create a command to list remote binaries.
Diffstat (limited to 'src/analysis/db/controller.c')
-rw-r--r-- | src/analysis/db/controller.c | 122 |
1 files changed, 77 insertions, 45 deletions
diff --git a/src/analysis/db/controller.c b/src/analysis/db/controller.c index 46b628d..a27c20a 100644 --- a/src/analysis/db/controller.c +++ b/src/analysis/db/controller.c @@ -26,6 +26,7 @@ #include <assert.h> #include <errno.h> +#include <dirent.h> #include <malloc.h> #include <poll.h> #include <pthread.h> @@ -36,6 +37,8 @@ #include "backend-int.h" +#include "misc/rlestr.h" +#include "../../common/leb128.h" #include "../../common/packed.h" #include "../../core/logs.h" @@ -80,6 +83,9 @@ static void *g_cdb_controller_process(GCdbController *); /* Prend en compte une connexion nouvelle d'un utilisateur. */ static void g_cdb_controller_add_client(GCdbController *, SSL *, const char *, const char *); +/* Envoie au client la liste des binaires présents. */ +static bool g_cdb_controller_send_existing_binaries(GCdbController *, packed_buffer_t *); + /* Indique le type défini pour une gestion d'archives. */ @@ -238,12 +244,10 @@ static void *g_cdb_controller_process(GCdbController *controller) GServerBackend *base; /* Base de l'instance */ struct pollfd fds[3]; /* Surveillance des flux */ int ret; /* Bilan d'un appel */ - packed_buffer in_pbuf; /* Tampon de réception */ - uint32_t tmp32; /* Valeur sur 32 bits */ + packed_buffer_t in_pbuf; /* Tampon de réception */ bool status; /* Bilan de lecture initiale */ uint32_t command; /* Commande de la requête */ - DBError error; /* Bilan d'une opération */ - packed_buffer out_pbuf; /* Tampon d'émission */ + packed_buffer_t out_pbuf; /* Tampon d'émission */ char *msg; /* Erreur à faire remonter */ base = G_SERVER_BACKEND(controller); @@ -305,27 +309,23 @@ static void *g_cdb_controller_process(GCdbController *controller) switch (command) { - case DBC_LIST_ARCHIVES: - - /* - error = g_cdb_controller_write(archive); + case DBC_LIST_BINARIES: - init_packed_buffer(&out_pbuf); + init_packed_buffer(&out_pbuf); - status = extend_packed_buffer(&out_pbuf, (uint32_t []) { DBC_SAVE }, - sizeof(uint32_t), true); - if (!status) goto reply_error; + status = extend_packed_buffer(&out_pbuf, (uint32_t []) { DBC_EXISTING_BINARIES }, + sizeof(uint32_t), true); + if (!status) goto reply_error; - status = extend_packed_buffer(&out_pbuf, (uint32_t []) { error }, sizeof(uint32_t), true); - if (!status) goto reply_error; + status = g_cdb_controller_send_existing_binaries(controller, &out_pbuf); + if (!status) goto reply_error; - status = ssl_send_packed_buffer(&out_pbuf, controller->tls_fd); - if (!status) goto reply_error; - */ + status = ssl_send_packed_buffer(&out_pbuf, controller->tls_fd); + if (!status) goto reply_error; - exit_packed_buffer(&out_pbuf); + exit_packed_buffer(&out_pbuf); - break; + break; default: asprintf(&msg, _("Bad protocol command: 0x%08x"), command); @@ -397,16 +397,12 @@ static void g_cdb_controller_add_client(GCdbController *controller, SSL *fd, con } - - - -#if 0 /****************************************************************************** * * -* Paramètres : controller = archive à connecter avec un utilisateur. * -* pbuf = paquet à consituer pour un envoi unique. [OUT] * +* Paramètres : controller = administration d'archives d'analyse. * +* pbuf = paquet à consituer pour un envoi unique. [OUT] * * * -* Description : Envoie à tous les clients la nouvelle liste d'instantanés. * +* Description : Envoie au client la liste des binaires présents. * * * * Retour : Bilan de constitution de la réponse. * * * @@ -414,36 +410,72 @@ static void g_cdb_controller_add_client(GCdbController *controller, SSL *fd, con * * ******************************************************************************/ -static bool g_cdb_controller_send_snapshot_update(GCdbController *controller, packed_buffer *pbuf) +static bool g_cdb_controller_send_existing_binaries(GCdbController *controller, packed_buffer_t *pbuf) { bool result; /* Bilan à retourner */ - bool do_send; /* Réalisation de l'émission */ - packed_buffer out_pbuf; /* Tampon d'émission */ + DIR *directory; /* Répertoire de travail */ + uleb128_t count; /* Nombre d'éléments détectés */ + packed_buffer_t items; /* Liste des éléments trouvés */ + struct dirent *item; /* Propriétés d'un élément */ + rle_string name; /* Nom à exporter */ + bool status; /* Bilan d'une inscription */ + int ret; /* Bilan de fermture */ + + result = false; + + directory = opendir(controller->basedir); + if (directory == NULL) + { + LOG_ERROR_N("opendir"); - do_send = (pbuf == NULL); + if (errno == ENOENT) + { + count = 0; + result = pack_uleb128(&count, pbuf); + } - if (pbuf == NULL) - pbuf = &out_pbuf; + goto bad_dir; - init_packed_buffer(pbuf); + } - result = extend_packed_buffer(pbuf, (uint32_t []) { DBC_SNAPSHOTS_UPDATED }, - sizeof(uint32_t), true); - if (!result) goto bad_reply; + count = 0; + init_packed_buffer(&items); - result = g_db_snapshot_pack_all(archive->snapshot, pbuf); - if (!result) goto bad_reply; + for (item = readdir(directory); item != NULL; item = readdir(directory)) + { + if (item->d_type != DT_DIR) + continue; - result = extend_packed_buffer(pbuf, SNAPSHOT_END_MARK, SNAP_ID_HEX_SZ, false); - if (!result) goto bad_reply; + if (strcmp(item->d_name, ".") == 0 || strcmp(item->d_name, "..") == 0) + continue; - bad_reply: + init_static_rle_string(&name, item->d_name); - if (do_send || !result) - exit_packed_buffer(pbuf); + status = pack_rle_string(&name, &items); + + exit_rle_string(&name); + + if (!status) + goto reg_error; + + count++; + + } + + result = pack_uleb128(&count, pbuf); + + if (result) + result = include_packed_buffer(pbuf, &items); + + reg_error: + + exit_packed_buffer(&items); + + ret = closedir(directory); + if (ret == -1) LOG_ERROR_N("closedir"); + + bad_dir: return result; } - -#endif |