2022-07-28 15:15:04 +00:00
|
|
|
/*
|
2022-12-26 15:52:52 +00:00
|
|
|
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
2022-07-28 15:15:04 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person
|
|
|
|
* obtaining a copy of this software and associated documentation files
|
|
|
|
* (the "Software"), to deal in the Software without restriction,
|
|
|
|
* including without limitation the rights to use, copy, modify, merge,
|
|
|
|
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
|
|
* and to permit persons to whom the Software is furnished to do so,
|
|
|
|
* subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
2022-07-28 16:00:52 +00:00
|
|
|
* included in all copies or portions of the Software.
|
2022-07-28 15:15:04 +00:00
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
|
|
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
2022-07-23 00:19:12 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2022-12-15 20:18:12 +00:00
|
|
|
#include <ctype.h>
|
2022-07-23 00:19:12 +00:00
|
|
|
#include <unistd.h>
|
2022-08-11 01:13:05 +00:00
|
|
|
#include <errno.h>
|
2022-08-11 23:40:13 +00:00
|
|
|
#include <signal.h>
|
2022-07-23 00:19:12 +00:00
|
|
|
|
2022-08-10 15:58:39 +00:00
|
|
|
#include <grp.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
|
2023-01-17 20:29:16 +00:00
|
|
|
#include <Telodendria.h>
|
2022-10-13 13:08:05 +00:00
|
|
|
#include <Memory.h>
|
2022-08-11 23:40:13 +00:00
|
|
|
#include <TelodendriaConfig.h>
|
2022-07-23 00:19:12 +00:00
|
|
|
#include <Log.h>
|
|
|
|
#include <HashMap.h>
|
2022-12-09 23:57:30 +00:00
|
|
|
#include <Json.h>
|
2022-08-11 23:40:13 +00:00
|
|
|
#include <HttpServer.h>
|
2022-08-28 19:45:24 +00:00
|
|
|
#include <Matrix.h>
|
2022-10-24 18:36:48 +00:00
|
|
|
#include <Db.h>
|
2022-12-24 21:49:37 +00:00
|
|
|
#include <Cron.h>
|
2023-02-19 14:58:56 +00:00
|
|
|
#include <Uia.h>
|
2022-08-11 23:40:13 +00:00
|
|
|
|
2023-01-17 20:29:16 +00:00
|
|
|
const char
|
|
|
|
TelodendriaLogo[TELODENDRIA_LOGO_HEIGHT][TELODENDRIA_LOGO_WIDTH] = {
|
|
|
|
" .= -=- ",
|
|
|
|
" :.:+ .=:. ",
|
|
|
|
" .=+-==. :. ",
|
|
|
|
" .+- =. ",
|
|
|
|
" .+ :+. ",
|
|
|
|
" ==. -+: ",
|
|
|
|
" =++==--:: =+. ",
|
|
|
|
" .:::--=+=: :+= ",
|
|
|
|
" :==. -=: ",
|
|
|
|
" ===----=-. ... :+. ",
|
|
|
|
" :==+=======: .-+-::-+-=+=",
|
|
|
|
" .==*%#======= :+- .. ",
|
|
|
|
" .:--=-===+=========-. :+: ",
|
|
|
|
" .=++=::..:============-+=-=- ",
|
|
|
|
":+=: :=+-: .-=========-. . ",
|
|
|
|
" =+++: .:=+-: .:--. .--:==: ",
|
|
|
|
" ::---:.. :=+: == ",
|
|
|
|
" ++. .+- ",
|
|
|
|
" =+ .+- ...: ",
|
|
|
|
" +- -+-:-+=::+: ",
|
|
|
|
" :=-....:-=: .--: =- ",
|
|
|
|
" -++=:.:::.. "
|
|
|
|
};
|
|
|
|
|
|
|
|
const char
|
|
|
|
TelodendriaHeader[TELODENDRIA_HEADER_HEIGHT][TELODENDRIA_HEADER_WIDTH] = {
|
|
|
|
"=======================================================",
|
|
|
|
"|_ _|__| | ___ __| | ___ _ __ __| |_ __(_) __ _ ",
|
|
|
|
" | |/ _ \\ |/ _ \\ / _` |/ _ \\ '_ \\ / _` | '__| |/ _` | ",
|
|
|
|
" | | __/ | (_) | (_| | __/ | | | (_| | | | | (_| | ",
|
|
|
|
" |_|\\___|_|\\___/ \\__,_|\\___|_| |_|\\__,_|_| |_|\\__,_| ",
|
|
|
|
"======================================================="
|
|
|
|
};
|
|
|
|
|
2023-02-23 03:46:05 +00:00
|
|
|
static void
|
|
|
|
TelodendriaHexDump(size_t off, char *hexBuf, char *asciiBuf, void *args)
|
|
|
|
{
|
|
|
|
LogConfig *lc = args;
|
|
|
|
|
|
|
|
if (hexBuf && asciiBuf)
|
|
|
|
{
|
|
|
|
Log(lc, LOG_DEBUG, "%04x: %s | %s |", off, hexBuf, asciiBuf);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Log(lc, LOG_DEBUG, "%04x", off);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-13 16:18:42 +00:00
|
|
|
static void
|
2022-10-13 13:09:26 +00:00
|
|
|
TelodendriaMemoryHook(MemoryAction a, MemoryInfo * i, void *args)
|
2022-10-13 13:08:05 +00:00
|
|
|
{
|
2022-10-13 13:09:26 +00:00
|
|
|
LogConfig *lc = (LogConfig *) args;
|
|
|
|
char *action;
|
|
|
|
|
|
|
|
switch (a)
|
|
|
|
{
|
|
|
|
case MEMORY_ALLOCATE:
|
|
|
|
action = "Allocated";
|
|
|
|
break;
|
|
|
|
case MEMORY_REALLOCATE:
|
|
|
|
action = "Re-allocated";
|
|
|
|
break;
|
|
|
|
case MEMORY_FREE:
|
|
|
|
action = "Freed";
|
|
|
|
break;
|
2022-10-28 18:07:44 +00:00
|
|
|
case MEMORY_BAD_POINTER:
|
2022-10-31 23:52:37 +00:00
|
|
|
action = "Bad pointer to";
|
|
|
|
break;
|
2022-10-13 13:09:26 +00:00
|
|
|
default:
|
|
|
|
action = "Unknown operation on";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-12-28 15:52:19 +00:00
|
|
|
Log(lc, a == MEMORY_BAD_POINTER ? LOG_WARNING : LOG_DEBUG,
|
|
|
|
"%s:%d: %s %lu bytes of memory at %p.",
|
2022-10-13 13:09:26 +00:00
|
|
|
MemoryInfoGetFile(i), MemoryInfoGetLine(i),
|
2022-10-15 13:34:47 +00:00
|
|
|
action, MemoryInfoGetSize(i),
|
2022-10-13 13:09:26 +00:00
|
|
|
MemoryInfoGetPointer(i));
|
2022-10-13 13:08:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-10-13 13:09:26 +00:00
|
|
|
TelodendriaMemoryIterator(MemoryInfo * i, void *args)
|
2022-10-13 13:08:05 +00:00
|
|
|
{
|
2022-10-13 13:09:26 +00:00
|
|
|
LogConfig *lc = (LogConfig *) args;
|
|
|
|
|
|
|
|
/* We haven't freed the logger memory yet */
|
|
|
|
if (MemoryInfoGetPointer(i) != lc)
|
|
|
|
{
|
2022-10-28 18:07:44 +00:00
|
|
|
Log(lc, LOG_WARNING, "%s:%d: %lu bytes of memory at %p leaked.",
|
|
|
|
MemoryInfoGetFile(i), MemoryInfoGetLine(i),
|
|
|
|
MemoryInfoGetSize(i), MemoryInfoGetPointer(i));
|
2022-12-15 20:18:12 +00:00
|
|
|
|
2023-01-01 22:10:23 +00:00
|
|
|
MemoryHexDump(i, TelodendriaHexDump, lc);
|
2022-10-13 13:09:26 +00:00
|
|
|
}
|
2022-10-13 13:08:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static HttpServer *httpServer = NULL;
|
2022-08-11 23:40:13 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
TelodendriaSignalHandler(int signalNo)
|
|
|
|
{
|
|
|
|
(void) signalNo;
|
|
|
|
HttpServerStop(httpServer);
|
|
|
|
}
|
2022-07-23 00:19:12 +00:00
|
|
|
|
|
|
|
typedef enum ArgFlag
|
|
|
|
{
|
|
|
|
ARG_VERSION = (1 << 0),
|
2022-09-24 23:41:39 +00:00
|
|
|
ARG_CONFIGTEST = (1 << 1),
|
|
|
|
ARG_VERBOSE = (1 << 2)
|
2022-07-23 00:19:12 +00:00
|
|
|
} ArgFlag;
|
|
|
|
|
|
|
|
static void
|
2022-07-25 19:18:35 +00:00
|
|
|
TelodendriaPrintHeader(LogConfig * lc)
|
2022-07-23 00:19:12 +00:00
|
|
|
{
|
2023-01-17 20:29:16 +00:00
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < TELODENDRIA_LOGO_HEIGHT; i++)
|
|
|
|
{
|
|
|
|
Log(lc, LOG_INFO, "%s", TelodendriaLogo[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < TELODENDRIA_HEADER_HEIGHT; i++)
|
|
|
|
{
|
|
|
|
Log(lc, LOG_INFO, "%s", TelodendriaHeader[i]);
|
|
|
|
}
|
|
|
|
|
2022-11-24 18:51:07 +00:00
|
|
|
Log(lc, LOG_INFO, "Telodendria v" TELODENDRIA_VERSION);
|
|
|
|
Log(lc, LOG_INFO, "");
|
|
|
|
Log(lc, LOG_INFO,
|
2023-01-07 04:40:12 +00:00
|
|
|
"Copyright (C) 2023 Jordan Bancino <@jordan:bancino.net>");
|
2022-11-24 18:51:07 +00:00
|
|
|
Log(lc, LOG_INFO,
|
2022-07-23 04:50:12 +00:00
|
|
|
"Documentation/Support: https://telodendria.io");
|
2022-11-24 18:51:07 +00:00
|
|
|
Log(lc, LOG_INFO, "");
|
2022-07-23 00:19:12 +00:00
|
|
|
}
|
|
|
|
|
2022-07-25 19:25:06 +00:00
|
|
|
int
|
2022-07-25 19:18:35 +00:00
|
|
|
main(int argc, char **argv)
|
2022-07-23 00:19:12 +00:00
|
|
|
{
|
|
|
|
LogConfig *lc;
|
|
|
|
int exit = EXIT_SUCCESS;
|
|
|
|
|
|
|
|
/* Arg parsing */
|
|
|
|
int opt;
|
|
|
|
int flags = 0;
|
2022-11-23 14:31:05 +00:00
|
|
|
char *configArg = NULL;
|
2022-07-23 00:19:12 +00:00
|
|
|
|
|
|
|
/* Config file */
|
|
|
|
FILE *configFile = NULL;
|
|
|
|
HashMap *config = NULL;
|
|
|
|
|
2022-08-10 00:05:41 +00:00
|
|
|
/* Program configuration */
|
|
|
|
TelodendriaConfig *tConfig = NULL;
|
2022-07-23 00:19:12 +00:00
|
|
|
|
2022-08-10 15:58:39 +00:00
|
|
|
/* User validation */
|
2022-11-17 19:33:59 +00:00
|
|
|
struct passwd *userInfo = NULL;
|
|
|
|
struct group *groupInfo = NULL;
|
2022-08-10 15:58:39 +00:00
|
|
|
|
2022-08-11 23:40:13 +00:00
|
|
|
/* Signal handling */
|
|
|
|
struct sigaction sigAction;
|
|
|
|
|
2022-09-29 17:41:26 +00:00
|
|
|
MatrixHttpHandlerArgs matrixArgs;
|
2022-12-24 21:49:37 +00:00
|
|
|
Cron *cron = NULL;
|
2022-08-28 19:45:24 +00:00
|
|
|
|
2022-10-28 18:07:44 +00:00
|
|
|
memset(&matrixArgs, 0, sizeof(matrixArgs));
|
|
|
|
|
2022-08-10 00:05:41 +00:00
|
|
|
lc = LogConfigCreate();
|
2022-07-23 00:19:12 +00:00
|
|
|
|
|
|
|
if (!lc)
|
|
|
|
{
|
|
|
|
printf("Fatal error: unable to allocate memory for logger.\n");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2022-10-13 13:09:26 +00:00
|
|
|
MemoryHook(TelodendriaMemoryHook, lc);
|
2022-10-13 13:08:05 +00:00
|
|
|
|
2022-07-23 00:19:12 +00:00
|
|
|
TelodendriaPrintHeader(lc);
|
|
|
|
|
2022-08-11 01:13:05 +00:00
|
|
|
#ifdef __OpenBSD__
|
2022-08-11 15:50:35 +00:00
|
|
|
Log(lc, LOG_DEBUG, "Attempting pledge...");
|
2022-08-11 01:13:05 +00:00
|
|
|
|
2022-11-21 16:13:11 +00:00
|
|
|
if (pledge("stdio rpath wpath cpath flock inet dns getpw id unveil", NULL) != 0)
|
2022-08-11 01:13:05 +00:00
|
|
|
{
|
2022-11-24 18:51:07 +00:00
|
|
|
Log(lc, LOG_ERR, "Pledge failed: %s", strerror(errno));
|
2022-08-11 01:13:05 +00:00
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-09-24 23:41:39 +00:00
|
|
|
while ((opt = getopt(argc, argv, "f:Vvn")) != -1)
|
2022-07-23 00:19:12 +00:00
|
|
|
{
|
|
|
|
switch (opt)
|
|
|
|
{
|
2022-09-24 23:41:39 +00:00
|
|
|
case 'f':
|
2022-07-23 00:19:12 +00:00
|
|
|
configArg = optarg;
|
|
|
|
break;
|
|
|
|
case 'V':
|
|
|
|
flags |= ARG_VERSION;
|
|
|
|
break;
|
2022-09-24 23:41:39 +00:00
|
|
|
case 'v':
|
|
|
|
flags |= ARG_VERBOSE;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
flags |= ARG_CONFIGTEST;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
2022-07-23 00:19:12 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-17 21:38:39 +00:00
|
|
|
if (flags & ARG_VERBOSE)
|
|
|
|
{
|
|
|
|
LogConfigLevelSet(lc, LOG_DEBUG);
|
|
|
|
}
|
|
|
|
|
2022-07-23 00:19:12 +00:00
|
|
|
if (flags & ARG_VERSION)
|
|
|
|
{
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2022-11-23 14:56:31 +00:00
|
|
|
if (!configArg)
|
|
|
|
{
|
2022-11-24 18:51:07 +00:00
|
|
|
Log(lc, LOG_ERR, "No configuration file specified.");
|
2022-11-23 14:56:31 +00:00
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
2022-11-23 14:31:05 +00:00
|
|
|
else if (strcmp(configArg, "-") == 0)
|
2022-07-23 00:19:12 +00:00
|
|
|
{
|
2022-08-11 15:50:35 +00:00
|
|
|
configFile = stdin;
|
2022-07-23 00:19:12 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-11-02 00:35:05 +00:00
|
|
|
fclose(stdin);
|
2022-08-11 01:13:05 +00:00
|
|
|
#ifdef __OpenBSD__
|
|
|
|
if (unveil(configArg, "r") != 0)
|
|
|
|
{
|
2022-11-24 18:51:07 +00:00
|
|
|
Log(lc, LOG_ERR, "Unable to unveil() configuration file '%s' for reading.", configArg);
|
2022-08-11 01:13:05 +00:00
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
#endif
|
2022-07-23 00:19:12 +00:00
|
|
|
configFile = fopen(configArg, "r");
|
|
|
|
if (!configFile)
|
|
|
|
{
|
2022-11-24 18:51:07 +00:00
|
|
|
Log(lc, LOG_ERR, "Unable to open configuration file '%s' for reading.", configArg);
|
2022-07-23 00:19:12 +00:00
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-24 18:51:07 +00:00
|
|
|
Log(lc, LOG_NOTICE, "Processing configuration file '%s'.", configArg);
|
2022-07-26 01:31:09 +00:00
|
|
|
|
2022-12-09 23:57:30 +00:00
|
|
|
config = JsonDecode(configFile);
|
|
|
|
fclose(configFile);
|
|
|
|
|
|
|
|
if (!config)
|
2022-07-23 00:19:12 +00:00
|
|
|
{
|
2022-12-09 23:57:30 +00:00
|
|
|
Log(lc, LOG_ERR, "Syntax error in configuration file.");
|
2022-07-23 00:19:12 +00:00
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2022-08-10 00:05:41 +00:00
|
|
|
tConfig = TelodendriaConfigParse(config, lc);
|
2022-12-09 23:57:30 +00:00
|
|
|
JsonFree(config);
|
|
|
|
|
2022-08-10 00:05:41 +00:00
|
|
|
if (!tConfig)
|
2022-07-23 00:19:12 +00:00
|
|
|
{
|
2022-08-10 00:05:41 +00:00
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
2022-07-23 00:19:12 +00:00
|
|
|
}
|
2022-08-10 00:05:41 +00:00
|
|
|
|
2022-09-24 23:41:39 +00:00
|
|
|
if (flags & ARG_CONFIGTEST)
|
|
|
|
{
|
2022-11-24 18:51:07 +00:00
|
|
|
Log(lc, LOG_INFO, "Configuration is OK.");
|
2022-09-24 23:41:39 +00:00
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2022-08-11 01:13:05 +00:00
|
|
|
#ifdef __OpenBSD__
|
2022-10-11 15:38:34 +00:00
|
|
|
if (unveil(tConfig->dataDir, "rwc") != 0)
|
2022-08-11 01:13:05 +00:00
|
|
|
{
|
2022-11-24 18:51:07 +00:00
|
|
|
Log(lc, LOG_ERR, "Unveil of data directory failed: %s", strerror(errno));
|
2022-08-11 01:13:05 +00:00
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
unveil(NULL, NULL); /* Done with unveil(), so disable it */
|
|
|
|
#endif
|
|
|
|
|
2022-12-09 23:57:30 +00:00
|
|
|
if (!tConfig->logTimestamp || strcmp(tConfig->logTimestamp, "default") != 0)
|
|
|
|
{
|
|
|
|
LogConfigTimeStampFormatSet(lc, tConfig->logTimestamp);
|
|
|
|
}
|
2023-01-16 03:57:01 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
Free(tConfig->logTimestamp);
|
|
|
|
tConfig->logTimestamp = NULL;
|
|
|
|
}
|
2022-08-11 01:13:05 +00:00
|
|
|
|
2022-10-11 16:00:36 +00:00
|
|
|
if (tConfig->flags & TELODENDRIA_LOG_COLOR)
|
2022-08-11 01:13:05 +00:00
|
|
|
{
|
2022-10-13 13:09:26 +00:00
|
|
|
LogConfigFlagSet(lc, LOG_FLAG_COLOR);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-08-11 01:13:05 +00:00
|
|
|
LogConfigFlagClear(lc, LOG_FLAG_COLOR);
|
|
|
|
}
|
|
|
|
|
2022-09-24 23:41:39 +00:00
|
|
|
LogConfigLevelSet(lc, flags & ARG_VERBOSE ? LOG_DEBUG : tConfig->logLevel);
|
2022-08-11 01:13:05 +00:00
|
|
|
|
2022-10-11 16:00:36 +00:00
|
|
|
if (chdir(tConfig->dataDir) != 0)
|
|
|
|
{
|
2022-11-24 18:51:07 +00:00
|
|
|
Log(lc, LOG_ERR, "Unable to change into data directory: %s.", strerror(errno));
|
2022-10-11 16:00:36 +00:00
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Log(lc, LOG_DEBUG, "Changed working directory to: %s", tConfig->dataDir);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (tConfig->flags & TELODENDRIA_LOG_FILE)
|
2022-08-11 01:13:05 +00:00
|
|
|
{
|
2022-10-11 16:00:36 +00:00
|
|
|
FILE *logFile = fopen("telodendria.log", "a");
|
2022-08-11 01:13:05 +00:00
|
|
|
|
|
|
|
if (!logFile)
|
|
|
|
{
|
2022-11-24 18:51:07 +00:00
|
|
|
Log(lc, LOG_ERR, "Unable to open log file for appending.");
|
2022-08-11 01:13:05 +00:00
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2022-11-24 18:51:07 +00:00
|
|
|
Log(lc, LOG_INFO, "Logging to the log file. Check there for all future messages.");
|
2022-08-11 01:13:05 +00:00
|
|
|
LogConfigOutputSet(lc, logFile);
|
|
|
|
}
|
2022-10-13 13:09:26 +00:00
|
|
|
else if (tConfig->flags & TELODENDRIA_LOG_STDOUT)
|
|
|
|
{
|
|
|
|
Log(lc, LOG_DEBUG, "Already logging to standard output.");
|
|
|
|
}
|
|
|
|
else if (tConfig->flags & TELODENDRIA_LOG_SYSLOG)
|
|
|
|
{
|
2022-11-24 18:51:07 +00:00
|
|
|
Log(lc, LOG_INFO, "Logging to the syslog. Check there for all future messages.");
|
2022-10-15 00:55:15 +00:00
|
|
|
LogConfigFlagSet(lc, LOG_FLAG_SYSLOG);
|
|
|
|
|
|
|
|
openlog("telodendria", LOG_PID | LOG_NDELAY, LOG_DAEMON);
|
|
|
|
/* Always log everything, because the Log API will control what
|
|
|
|
* messages get passed to the syslog */
|
|
|
|
setlogmask(LOG_UPTO(LOG_DEBUG));
|
2022-10-13 13:09:26 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-11-24 18:51:07 +00:00
|
|
|
Log(lc, LOG_ERR, "Unknown logging method in flags: '%d'", tConfig->flags);
|
|
|
|
Log(lc, LOG_ERR, "This is a programmer error; please report it.");
|
2022-10-13 13:09:26 +00:00
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
2022-08-11 01:13:05 +00:00
|
|
|
|
2022-08-10 00:05:41 +00:00
|
|
|
Log(lc, LOG_DEBUG, "Configuration:");
|
|
|
|
LogConfigIndent(lc);
|
2022-08-11 23:40:13 +00:00
|
|
|
Log(lc, LOG_DEBUG, "Listen On: %d", tConfig->listenPort);
|
2022-08-10 00:05:41 +00:00
|
|
|
Log(lc, LOG_DEBUG, "Server Name: %s", tConfig->serverName);
|
2022-09-29 17:41:26 +00:00
|
|
|
Log(lc, LOG_DEBUG, "Base URL: %s", tConfig->baseUrl);
|
|
|
|
Log(lc, LOG_DEBUG, "Identity Server: %s", tConfig->identityServer);
|
2022-08-10 00:05:41 +00:00
|
|
|
Log(lc, LOG_DEBUG, "Run As: %s:%s", tConfig->uid, tConfig->gid);
|
|
|
|
Log(lc, LOG_DEBUG, "Data Directory: %s", tConfig->dataDir);
|
|
|
|
Log(lc, LOG_DEBUG, "Threads: %d", tConfig->threads);
|
2022-10-24 17:40:21 +00:00
|
|
|
Log(lc, LOG_DEBUG, "Max Connections: %d", tConfig->maxConnections);
|
|
|
|
Log(lc, LOG_DEBUG, "Max Cache: %ld", tConfig->maxCache);
|
2022-08-10 00:05:41 +00:00
|
|
|
Log(lc, LOG_DEBUG, "Flags: %x", tConfig->flags);
|
|
|
|
LogConfigUnindent(lc);
|
|
|
|
|
2022-09-29 17:41:26 +00:00
|
|
|
/* Arguments to pass into the HTTP handler */
|
|
|
|
matrixArgs.lc = lc;
|
|
|
|
matrixArgs.config = tConfig;
|
2022-08-28 19:45:24 +00:00
|
|
|
|
2022-08-11 23:40:13 +00:00
|
|
|
/* Bind the socket before possibly dropping permissions */
|
2022-08-21 16:35:16 +00:00
|
|
|
httpServer = HttpServerCreate(tConfig->listenPort, tConfig->threads, tConfig->maxConnections,
|
2022-09-29 17:41:26 +00:00
|
|
|
MatrixHttpHandler, &matrixArgs);
|
2022-08-12 00:13:10 +00:00
|
|
|
if (!httpServer)
|
2022-08-11 23:40:13 +00:00
|
|
|
{
|
2022-11-24 18:51:07 +00:00
|
|
|
Log(lc, LOG_ERR, "Unable to create HTTP server on port %d: %s",
|
2022-08-12 00:13:10 +00:00
|
|
|
tConfig->listenPort, strerror(errno));
|
2022-08-11 23:40:13 +00:00
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2022-11-08 17:53:01 +00:00
|
|
|
Log(lc, LOG_DEBUG, "Running as uid:gid: %d:%d.", getuid(), getgid());
|
|
|
|
|
|
|
|
if (tConfig->uid && tConfig->gid)
|
|
|
|
{
|
|
|
|
userInfo = getpwnam(tConfig->uid);
|
|
|
|
groupInfo = getgrnam(tConfig->gid);
|
|
|
|
|
|
|
|
if (!userInfo || !groupInfo)
|
|
|
|
{
|
2022-11-24 18:51:07 +00:00
|
|
|
Log(lc, LOG_ERR, "Unable to locate the user/group specified in the configuration.");
|
2022-11-08 17:53:01 +00:00
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Log(lc, LOG_DEBUG, "Found user/group information using getpwnam() and getgrnam().");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Log(lc, LOG_DEBUG, "No user/group info specified in the config.");
|
|
|
|
}
|
|
|
|
|
2022-08-10 15:58:39 +00:00
|
|
|
if (getuid() == 0)
|
|
|
|
{
|
2022-11-21 16:13:11 +00:00
|
|
|
#ifndef __OpenBSD__ /* chroot() is only useful without
|
|
|
|
* unveil() */
|
2022-08-24 20:10:34 +00:00
|
|
|
if (chroot(".") == 0)
|
2022-08-10 15:58:39 +00:00
|
|
|
{
|
2022-10-11 15:38:34 +00:00
|
|
|
Log(lc, LOG_DEBUG, "Changed the root directory to: %s.", tConfig->dataDir);
|
2022-08-10 15:58:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-10-11 15:38:34 +00:00
|
|
|
Log(lc, LOG_WARNING, "Unable to chroot into directory: %s.", tConfig->dataDir);
|
2022-08-10 15:58:39 +00:00
|
|
|
}
|
2022-11-20 13:19:56 +00:00
|
|
|
#endif
|
2022-08-10 15:58:39 +00:00
|
|
|
|
2022-11-17 19:33:59 +00:00
|
|
|
if (userInfo && groupInfo)
|
2022-08-10 15:58:39 +00:00
|
|
|
{
|
2022-11-08 17:53:01 +00:00
|
|
|
if (setgid(groupInfo->gr_gid) != 0 || setuid(userInfo->pw_uid) != 0)
|
|
|
|
{
|
2022-11-24 18:51:07 +00:00
|
|
|
Log(lc, LOG_ERR, "Unable to set process uid/gid.");
|
2022-11-08 17:53:01 +00:00
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Log(lc, LOG_DEBUG, "Set uid/gid to %s:%s.", tConfig->uid, tConfig->gid);
|
|
|
|
}
|
2022-08-10 15:58:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-11-08 17:53:01 +00:00
|
|
|
Log(lc, LOG_WARNING, "We are running as root, and we are not dropping to another user");
|
|
|
|
Log(lc, LOG_WARNING, "because none was specified in the configuration file.");
|
|
|
|
Log(lc, LOG_WARNING, "This is probably a security issue.");
|
2022-08-10 15:58:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-11-08 17:53:01 +00:00
|
|
|
Log(lc, LOG_WARNING, "Not setting root directory, because we are not root.");
|
2022-08-10 15:58:39 +00:00
|
|
|
|
2022-11-08 17:53:01 +00:00
|
|
|
if (tConfig->uid && tConfig->gid)
|
2022-08-10 15:58:39 +00:00
|
|
|
{
|
2022-11-08 17:53:01 +00:00
|
|
|
if (getuid() != userInfo->pw_uid || getgid() != groupInfo->gr_gid)
|
|
|
|
{
|
|
|
|
Log(lc, LOG_WARNING, "Not running as the uid/gid specified in the configuration.");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Log(lc, LOG_DEBUG, "Running as the uid/gid specified in the configuration.");
|
|
|
|
}
|
2022-08-10 15:58:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* These config values are no longer needed; don't hold them in
|
|
|
|
* memory anymore */
|
2022-10-13 16:27:24 +00:00
|
|
|
Free(tConfig->dataDir);
|
|
|
|
Free(tConfig->uid);
|
|
|
|
Free(tConfig->gid);
|
2022-08-10 15:58:39 +00:00
|
|
|
|
2022-10-11 15:38:34 +00:00
|
|
|
tConfig->dataDir = NULL;
|
2022-08-10 15:58:39 +00:00
|
|
|
tConfig->uid = NULL;
|
|
|
|
tConfig->gid = NULL;
|
|
|
|
|
2022-10-24 18:36:48 +00:00
|
|
|
|
|
|
|
if (!tConfig->maxCache)
|
|
|
|
{
|
2023-03-03 14:26:10 +00:00
|
|
|
Log(lc, LOG_WARNING, "Database caching is disabled.");
|
2022-11-21 16:22:50 +00:00
|
|
|
Log(lc, LOG_WARNING,
|
|
|
|
"If this is not what you intended, check the config file");
|
|
|
|
Log(lc, LOG_WARNING,
|
2023-03-03 14:26:10 +00:00
|
|
|
"and ensure that maxCache is a valid number of bytes.");
|
2022-10-24 18:36:48 +00:00
|
|
|
}
|
|
|
|
|
2022-11-21 16:39:38 +00:00
|
|
|
matrixArgs.db = DbOpen(".", tConfig->maxCache);
|
|
|
|
|
2022-10-24 18:36:48 +00:00
|
|
|
if (!matrixArgs.db)
|
|
|
|
{
|
2022-11-24 18:51:07 +00:00
|
|
|
Log(lc, LOG_ERR, "Unable to open data directory as a database.");
|
2022-10-24 18:36:48 +00:00
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2022-12-24 21:49:37 +00:00
|
|
|
cron = CronCreate(60 * 1000); /* 1-minute tick */
|
|
|
|
if (!cron)
|
|
|
|
{
|
|
|
|
Log(lc, LOG_ERR, "Unable to set up job scheduler.");
|
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
Log(lc, LOG_DEBUG, "Registering jobs...");
|
|
|
|
|
2023-02-19 14:58:56 +00:00
|
|
|
CronEvery(cron, 30 * 60 * 1000, (JobFunc *) UiaCleanup, &matrixArgs);
|
2022-12-24 21:49:37 +00:00
|
|
|
|
|
|
|
Log(lc, LOG_NOTICE, "Starting job scheduler...");
|
|
|
|
CronStart(cron);
|
|
|
|
|
2022-11-24 18:51:07 +00:00
|
|
|
Log(lc, LOG_NOTICE, "Starting server...");
|
2022-08-11 23:40:13 +00:00
|
|
|
|
|
|
|
if (!HttpServerStart(httpServer))
|
|
|
|
{
|
2022-11-24 18:51:07 +00:00
|
|
|
Log(lc, LOG_ERR, "Unable to start HTTP server.");
|
2022-08-11 23:40:13 +00:00
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2022-11-24 18:51:07 +00:00
|
|
|
Log(lc, LOG_INFO, "Listening on port: %d", tConfig->listenPort);
|
2022-08-11 23:40:13 +00:00
|
|
|
|
|
|
|
sigAction.sa_handler = TelodendriaSignalHandler;
|
|
|
|
sigfillset(&sigAction.sa_mask);
|
|
|
|
sigAction.sa_flags = SA_RESTART;
|
|
|
|
|
|
|
|
if (sigaction(SIGINT, &sigAction, NULL) < 0)
|
|
|
|
{
|
2022-11-24 18:51:07 +00:00
|
|
|
Log(lc, LOG_ERR, "Unable to install signal handler.");
|
2022-08-11 23:40:13 +00:00
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Block this thread until the server is terminated by a signal
|
|
|
|
* handler */
|
|
|
|
HttpServerJoin(httpServer);
|
|
|
|
|
2022-08-10 00:05:41 +00:00
|
|
|
finish:
|
2022-11-24 18:51:07 +00:00
|
|
|
Log(lc, LOG_NOTICE, "Shutting down...");
|
2022-08-11 23:40:13 +00:00
|
|
|
if (httpServer)
|
|
|
|
{
|
|
|
|
HttpServerFree(httpServer);
|
2022-10-13 13:09:26 +00:00
|
|
|
Log(lc, LOG_DEBUG, "Freed HTTP Server.");
|
2022-08-11 23:40:13 +00:00
|
|
|
}
|
2022-11-01 13:04:15 +00:00
|
|
|
|
2022-12-24 21:49:37 +00:00
|
|
|
if (cron)
|
|
|
|
{
|
|
|
|
CronStop(cron);
|
|
|
|
CronFree(cron);
|
|
|
|
Log(lc, LOG_DEBUG, "Stopped and freed job scheduler.");
|
|
|
|
}
|
|
|
|
|
2022-11-01 13:04:15 +00:00
|
|
|
/*
|
|
|
|
* If we're not logging to standard output, then we can close it. Otherwise,
|
|
|
|
* if we are logging to stdout, LogConfigFree() will close it for us.
|
|
|
|
*/
|
2022-11-01 13:47:25 +00:00
|
|
|
if (!tConfig || !(tConfig->flags & TELODENDRIA_LOG_STDOUT))
|
2022-11-01 13:04:15 +00:00
|
|
|
{
|
|
|
|
fclose(stdout);
|
|
|
|
}
|
|
|
|
|
2022-10-24 18:36:48 +00:00
|
|
|
DbClose(matrixArgs.db);
|
2022-10-13 13:08:05 +00:00
|
|
|
|
2023-01-16 03:57:01 +00:00
|
|
|
LogConfigTimeStampFormatSet(lc, NULL);
|
|
|
|
TelodendriaConfigFree(tConfig);
|
|
|
|
|
2022-10-13 13:09:26 +00:00
|
|
|
Log(lc, LOG_DEBUG, "");
|
|
|
|
MemoryIterate(TelodendriaMemoryIterator, lc);
|
|
|
|
Log(lc, LOG_DEBUG, "");
|
2022-10-13 13:08:05 +00:00
|
|
|
|
|
|
|
Log(lc, LOG_DEBUG, "Exiting with code '%d'.", exit);
|
2022-08-11 01:13:05 +00:00
|
|
|
LogConfigFree(lc);
|
2022-10-13 16:27:24 +00:00
|
|
|
|
|
|
|
MemoryFreeAll();
|
2022-11-01 13:04:15 +00:00
|
|
|
|
|
|
|
fclose(stderr);
|
2022-07-23 00:19:12 +00:00
|
|
|
return exit;
|
|
|
|
}
|