sinkserver/sink/sink20169.c

575 lines
18 KiB
C
Raw Normal View History

2021-04-21 18:51:12 +02:00
/*
* vim:sw=4:ts=4:et
*/
2021-02-11 11:07:33 +01:00
#define _DEFAULT_SOURCE
2021-04-21 18:51:12 +02:00
#define POSTGRESQL
// #define INFLUXDB
2021-02-08 14:53:49 +01:00
#include <stdio.h>
2021-02-09 18:37:11 +01:00
#include <stdlib.h>
2021-02-11 11:28:43 +01:00
#include <stdbool.h>
2021-02-10 11:12:02 +01:00
#include <unistd.h>
2021-02-10 11:13:36 +01:00
#include <errno.h>
#include <sys/socket.h>
#include <sys/types.h>
2021-02-10 11:18:49 +01:00
#include <netinet/in.h>
2021-02-10 11:20:02 +01:00
#include <string.h>
2021-02-11 11:07:33 +01:00
#include <getopt.h>
2021-02-11 12:16:14 +01:00
#include <pwd.h>
2021-02-08 15:41:06 +01:00
2021-02-09 18:38:00 +01:00
#include <libconfig.h>
2021-04-21 18:51:12 +02:00
#ifdef INFLUXDB
2021-02-10 15:26:56 +01:00
#include <curl/curl.h>
2021-04-21 18:51:12 +02:00
#endif
#ifdef POSTGRESQL
#include <libpq-fe.h>
#endif
2021-02-09 18:38:00 +01:00
2021-02-08 18:07:17 +01:00
#include <sinkStruct.h>
2021-02-09 18:32:58 +01:00
#include <logging.h>
2021-02-10 11:20:02 +01:00
#include <sha256.h>
2021-02-09 18:38:00 +01:00
2021-02-08 18:07:17 +01:00
2021-02-11 10:59:53 +01:00
const char DEFAULT_CONFIG_FILENAME[] = "./sink20169.cfg";
2021-02-10 17:23:59 +01:00
typedef struct {
const char *deviceId;
const char *location;
const char *sharedSecret;
2021-04-14 18:04:32 +02:00
int inactive;
2021-02-10 17:23:59 +01:00
} t_device;
2021-02-08 18:07:17 +01:00
2021-02-10 12:04:21 +01:00
typedef struct {
2021-02-10 17:23:59 +01:00
config_t cfg;
uint16_t numOfDevices;
t_device *devices;
} t_configHandle;
typedef struct {
t_configHandle *configHandle;
2021-02-10 12:04:21 +01:00
int receiveSockFd;
} t_receiverHandle;
2021-02-08 18:07:17 +01:00
2021-04-21 18:51:12 +02:00
#ifdef POSTGRESQL
#define NUM_OF_STMT_PARAMS 4
#endif
2021-02-10 12:04:21 +01:00
typedef struct {
2021-02-10 17:23:59 +01:00
t_configHandle *configHandle;
2021-04-21 18:51:12 +02:00
int32_t lowerBound;
int32_t upperBound;
#ifdef INFLUXDB
2021-02-10 12:15:27 +01:00
const char *influxUser;
const char *influxPass;
const char *influxServer;
2021-02-10 12:04:21 +01:00
uint16_t influxPort;
2021-02-10 12:15:27 +01:00
const char *influxDatabase;
const char *influxMeasurement;
2021-02-10 15:26:56 +01:00
char influxUrl[1024];
2021-04-21 18:51:12 +02:00
#endif
#ifdef POSTGRESQL
const char *postgresqlConnInfo;
PGconn *conn;
#endif
2021-02-10 12:04:21 +01:00
} t_forwarderHandle;
2021-02-11 11:28:43 +01:00
bool verbose = false;
2021-02-10 12:04:21 +01:00
2021-02-11 11:03:16 +01:00
int initConfig(const char *configFilename, t_configHandle *configHandle) {
2021-02-10 17:23:59 +01:00
configHandle->numOfDevices = 0;
configHandle->devices = NULL;
config_init(&(configHandle->cfg));
2021-02-11 10:59:53 +01:00
if (! config_read_file(&(configHandle->cfg), configFilename)) {
2021-02-10 11:12:02 +01:00
logmsg(LOG_ERR, "failed to read config file: %s:%d - %s\n",
2021-02-10 17:23:59 +01:00
config_error_file(&(configHandle->cfg)), config_error_line(&(configHandle->cfg)),
config_error_text(&(configHandle->cfg)));
config_destroy(&(configHandle->cfg));
2021-02-10 12:04:21 +01:00
return -1;
2021-02-10 11:12:02 +01:00
}
2021-02-10 17:23:59 +01:00
config_setting_t *devicesConfig = config_lookup(&(configHandle->cfg), "devices");
if (devicesConfig == NULL) {
logmsg(LOG_ERR, "receiver: no devices configuration found");
return -2;
}
configHandle->numOfDevices = config_setting_length(devicesConfig);
configHandle->devices = (t_device*) malloc(configHandle->numOfDevices * sizeof(t_device));
for (uint16_t i = 0; i < configHandle->numOfDevices; i++) {
2021-02-10 17:30:14 +01:00
config_setting_t *deviceConfig = config_setting_get_elem(devicesConfig, i);
if (! config_setting_lookup_string(deviceConfig, "deviceId", &(configHandle->devices[i].deviceId))) {
2021-02-10 17:23:59 +01:00
logmsg(LOG_ERR, "no deviceId for device %d", i);
2021-02-10 17:30:14 +01:00
return -3;
2021-02-10 17:23:59 +01:00
}
2021-02-10 17:30:14 +01:00
if (! config_setting_lookup_string(deviceConfig, "location", &(configHandle->devices[i].location))) {
2021-02-10 17:23:59 +01:00
logmsg(LOG_ERR, "no location for device %d", i);
2021-02-10 17:30:14 +01:00
return -4;
2021-02-10 17:23:59 +01:00
}
2021-02-10 17:30:14 +01:00
if (! config_setting_lookup_string(deviceConfig, "sharedSecret", &(configHandle->devices[i].sharedSecret))) {
2021-02-10 17:23:59 +01:00
logmsg(LOG_ERR, "no sharedSecret for device %d", i);
2021-02-10 17:30:14 +01:00
return -5;
2021-02-10 17:23:59 +01:00
}
if (strlen(configHandle->devices[i].sharedSecret) >= SHA256_BLOCK_SIZE) {
logmsg(LOG_ERR, "Configured sharedsecret for device %d is too long", i);
return -6;
}
2021-04-14 18:04:32 +02:00
if (! config_setting_lookup_bool(deviceConfig, "inactive", &(configHandle->devices[i].inactive))) {
logmsg(LOG_INFO, "no inactive flag set for device %d, consider as active", i);
configHandle->devices[i].inactive = 0;
}
logmsg(LOG_INFO, "Device loaded: %d %s %d %s %s", i,
2021-02-10 17:23:59 +01:00
configHandle->devices[i].deviceId,
2021-04-14 18:04:32 +02:00
configHandle->devices[i].inactive,
2021-02-10 17:23:59 +01:00
configHandle->devices[i].location,
configHandle->devices[i].sharedSecret);
}
2021-02-10 12:04:21 +01:00
return 0;
}
2021-02-08 18:07:17 +01:00
2021-02-10 17:23:59 +01:00
void deinitConfig(t_configHandle *configHandle) {
config_destroy(&(configHandle->cfg));
if (configHandle->devices) {
free(configHandle->devices);
configHandle->devices = NULL;
2021-02-10 11:12:02 +01:00
}
2021-02-10 17:23:59 +01:00
}
t_device *findDevice(t_configHandle *configHandle, char *deviceId) {
for (uint16_t i = 0; i < configHandle->numOfDevices; i++) {
2021-02-10 17:30:14 +01:00
if (! strcmp(configHandle->devices[i].deviceId, deviceId)) {
return &(configHandle->devices[i]);
2021-02-10 17:23:59 +01:00
}
}
return NULL;
}
int initReceiver(t_configHandle *configHandle, t_receiverHandle *handle) {
handle->configHandle = configHandle;
2021-02-08 14:53:49 +01:00
2021-02-10 11:12:02 +01:00
struct sockaddr_in servaddr;
2021-02-10 12:04:21 +01:00
handle->receiveSockFd = socket(AF_INET, SOCK_DGRAM, 0);
if (handle->receiveSockFd == -1) {
2021-02-10 11:12:02 +01:00
logmsg(LOG_ERR, "failed to create receive socket: %d", errno);
2021-02-10 12:04:21 +01:00
return -1;
2021-02-10 11:12:02 +01:00
}
int receivePort = 20169;
2021-02-10 17:23:59 +01:00
config_lookup_int(&(configHandle->cfg), "receivePort", &receivePort);
2021-02-10 11:12:02 +01:00
if (receivePort < 1 || receivePort > 65535) {
logmsg(LOG_ERR, "illegal receive port configured");
2021-02-10 12:04:21 +01:00
return -2;
2021-02-10 11:12:02 +01:00
}
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(receivePort);
2021-02-10 12:15:27 +01:00
if (-1 == bind(handle->receiveSockFd, (const struct sockaddr *) &servaddr, sizeof(servaddr))) {
2021-02-10 11:12:02 +01:00
logmsg(LOG_ERR, "unable to bind receive: %d", errno);
2021-02-10 12:04:21 +01:00
return -3;
2021-02-10 11:12:02 +01:00
}
2021-02-10 12:15:27 +01:00
return 0;
2021-02-10 11:12:02 +01:00
}
2021-02-10 12:06:03 +01:00
void deinitReceiver(t_receiverHandle *handle) {
close(handle->receiveSockFd);
}
2021-02-10 12:04:21 +01:00
int receiveAndVerifyMinuteBuffer(t_receiverHandle *handle, t_minuteBuffer *buf) {
2021-02-10 11:28:01 +01:00
struct sockaddr_in cliaddr;
2021-02-10 11:12:02 +01:00
socklen_t cliaddrlen = sizeof(cliaddr);
2021-02-10 12:04:21 +01:00
int n = recvfrom(handle->receiveSockFd, buf->b, sizeof(buf->b), MSG_TRUNC,
(struct sockaddr *) &cliaddr, &cliaddrlen);
2021-02-10 11:28:01 +01:00
logmsg(LOG_INFO, "received %d octets from %d.%d.%d.%d",
n,
(cliaddr.sin_addr.s_addr & 0x0ff),
((cliaddr.sin_addr.s_addr >> 8) & 0x0ff),
((cliaddr.sin_addr.s_addr >> 16) & 0x0ff),
((cliaddr.sin_addr.s_addr >> 24) & 0x0ff));
2021-02-10 11:12:02 +01:00
if (n != sizeof(buf->b)) {
logmsg(LOG_INFO, "Illegal packet size: %d", n);
return -1;
}
2021-02-10 17:23:59 +01:00
t_device *device = findDevice(handle->configHandle, buf->s.deviceId);
2021-03-22 17:36:26 +01:00
if (device == NULL) {
logmsg(LOG_ERR, "Device %s not found", buf->s.deviceId);
return -4;
}
2021-02-10 17:23:59 +01:00
const char *sharedSecret = device->sharedSecret;
2021-02-10 11:12:02 +01:00
uint8_t receivedHash[SHA256_BLOCK_SIZE];
memcpy(receivedHash, buf->s.hash, SHA256_BLOCK_SIZE);
memcpy(buf->s.hash, sharedSecret, SHA256_BLOCK_SIZE);
SHA256_CTX ctx;
uint8_t calculatedHash[SHA256_BLOCK_SIZE];
sha256_init(&ctx);
sha256_update(&ctx, buf->b, sizeof(buf->b));
sha256_final(&ctx, calculatedHash);
if (memcmp(receivedHash, calculatedHash, SHA256_BLOCK_SIZE) != 0) {
logmsg(LOG_INFO, "Invalid hash in msg for device %s", buf->s.deviceId);
return -5;
}
return 0;
2021-02-09 18:32:58 +01:00
}
2021-02-08 18:07:17 +01:00
2021-02-10 16:28:19 +01:00
2021-02-10 17:23:59 +01:00
int initForwarder(t_configHandle *configHandle, t_forwarderHandle *handle) {
handle->configHandle = configHandle;
2021-04-21 18:51:12 +02:00
#ifdef INFLUXDB
2021-02-10 17:23:59 +01:00
handle->influxUser = NULL;
handle->influxPass = NULL;
handle->influxServer = NULL;
handle->influxDatabase = NULL;
handle->influxMeasurement = NULL;
config_lookup_string(&(configHandle->cfg), "influxUser", &(handle->influxUser));
config_lookup_string(&(configHandle->cfg), "influxPass", &(handle->influxPass));
config_lookup_string(&(configHandle->cfg), "influxServer", &(handle->influxServer));
config_lookup_string(&(configHandle->cfg), "influxDatabase", &(handle->influxDatabase));
config_lookup_string(&(configHandle->cfg), "influxMeasurement", &(handle->influxMeasurement));
2021-02-10 12:15:27 +01:00
int influxPort = 8086;
2021-02-10 17:23:59 +01:00
config_lookup_int(&(configHandle->cfg), "influxPort", &influxPort);
2021-02-10 12:15:27 +01:00
if (influxPort < 1 || influxPort > 65535) {
logmsg(LOG_ERR, "illegal influx port configured");
return -2;
}
handle->influxPort = influxPort;
2021-02-10 12:04:21 +01:00
if (! handle->influxServer) {
2021-02-10 12:15:27 +01:00
logmsg(LOG_ERR, "no influxServer configured");
2021-02-10 12:04:21 +01:00
return -1;
}
if (! handle->influxDatabase) {
2021-02-10 12:15:27 +01:00
logmsg(LOG_ERR, "no influxDatabase configured");
2021-02-10 12:04:21 +01:00
return -2;
}
if (! handle->influxMeasurement) {
2021-02-10 12:15:27 +01:00
logmsg(LOG_ERR, "no influxMeasurement configured");
2021-02-10 12:04:21 +01:00
return -3;
}
2021-02-10 15:28:17 +01:00
int res = snprintf(handle->influxUrl, sizeof(handle->influxUrl),
2021-02-10 15:26:56 +01:00
"http://%s:%d/write?db=%s&precision=s",
handle->influxServer, handle->influxPort, handle->influxDatabase);
if (res > sizeof(handle->influxUrl)) {
logmsg(LOG_ERR, "influxUrl has not enough space");
return -4;
}
logmsg(LOG_INFO, "influxUrl is %s", handle->influxUrl);
2021-04-21 18:51:12 +02:00
#endif // INFLUXDB
#ifdef POSTGRESQL
handle->postgresqlConnInfo = NULL;
config_lookup_string(&(configHandle->cfg), "postgresqlConnInfo", &(handle->postgresqlConnInfo));
if (! handle->postgresqlConnInfo) {
logmsg(LOG_ERR, "no postgresql connInfo configured");
return -1;
}
handle->conn = NULL;
#endif // POSTGRESQL
2021-02-10 15:26:56 +01:00
2021-03-15 17:20:39 +01:00
handle->lowerBound = 45000;
config_lookup_int(&(configHandle->cfg), "lowerBound", &(handle->lowerBound));
handle->upperBound = 55000;
config_lookup_int(&(configHandle->cfg), "upperBound", &(handle->upperBound));
logmsg(LOG_INFO, "lowerBound: %u, upperBound: %u", handle->lowerBound, handle->upperBound);
2021-03-15 17:16:19 +01:00
2021-02-10 12:04:21 +01:00
return 0;
}
2021-02-10 15:26:56 +01:00
void deinitForwarder(t_forwarderHandle *handle) {
}
2021-04-21 18:51:12 +02:00
#ifdef INFLUXDB
2021-02-10 17:55:10 +01:00
int httpPostRequest(char *url, const char *user, const char *pass, char *payload) {
2021-02-10 15:53:31 +01:00
CURL *curl = curl_easy_init();
if (! curl) {
logmsg(LOG_ERR, "error instantiating curl");
return -1;
}
curl_easy_setopt(curl, CURLOPT_URL, url);
if (user && pass) {
2021-03-22 17:36:26 +01:00
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
2021-02-10 15:53:31 +01:00
curl_easy_setopt(curl, CURLOPT_USERNAME, user);
curl_easy_setopt(curl, CURLOPT_PASSWORD, pass);
}
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, payload);
CURLcode res = curl_easy_perform(curl);
if (res != CURLE_OK) {
logmsg(LOG_ERR, "post request failed: %s", curl_easy_strerror(res));
return -2;
}
curl_easy_cleanup(curl);
return 0;
}
2021-04-21 18:51:12 +02:00
int sendToDB(t_forwarderHandle *handle, const char *location, const char *deviceId,
uint32_t frequency, uint64_t timestamp) {
int frequency_before_point = frequency / 1000;
int frequency_behind_point = frequency - (frequency_before_point * 1000);
char payload[256];
int res = snprintf(payload, sizeof(payload),
"%s,valid=1,location=%s,host=%s freq=%d.%03d"
#ifdef OpenBSD
" %llu"
#else
" %lu"
#endif
"",
handle->influxMeasurement, location, deviceId,
frequency_before_point, frequency_behind_point,
timestamp);
if (res > sizeof(payload)) {
logmsg(LOG_ERR, "payload buffer to small");
return -1;
}
logmsg(LOG_DEBUG, "Payload: %s", payload);
res = httpPostRequest(handle->influxUrl, handle->influxUser, handle->influxPass, payload);
if (res == 0) {
logmsg(LOG_DEBUG, "Successfully sent to InfluxDB");
}
return res;
}
#endif // INFLUXDB
#ifdef POSTGRESQL
int openDatabaseConnection(t_forwarderHandle *handle) {
int res = 0;
if (! handle->conn) {
logmsg(LOG_DEBUG, "Opening connection to database");
handle->conn = PQconnectdb(handle->postgresqlConnInfo);
} else if (PQstatus(handle->conn) != CONNECTION_OK) {
logmsg(LOG_DEBUG, "Resetting connection to database");
PQreset(handle->conn);
}
if (PQstatus(handle->conn) != CONNECTION_OK) {
logmsg(LOG_ERR, "Connection to database failed: %s", PQerrorMessage(handle->conn));
res = -1;
}
return res;
}
int sendToDB(t_forwarderHandle *handle, const char *location, const char *deviceId,
uint32_t frequency, uint64_t timestamp) {
int retcode = 0;
if (0 == openDatabaseConnection(handle)) {
int frequency_before_point = frequency / 1000;
int frequency_behind_point = frequency - (frequency_before_point * 1000);
char stmt[256];
int res1 = snprintf(stmt, sizeof(stmt),
"INSERT INTO mainsfrequency (time, host, location, freq) "
2021-04-22 16:55:44 +02:00
"VALUES(to_timestamp("
#ifdef OpenBSD
"%llu"
#else
"%lu"
#endif
"), '%s', '%s', %d.%03d)",
2021-04-21 18:51:12 +02:00
timestamp, deviceId, location,
frequency_before_point, frequency_behind_point);
if (res1 > sizeof(stmt)) {
logmsg(LOG_ERR, "stmt buffer to small");
retcode = -1;
} else {
logmsg(LOG_DEBUG, "Statement: %s", stmt);
PGresult *res2 = PQexec(handle->conn, stmt);
if (PQresultStatus(res2) != PGRES_COMMAND_OK) {
logmsg(LOG_ERR, "Failed to insert into database (%s), data lost",
PQresultErrorMessage(res2));
retcode = -2;
}
PQclear(res2);
}
} else {
logmsg(LOG_ERR, "No database connection available, data lost");
retcode = -1;
}
return retcode;
}
#endif // POSTGRESQL
2021-02-10 12:04:21 +01:00
int forwardMinuteBuffer(t_forwarderHandle *handle, t_minuteBuffer *buf) {
2021-02-10 17:23:59 +01:00
t_device *device = findDevice(handle->configHandle, buf->s.deviceId);
2021-03-22 17:36:26 +01:00
if (device == NULL) {
logmsg(LOG_ERR, "Device %s not found", buf->s.deviceId);
return -4;
}
2021-02-10 17:23:59 +01:00
const char *location = device->location;
2021-02-10 16:28:19 +01:00
2021-03-22 17:36:26 +01:00
logmsg(LOG_INFO, "D: %s, R: %u, P: %u, W: %u, V: %08x, L: %s",
buf->s.deviceId, buf->s.totalRunningHours, buf->s.totalPowercycles, buf->s.totalWatchdogResets,
buf->s.version, location);
2021-04-22 16:10:25 +02:00
int sendSuccess = 0;
2021-04-17 19:02:25 +02:00
for (uint8_t j = 0; j < SECONDS_PER_MINUTE; j++) {
uint64_t timestamp = buf->s.timestamp + j;
logmsg(LOG_DEBUG, "Time: %lu, Frequency: %u", timestamp, buf->s.frequency[j]);
2021-04-14 18:04:32 +02:00
2021-04-17 19:02:25 +02:00
if (device->inactive == 0) {
2021-04-14 18:04:32 +02:00
if ((buf->s.frequency[j] >= handle->lowerBound) && (buf->s.frequency[j] <= handle->upperBound)) {
2021-04-22 16:10:25 +02:00
sendSuccess += sendToDB(handle, location, buf->s.deviceId, buf->s.frequency[j], timestamp);
2021-04-14 18:04:32 +02:00
} else {
logmsg(LOG_ERR, "%u out of bound, ignored", buf->s.frequency[j]);
2021-03-15 17:20:39 +01:00
}
2021-04-17 19:02:25 +02:00
} else {
2021-04-21 18:51:12 +02:00
logmsg(LOG_DEBUG, "Inactive device, not sent to database");
2021-02-10 17:54:33 +01:00
}
2021-02-10 11:12:02 +01:00
}
2021-04-17 19:02:25 +02:00
if (device->inactive == 0) {
2021-04-22 16:10:25 +02:00
if (sendSuccess == 0) {
logmsg(LOG_INFO, "Successfully sent whole minute to database");
} else {
logmsg(LOG_INFO, "Errors when sending to database, see above");
}
2021-04-17 19:02:25 +02:00
} else {
logmsg(LOG_INFO, "Not sent to database, device is marked as inactive");
}
2021-02-10 11:12:02 +01:00
return 0;
}
2021-02-10 15:26:56 +01:00
2021-02-11 11:36:26 +01:00
void usage() {
printf("sinkserver for mainsfrequency counter\n");
printf("https://home.hottis.de/gitlab/wolutator/mains-frequency-counter-stm32\n");
printf("Version: " VERSION "\n");
printf("\nUsage\n");
printf(" -f FILENAME R..... Config file to be used\n");
printf(" -v ............... Verbose, writes all logging on stdout too\n");
printf(" -s FACILITY ...... Sets syslog facility, only LOCAL[0..7]\n");
printf(" USER and DAEMON are supported\n");
2021-02-11 12:16:14 +01:00
printf(" -n USER .......... If started as root drop privileges and become\n");
printf(" USER\n");
2021-02-11 12:26:16 +01:00
printf(" -b ............... fork into background\n");
2021-02-11 11:36:26 +01:00
printf(" -h ............... This help\n");
}
2021-02-11 10:59:53 +01:00
int main(int argc, char **argv) {
2021-02-10 17:23:59 +01:00
t_configHandle configHandle;
t_forwarderHandle forwarderHandle;
t_receiverHandle receiverHandle;
2021-02-11 11:03:16 +01:00
const char *configFilename = DEFAULT_CONFIG_FILENAME;
2021-02-11 12:27:30 +01:00
const char *dropPrivilegesToUser = NULL;
2021-02-11 12:26:16 +01:00
bool doFork = false;
2021-02-11 10:59:53 +01:00
2021-02-11 11:00:30 +01:00
int c;
2021-02-11 12:28:32 +01:00
while ((c = getopt(argc, argv, "f:vs:hn:b")) != -1) {
2021-02-11 10:59:53 +01:00
switch (c) {
case 'f':
configFilename = strdup(optarg);
break;
2021-02-11 11:28:43 +01:00
case 'v':
verbose = true;
break;
case 's':
setfacility(optarg);
break;
2021-02-11 12:07:37 +01:00
case 'n':
2021-02-11 12:16:14 +01:00
dropPrivilegesToUser = strdup(optarg);
2021-02-11 12:07:37 +01:00
break;
2021-02-11 12:26:16 +01:00
case 'b':
doFork = true;
break;
2021-02-11 11:28:43 +01:00
case 'h':
2021-02-11 11:36:26 +01:00
usage();
2021-02-11 11:33:59 +01:00
exit(0);
2021-02-11 11:28:43 +01:00
break;
2021-02-11 10:59:53 +01:00
}
}
2021-02-11 12:16:14 +01:00
if ((getuid() == 0) && (dropPrivilegesToUser != NULL)) {
logmsg(LOG_INFO, "dropping root privileges, become %s", dropPrivilegesToUser);
2021-02-11 12:27:02 +01:00
struct passwd *userEntry = getpwnam(dropPrivilegesToUser);
2021-02-11 12:16:14 +01:00
if (userEntry == NULL) {
logmsg(LOG_ERR, "can not find entry for user %s", dropPrivilegesToUser);
2021-02-11 12:26:16 +01:00
exit(1);
2021-02-11 12:16:14 +01:00
}
if (setuid(userEntry->pw_uid) != 0) {
logmsg(LOG_ERR, "unable to drop root privileges to %d", userEntry->pw_uid);
2021-02-11 12:26:16 +01:00
exit(2);
2021-02-11 12:07:37 +01:00
}
}
2021-03-15 17:23:29 +01:00
logmsg(LOG_INFO, "Version: " VERSION);
2021-02-11 10:59:53 +01:00
if (0 != initConfig(configFilename, &configHandle)) {
2021-02-10 12:04:21 +01:00
logmsg(LOG_ERR, "error when reading configuration");
2021-02-11 12:26:16 +01:00
exit(3);
2021-02-10 12:04:21 +01:00
}
2021-02-09 18:32:58 +01:00
2021-02-11 12:26:16 +01:00
if (doFork) {
int pid = fork();
if (pid == -1) {
logmsg(LOG_ERR, "error when forking into background: %d", errno);
exit(4);
}
if (pid != 0) {
logmsg(LOG_INFO, "successfully forking into background, child's pid is %d", pid);
exit(0);
}
}
2021-02-10 17:23:59 +01:00
if (0 != initReceiver(&configHandle, &receiverHandle)) {
2021-02-10 12:04:21 +01:00
logmsg(LOG_ERR, "error when initializing receiver");
2021-02-11 12:26:16 +01:00
exit(5);
2021-02-10 12:04:21 +01:00
}
2021-02-10 17:23:59 +01:00
if (0 != initForwarder(&configHandle, &forwarderHandle)) {
2021-02-10 12:04:21 +01:00
logmsg(LOG_ERR, "error when initializing forwarder");
2021-02-11 12:26:16 +01:00
exit(6);
2021-02-10 12:04:21 +01:00
}
2021-02-10 11:12:02 +01:00
2021-02-10 15:26:56 +01:00
2021-02-10 11:12:02 +01:00
while (1) {
t_minuteBuffer buf;
2021-02-10 15:26:56 +01:00
if (receiveAndVerifyMinuteBuffer(&receiverHandle, &buf) < 0) {
2021-02-10 11:12:02 +01:00
logmsg(LOG_ERR, "error in receiveAndVerify");
2021-02-10 12:15:27 +01:00
continue;
}
if (forwardMinuteBuffer(&forwarderHandle, &buf) < 0) {
logmsg(LOG_ERR, "error in send");
2021-02-10 11:12:02 +01:00
}
2021-02-08 15:41:06 +01:00
}
2021-02-09 18:37:11 +01:00
2021-02-10 12:06:03 +01:00
deinitForwarder(&forwarderHandle);
2021-02-10 15:26:56 +01:00
deinitReceiver(&receiverHandle);
2021-02-10 17:23:59 +01:00
deinitConfig(&configHandle);
2021-02-08 16:00:36 +01:00
}