Add collector NMAP store submission

This commit is contained in:
Mario Fetka
2026-07-16 15:20:05 +02:00
parent 0f26be3eda
commit 63d1e19ef2
3 changed files with 102 additions and 0 deletions
+1
View File
@@ -16,6 +16,7 @@ add_custom_command(
add_executable(bongocollector
collector.c
external_accounts.c
external_store.c
${EXTERNAL_ACCOUNTS_SQL_C}
)
+88
View File
@@ -0,0 +1,88 @@
#include <config.h>
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <xpl.h>
#include <memmgr.h>
#include <bongoutil.h>
#include <bongoagent.h>
#include <nmap.h>
#include <nmlib.h>
#include <msgapi.h>
#include <connio.h>
#include <bongostore.h>
#include "external_store.h"
static int
SafeNmapToken(const char *value)
{
return value != NULL && value[0] != '\0' &&
strpbrk(value, "\r\n\"\\") == NULL;
}
static void
SetError(char *error, size_t error_size, const char *message)
{
if (error != NULL && error_size > 0) {
snprintf(error, error_size, "%s", message != NULL ? message : "unknown error");
}
}
int
CollectorStoreSubmitFile(const char *owner, const char *folder,
FILE *message, size_t message_size,
time_t received_at, char *error,
size_t error_size)
{
struct sockaddr_in store_address;
Connection *connection = NULL;
char response[CONN_BUFSIZE + 1];
int result = -1;
if (!SafeNmapToken(owner) || !SafeNmapToken(folder) || message == NULL ||
message_size == 0 || message_size > INT_MAX) {
SetError(error, error_size, "invalid external message submission parameters");
return -1;
}
if (MsgAuthGetUserStore(owner, &store_address) != 0) {
SetError(error, error_size, "cannot locate the user's Bongo store");
return -1;
}
connection = NMAPConnect(NULL, &store_address);
if (connection == NULL) {
SetError(error, error_size, "cannot connect to the user's Bongo store");
return -1;
}
if (!NMAPAuthenticateThenUserAndStore(connection, (unsigned char *) owner)) {
SetError(error, error_size, "cannot authenticate to the user's Bongo store");
goto done;
}
if (NMAPRunCommandF(connection, response, sizeof(response),
"WRITE \"/mail/%s\" %u %zu T%lu\r\n", folder,
STORE_DOCTYPE_MAIL, message_size,
(unsigned long) received_at) != 2002) {
SetError(error, error_size, "the Bongo store rejected the target folder");
goto done;
}
rewind(message);
if (ConnWriteFromFile(connection, message, (int) message_size) != (int) message_size ||
ConnFlush(connection) < 0) {
SetError(error, error_size, "cannot send the external message to the Bongo store");
goto done;
}
if (NMAPReadAnswer(connection, response, sizeof(response), TRUE) != 1000) {
SetError(error, error_size, "the Bongo store could not import the external message");
goto done;
}
SetError(error, error_size, "");
result = 0;
done:
NMAPQuit(connection);
ConnFree(connection);
return result;
}
+13
View File
@@ -0,0 +1,13 @@
#ifndef BONGO_COLLECTOR_EXTERNAL_STORE_H
#define BONGO_COLLECTOR_EXTERNAL_STORE_H
#include <stddef.h>
#include <stdio.h>
#include <time.h>
int CollectorStoreSubmitFile(const char *owner, const char *folder,
FILE *message, size_t message_size,
time_t received_at, char *error,
size_t error_size);
#endif