2023-03-09 02:46:04 +00:00
|
|
|
/*
|
2024-01-05 23:57:19 +00:00
|
|
|
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net> with
|
|
|
|
* other valuable contributors. See CONTRIBUTORS.txt for the full list.
|
2023-03-09 02:46: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
|
|
|
|
* included in all copies or portions of the Software.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <signal.h>
|
2023-04-19 00:33:38 +00:00
|
|
|
#include <limits.h>
|
2023-03-09 02:46:04 +00:00
|
|
|
|
|
|
|
#include <grp.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
|
Use `Makefile`s instead of a custom script (#38)
This pull request also requires the use of the external [Cytoplasm](/Telodendria/Cytoplasm) repository by removing the in-tree copy of Cytoplasm. The increased modularity requires a little more complex build process, but is overall better. Closes #19
The appropriate documentation has been updated. Closes #18
---
Please review the developer certificate of origin:
1. The contribution was created in whole or in part by me, and I have
the right to submit it under the open source licenses of the
Telodendria project; or
1. The contribution is based upon a previous work that, to the best of
my knowledge, is covered under an appropriate open source license and
I have the right under that license to submit that work with
modifications, whether created in whole or in part by me, under the
Telodendria project license; or
1. The contribution was provided directly to me by some other person
who certified (1), (2), or (3), and I have not modified it.
1. I understand and agree that this project and the contribution are
made public and that a record of the contribution—including all
personal information I submit with it—is maintained indefinitely
and may be redistributed consistent with this project or the open
source licenses involved.
- [x] I have read the Telodendria Project development certificate of
origin, and I certify that I have permission to submit this patch
under the conditions specified in it.
Reviewed-on: https://git.telodendria.io/Telodendria/Telodendria/pulls/38
2023-11-01 16:27:45 +00:00
|
|
|
#include <Cytoplasm/Args.h>
|
|
|
|
#include <Cytoplasm/Memory.h>
|
|
|
|
#include <Cytoplasm/Log.h>
|
|
|
|
#include <Cytoplasm/HashMap.h>
|
|
|
|
#include <Cytoplasm/Json.h>
|
|
|
|
#include <Cytoplasm/HttpServer.h>
|
|
|
|
#include <Cytoplasm/Db.h>
|
|
|
|
#include <Cytoplasm/Cron.h>
|
|
|
|
#include <Cytoplasm/Util.h>
|
|
|
|
#include <Cytoplasm/Str.h>
|
2023-04-19 00:33:38 +00:00
|
|
|
|
2023-05-14 22:15:48 +00:00
|
|
|
#include <Telodendria.h>
|
2023-04-19 00:33:38 +00:00
|
|
|
#include <Matrix.h>
|
|
|
|
#include <User.h>
|
|
|
|
#include <RegToken.h>
|
|
|
|
#include <Routes.h>
|
2024-01-14 01:02:07 +00:00
|
|
|
#include <Uia.h>
|
|
|
|
#include <Config.h>
|
|
|
|
|
2023-04-19 00:33:38 +00:00
|
|
|
|
|
|
|
static Array *httpServers;
|
|
|
|
static volatile int restart;
|
2023-03-09 02:46:04 +00:00
|
|
|
|
|
|
|
static void
|
2023-04-19 00:33:38 +00:00
|
|
|
SignalHandler(int signal)
|
2023-03-09 02:46:04 +00:00
|
|
|
{
|
2023-03-22 17:00:48 +00:00
|
|
|
size_t i;
|
|
|
|
|
2023-03-23 16:39:15 +00:00
|
|
|
switch (signal)
|
2023-03-22 17:00:48 +00:00
|
|
|
{
|
2023-03-23 16:39:15 +00:00
|
|
|
case SIGPIPE:
|
|
|
|
return;
|
2023-04-19 00:33:38 +00:00
|
|
|
case SIGUSR1:
|
|
|
|
restart = 1;
|
|
|
|
/* Fall through */
|
2023-04-24 16:23:50 +00:00
|
|
|
case SIGTERM:
|
2023-03-23 16:39:15 +00:00
|
|
|
case SIGINT:
|
|
|
|
if (!httpServers)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2023-03-23 02:12:45 +00:00
|
|
|
|
2023-03-23 16:39:15 +00:00
|
|
|
for (i = 0; i < ArraySize(httpServers); i++)
|
|
|
|
{
|
|
|
|
HttpServer *server = ArrayGet(httpServers, i);
|
2023-03-24 02:41:01 +00:00
|
|
|
|
2023-03-23 16:39:15 +00:00
|
|
|
HttpServerStop(server);
|
|
|
|
}
|
|
|
|
break;
|
2023-03-22 17:00:48 +00:00
|
|
|
}
|
2023-03-09 02:46:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef enum ArgFlag
|
|
|
|
{
|
|
|
|
ARG_VERSION = (1 << 0),
|
|
|
|
ARG_VERBOSE = (1 << 2)
|
|
|
|
} ArgFlag;
|
|
|
|
|
|
|
|
int
|
2023-05-15 22:38:52 +00:00
|
|
|
Main(Array * args)
|
2023-03-09 02:46:04 +00:00
|
|
|
{
|
2023-04-19 00:33:38 +00:00
|
|
|
int exit;
|
2023-03-09 02:46:04 +00:00
|
|
|
|
|
|
|
/* Arg parsing */
|
2023-05-14 22:15:48 +00:00
|
|
|
ArgParseState arg;
|
2023-03-09 02:46:04 +00:00
|
|
|
int opt;
|
2023-04-19 00:33:38 +00:00
|
|
|
int flags;
|
|
|
|
char *dbPath;
|
2023-03-09 02:46:04 +00:00
|
|
|
|
|
|
|
/* Program configuration */
|
2024-01-06 02:00:27 +00:00
|
|
|
Config tConfig;
|
2023-04-19 00:33:38 +00:00
|
|
|
Stream *logFile;
|
2024-01-06 02:02:45 +00:00
|
|
|
Stream *pidFile;
|
2024-01-06 02:00:27 +00:00
|
|
|
|
2024-01-06 02:02:45 +00:00
|
|
|
char *pidPath;
|
2023-03-09 02:46:04 +00:00
|
|
|
|
|
|
|
/* User validation */
|
2023-04-19 00:33:38 +00:00
|
|
|
struct passwd *userInfo;
|
|
|
|
struct group *groupInfo;
|
2023-03-09 02:46:04 +00:00
|
|
|
|
2023-03-22 17:00:48 +00:00
|
|
|
/* HTTP server management */
|
|
|
|
size_t i;
|
|
|
|
HttpServer *server;
|
|
|
|
|
2023-03-09 02:46:04 +00:00
|
|
|
/* Signal handling */
|
|
|
|
struct sigaction sigAction;
|
|
|
|
|
|
|
|
MatrixHttpHandlerArgs matrixArgs;
|
2023-04-19 00:33:38 +00:00
|
|
|
Cron *cron;
|
|
|
|
|
|
|
|
char startDir[PATH_MAX];
|
|
|
|
|
2023-08-05 14:12:02 +00:00
|
|
|
char *token;
|
|
|
|
|
2023-04-19 00:33:38 +00:00
|
|
|
start:
|
|
|
|
/* Global variables */
|
|
|
|
httpServers = NULL;
|
|
|
|
restart = 0;
|
|
|
|
|
|
|
|
/* Local variables */
|
|
|
|
exit = EXIT_SUCCESS;
|
|
|
|
flags = 0;
|
|
|
|
dbPath = NULL;
|
|
|
|
logFile = NULL;
|
2024-01-06 02:02:45 +00:00
|
|
|
pidFile = NULL;
|
|
|
|
pidPath = NULL;
|
2023-04-19 00:33:38 +00:00
|
|
|
userInfo = NULL;
|
|
|
|
groupInfo = NULL;
|
|
|
|
cron = NULL;
|
|
|
|
|
2023-08-05 14:12:02 +00:00
|
|
|
token = NULL;
|
|
|
|
|
2023-03-09 02:46:04 +00:00
|
|
|
memset(&matrixArgs, 0, sizeof(matrixArgs));
|
|
|
|
|
2023-03-22 14:52:04 +00:00
|
|
|
if (!LogConfigGlobal())
|
2023-03-09 02:46:04 +00:00
|
|
|
{
|
|
|
|
printf("Fatal error: unable to allocate memory for logger.\n");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2023-03-22 14:52:04 +00:00
|
|
|
TelodendriaPrintHeader();
|
2023-03-09 02:46:04 +00:00
|
|
|
|
2023-05-14 22:15:48 +00:00
|
|
|
ArgParseStateInit(&arg);
|
|
|
|
while ((opt = ArgParse(&arg, args, "d:Vv")) != -1)
|
2023-03-09 02:46:04 +00:00
|
|
|
{
|
|
|
|
switch (opt)
|
|
|
|
{
|
2023-04-19 00:33:38 +00:00
|
|
|
case 'd':
|
2023-05-14 22:15:48 +00:00
|
|
|
dbPath = arg.optArg;
|
2023-03-09 02:46:04 +00:00
|
|
|
break;
|
|
|
|
case 'V':
|
|
|
|
flags |= ARG_VERSION;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
flags |= ARG_VERBOSE;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & ARG_VERBOSE)
|
|
|
|
{
|
2023-03-22 14:52:04 +00:00
|
|
|
LogConfigLevelSet(LogConfigGlobal(), LOG_DEBUG);
|
2023-05-21 13:24:00 +00:00
|
|
|
MemoryHook(TelodendriaMemoryHook, (void *) ARG_VERBOSE);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-22 18:29:05 +00:00
|
|
|
MemoryHook(TelodendriaMemoryHook, NULL);
|
2023-03-09 02:46:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & ARG_VERSION)
|
|
|
|
{
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2023-04-19 00:33:38 +00:00
|
|
|
if (!dbPath)
|
2023-03-09 02:46:04 +00:00
|
|
|
{
|
2023-04-19 00:33:38 +00:00
|
|
|
Log(LOG_ERR, "No database directory specified.");
|
2023-03-09 02:46:04 +00:00
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
2023-04-19 00:33:38 +00:00
|
|
|
|
|
|
|
if (!getcwd(startDir, PATH_MAX))
|
2023-03-09 02:46:04 +00:00
|
|
|
{
|
2023-04-19 00:33:38 +00:00
|
|
|
Log(LOG_ERR, "Unable to determine current working directory.");
|
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (chdir(dbPath) != 0)
|
|
|
|
{
|
|
|
|
Log(LOG_ERR, "Unable to change into data directory: %s.", strerror(errno));
|
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
2023-03-09 02:46:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-04-19 00:33:38 +00:00
|
|
|
Log(LOG_DEBUG, "Changed working directory to: %s", dbPath);
|
|
|
|
}
|
2023-03-20 19:23:37 +00:00
|
|
|
|
2023-04-19 00:33:38 +00:00
|
|
|
matrixArgs.db = DbOpen(".", 0);
|
|
|
|
if (!matrixArgs.db)
|
|
|
|
{
|
|
|
|
Log(LOG_ERR, "Unable to open data directory as a database.");
|
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Log(LOG_DEBUG, "Opened database.");
|
2023-03-09 02:46:04 +00:00
|
|
|
}
|
|
|
|
|
2023-04-19 00:33:38 +00:00
|
|
|
if (!ConfigExists(matrixArgs.db))
|
|
|
|
{
|
|
|
|
RegTokenInfo *info;
|
|
|
|
|
|
|
|
Log(LOG_NOTICE, "No configuration exists in the opened database.");
|
|
|
|
Log(LOG_NOTICE, "A default configuration will be created, and a");
|
|
|
|
Log(LOG_NOTICE, "new single-use registration token that grants all");
|
|
|
|
Log(LOG_NOTICE, "privileges will be created so an admin user can");
|
|
|
|
Log(LOG_NOTICE, "be created to configure this database using the");
|
|
|
|
Log(LOG_NOTICE, "administrator API.");
|
2023-03-09 02:46:04 +00:00
|
|
|
|
2023-04-19 00:33:38 +00:00
|
|
|
if (!ConfigCreateDefault(matrixArgs.db))
|
|
|
|
{
|
|
|
|
Log(LOG_ERR, "Unable to create default configuration.");
|
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
2023-03-09 02:46:04 +00:00
|
|
|
|
2023-04-19 00:33:38 +00:00
|
|
|
token = StrRandom(32);
|
2024-01-14 01:02:07 +00:00
|
|
|
info = RegTokenCreate(matrixArgs.db, token, NULL, /* expires */ 0, /* uses */ 1, USER_ALL);
|
2023-04-19 00:33:38 +00:00
|
|
|
if (!info)
|
|
|
|
{
|
|
|
|
Free(token);
|
|
|
|
Log(LOG_ERR, "Unable to create admin registration token.");
|
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
RegTokenClose(info);
|
|
|
|
RegTokenFree(info);
|
2023-08-05 14:12:02 +00:00
|
|
|
|
2023-08-13 03:11:40 +00:00
|
|
|
/* Don't free token, because we need to print it when logging
|
|
|
|
* is set up. */
|
2023-03-09 02:46:04 +00:00
|
|
|
}
|
|
|
|
|
2023-04-19 00:33:38 +00:00
|
|
|
Log(LOG_NOTICE, "Loading configuration...");
|
2023-03-09 02:46:04 +00:00
|
|
|
|
2024-01-06 02:00:27 +00:00
|
|
|
ConfigLock(matrixArgs.db, &tConfig);
|
|
|
|
if (!tConfig.ok)
|
2023-03-09 02:46:04 +00:00
|
|
|
{
|
2024-01-06 02:00:27 +00:00
|
|
|
Log(LOG_ERR, tConfig.err);
|
2023-04-19 00:33:38 +00:00
|
|
|
exit = EXIT_FAILURE;
|
2023-03-09 02:46:04 +00:00
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2024-01-06 02:00:27 +00:00
|
|
|
if (!tConfig.log.timestampFormat || !StrEquals(tConfig.log.timestampFormat, "default"))
|
2023-03-09 02:46:04 +00:00
|
|
|
{
|
2024-01-06 02:00:27 +00:00
|
|
|
LogConfigTimeStampFormatSet(LogConfigGlobal(), tConfig.log.timestampFormat);
|
2023-03-09 02:46:04 +00:00
|
|
|
}
|
|
|
|
|
2024-01-06 02:00:27 +00:00
|
|
|
if (tConfig.log.color)
|
2023-03-09 02:46:04 +00:00
|
|
|
{
|
2023-03-22 14:52:04 +00:00
|
|
|
LogConfigFlagSet(LogConfigGlobal(), LOG_FLAG_COLOR);
|
2023-03-09 02:46:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-22 14:52:04 +00:00
|
|
|
LogConfigFlagClear(LogConfigGlobal(), LOG_FLAG_COLOR);
|
2023-03-09 02:46:04 +00:00
|
|
|
}
|
|
|
|
|
2024-01-06 02:00:27 +00:00
|
|
|
LogConfigLevelSet(
|
|
|
|
LogConfigGlobal(),
|
|
|
|
flags & ARG_VERBOSE ?
|
|
|
|
LOG_DEBUG :
|
|
|
|
ConfigLogLevelToSyslog(tConfig.log.level));
|
2023-03-09 02:46:04 +00:00
|
|
|
|
2024-01-06 02:00:27 +00:00
|
|
|
if (tConfig.log.output == CONFIG_LOG_OUTPUT_FILE)
|
2023-03-09 02:46:04 +00:00
|
|
|
{
|
2023-04-19 00:33:38 +00:00
|
|
|
logFile = StreamOpen("telodendria.log", "a");
|
2023-03-09 02:46:04 +00:00
|
|
|
|
|
|
|
if (!logFile)
|
|
|
|
{
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_ERR, "Unable to open log file for appending.");
|
2023-03-09 02:46:04 +00:00
|
|
|
exit = EXIT_FAILURE;
|
2024-01-06 02:00:27 +00:00
|
|
|
tConfig.log.output = CONFIG_LOG_OUTPUT_STDOUT;
|
2023-03-09 02:46:04 +00:00
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_INFO, "Logging to the log file. Check there for all future messages.");
|
|
|
|
LogConfigOutputSet(LogConfigGlobal(), logFile);
|
2023-03-09 02:46:04 +00:00
|
|
|
}
|
2024-01-06 02:00:27 +00:00
|
|
|
else if (tConfig.log.output == CONFIG_LOG_OUTPUT_STDOUT)
|
2023-03-09 02:46:04 +00:00
|
|
|
{
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_DEBUG, "Already logging to standard output.");
|
2023-03-09 02:46:04 +00:00
|
|
|
}
|
2024-01-06 02:00:27 +00:00
|
|
|
else if (tConfig.log.output == CONFIG_LOG_OUTPUT_SYSLOG)
|
2023-03-09 02:46:04 +00:00
|
|
|
{
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_INFO, "Logging to the syslog. Check there for all future messages.");
|
|
|
|
LogConfigFlagSet(LogConfigGlobal(), LOG_FLAG_SYSLOG);
|
2023-03-09 02:46:04 +00:00
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2023-08-13 03:11:40 +00:00
|
|
|
/* If a token was created with a default config, print it to the
|
|
|
|
* log */
|
2023-08-05 14:12:02 +00:00
|
|
|
if (token)
|
|
|
|
{
|
|
|
|
Log(LOG_NOTICE, "Admin Registration token: %s", token);
|
|
|
|
Free(token);
|
|
|
|
}
|
|
|
|
|
2024-01-06 02:00:27 +00:00
|
|
|
if (tConfig.pid)
|
|
|
|
{
|
|
|
|
pidFile = StreamOpen(tConfig.pid, "w+");
|
|
|
|
if (!pidFile)
|
|
|
|
{
|
|
|
|
char *msg = "Couldn't lock PID file at '%s'";
|
|
|
|
Log(LOG_ERR, msg, tConfig.pid);
|
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
pidPath = StrDuplicate(tConfig.pid);
|
|
|
|
StreamPrintf(pidFile, "%ld", (long) getpid());
|
|
|
|
StreamClose(pidFile);
|
|
|
|
}
|
|
|
|
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_DEBUG, "Configuration:");
|
|
|
|
LogConfigIndent(LogConfigGlobal());
|
2024-01-06 02:00:27 +00:00
|
|
|
Log(LOG_DEBUG, "Server Name: %s", tConfig.serverName);
|
|
|
|
Log(LOG_DEBUG, "Base URL: %s", tConfig.baseUrl);
|
|
|
|
Log(LOG_DEBUG, "Identity Server: %s", tConfig.identityServer);
|
|
|
|
Log(LOG_DEBUG, "Run As: %s:%s", tConfig.runAs.uid, tConfig.runAs.gid);
|
|
|
|
Log(LOG_DEBUG, "Max Cache: %ld", tConfig.maxCache);
|
|
|
|
Log(LOG_DEBUG, "Registration: %s", tConfig.registration ? "true" : "false");
|
|
|
|
Log(LOG_DEBUG, "Federation: %s", tConfig.federation ? "true" : "false");
|
2023-03-22 14:52:04 +00:00
|
|
|
LogConfigUnindent(LogConfigGlobal());
|
2023-03-09 02:46:04 +00:00
|
|
|
|
2023-03-22 17:00:48 +00:00
|
|
|
httpServers = ArrayCreate();
|
|
|
|
if (!httpServers)
|
|
|
|
{
|
|
|
|
Log(LOG_ERR, "Error setting up HTTP server.");
|
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2023-03-23 02:12:45 +00:00
|
|
|
/* Bind servers before possibly dropping permissions. */
|
2024-01-06 02:00:27 +00:00
|
|
|
for (i = 0; i < ArraySize(tConfig.listen); i++)
|
2023-03-23 02:12:45 +00:00
|
|
|
{
|
2024-01-06 02:00:27 +00:00
|
|
|
ConfigListener *serverCfg = ArrayGet(tConfig.listen, i);
|
|
|
|
|
|
|
|
HttpServerConfig args;
|
|
|
|
|
|
|
|
args.port = serverCfg->port;
|
|
|
|
args.threads = serverCfg->maxConnections;
|
|
|
|
args.maxConnections = serverCfg->maxConnections;
|
|
|
|
args.tlsCert = serverCfg->tls.cert;
|
|
|
|
args.tlsKey = serverCfg->tls.key;
|
|
|
|
args.flags = args.tlsCert && args.tlsKey ? HTTP_FLAG_TLS : HTTP_FLAG_NONE;
|
2023-03-23 02:12:45 +00:00
|
|
|
|
|
|
|
Log(LOG_DEBUG, "HTTP listener: %lu", i);
|
|
|
|
LogConfigIndent(LogConfigGlobal());
|
|
|
|
Log(LOG_DEBUG, "Port: %hu", serverCfg->port);
|
|
|
|
Log(LOG_DEBUG, "Threads: %u", serverCfg->threads);
|
|
|
|
Log(LOG_DEBUG, "Max Connections: %u", serverCfg->maxConnections);
|
2024-01-06 02:00:27 +00:00
|
|
|
Log(LOG_DEBUG, "Flags: %d", args.flags);
|
|
|
|
Log(LOG_DEBUG, "TLS Cert: %s", serverCfg->tls.cert);
|
|
|
|
Log(LOG_DEBUG, "TLS Key: %s", serverCfg->tls.key);
|
2023-03-23 02:12:45 +00:00
|
|
|
LogConfigUnindent(LogConfigGlobal());
|
|
|
|
|
|
|
|
|
2024-01-06 02:00:27 +00:00
|
|
|
args.handler = MatrixHttpHandler;
|
|
|
|
args.handlerArgs = &matrixArgs;
|
|
|
|
|
|
|
|
if (args.flags & HTTP_FLAG_TLS)
|
2023-03-23 02:12:45 +00:00
|
|
|
{
|
2024-01-14 01:02:07 +00:00
|
|
|
if (!UtilLastModified(serverCfg->tls.cert))
|
2023-03-23 02:12:45 +00:00
|
|
|
{
|
2024-01-06 02:00:27 +00:00
|
|
|
Log(LOG_ERR, "%s: %s", strerror(errno), serverCfg->tls.cert);
|
2023-03-23 02:12:45 +00:00
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2024-01-14 01:02:07 +00:00
|
|
|
if (UtilLastModified(serverCfg->tls.key))
|
2023-03-23 02:12:45 +00:00
|
|
|
{
|
2024-01-06 02:00:27 +00:00
|
|
|
Log(LOG_ERR, "%s: %s", strerror(errno), serverCfg->tls.key);
|
2023-03-23 02:12:45 +00:00
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-06 02:00:27 +00:00
|
|
|
server = HttpServerCreate(&args);
|
2023-03-23 02:12:45 +00:00
|
|
|
if (!server)
|
|
|
|
{
|
|
|
|
Log(LOG_ERR, "Unable to create HTTP server on port %d: %s",
|
|
|
|
serverCfg->port, strerror(errno));
|
|
|
|
|
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
ArrayAdd(httpServers, server);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ArraySize(httpServers))
|
2023-03-09 02:46:04 +00:00
|
|
|
{
|
2023-03-23 02:12:45 +00:00
|
|
|
Log(LOG_ERR, "No valid HTTP listeners specified in the configuration.");
|
2023-03-09 02:46:04 +00:00
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_DEBUG, "Running as uid:gid: %d:%d.", getuid(), getgid());
|
2023-03-09 02:46:04 +00:00
|
|
|
|
2024-01-06 02:00:27 +00:00
|
|
|
if (tConfig.runAs.uid && tConfig.runAs.gid)
|
2023-03-09 02:46:04 +00:00
|
|
|
{
|
2024-01-06 02:00:27 +00:00
|
|
|
userInfo = getpwnam(tConfig.runAs.uid);
|
|
|
|
groupInfo = getgrnam(tConfig.runAs.gid);
|
2023-03-09 02:46:04 +00:00
|
|
|
|
|
|
|
if (!userInfo || !groupInfo)
|
|
|
|
{
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_ERR, "Unable to locate the user/group specified in the configuration.");
|
2023-03-09 02:46:04 +00:00
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_DEBUG, "Found user/group information using getpwnam() and getgrnam().");
|
2023-03-09 02:46:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_DEBUG, "No user/group info specified in the config.");
|
2023-03-09 02:46:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (getuid() == 0)
|
|
|
|
{
|
|
|
|
if (userInfo && groupInfo)
|
|
|
|
{
|
|
|
|
if (setgid(groupInfo->gr_gid) != 0 || setuid(userInfo->pw_uid) != 0)
|
|
|
|
{
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_ERR, "Unable to set process uid/gid.");
|
2023-03-09 02:46:04 +00:00
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-01-06 02:00:27 +00:00
|
|
|
Log(LOG_DEBUG, "Set uid/gid to %s:%s.", tConfig.runAs.uid, tConfig.runAs.gid);
|
2023-03-09 02:46:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_WARNING, "We are running as root, and we are not dropping to another user");
|
|
|
|
Log(LOG_WARNING, "because none was specified in the configuration file.");
|
|
|
|
Log(LOG_WARNING, "This is probably a security issue.");
|
2023-03-09 02:46:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-01-06 02:00:27 +00:00
|
|
|
if (tConfig.runAs.uid && tConfig.runAs.gid)
|
2023-03-09 02:46:04 +00:00
|
|
|
{
|
|
|
|
if (getuid() != userInfo->pw_uid || getgid() != groupInfo->gr_gid)
|
|
|
|
{
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_WARNING, "Not running as the uid/gid specified in the configuration.");
|
2023-03-09 02:46:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_DEBUG, "Running as the uid/gid specified in the configuration.");
|
2023-03-09 02:46:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-06 02:00:27 +00:00
|
|
|
if (!tConfig.maxCache)
|
2023-03-09 02:46:04 +00:00
|
|
|
{
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_WARNING, "Database caching is disabled.");
|
|
|
|
Log(LOG_WARNING, "If this is not what you intended, check the config file");
|
|
|
|
Log(LOG_WARNING, "and ensure that maxCache is a valid number of bytes.");
|
2023-03-09 02:46:04 +00:00
|
|
|
}
|
|
|
|
|
2024-01-06 02:00:27 +00:00
|
|
|
DbMaxCacheSet(matrixArgs.db, tConfig.maxCache);
|
2023-03-09 02:46:04 +00:00
|
|
|
|
2024-01-06 02:00:27 +00:00
|
|
|
ConfigUnlock(&tConfig);
|
2023-03-09 02:46:04 +00:00
|
|
|
|
|
|
|
cron = CronCreate(60 * 1000); /* 1-minute tick */
|
|
|
|
if (!cron)
|
|
|
|
{
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_ERR, "Unable to set up job scheduler.");
|
2023-03-09 02:46:04 +00:00
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_DEBUG, "Registering jobs...");
|
2023-03-09 02:46:04 +00:00
|
|
|
|
|
|
|
CronEvery(cron, 30 * 60 * 1000, (JobFunc *) UiaCleanup, &matrixArgs);
|
|
|
|
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_NOTICE, "Starting job scheduler...");
|
2023-03-09 02:46:04 +00:00
|
|
|
CronStart(cron);
|
|
|
|
|
2023-04-20 17:37:18 +00:00
|
|
|
Log(LOG_NOTICE, "Building routing tree...");
|
|
|
|
matrixArgs.router = RouterBuild();
|
|
|
|
if (!matrixArgs.router)
|
|
|
|
{
|
|
|
|
Log(LOG_ERR, "Unable to build routing tree.");
|
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_NOTICE, "Starting server...");
|
2023-03-09 02:46:04 +00:00
|
|
|
|
2023-03-22 17:00:48 +00:00
|
|
|
for (i = 0; i < ArraySize(httpServers); i++)
|
2023-03-09 02:46:04 +00:00
|
|
|
{
|
2023-03-23 02:12:45 +00:00
|
|
|
HttpServerConfig *serverCfg;
|
|
|
|
|
2023-03-22 17:00:48 +00:00
|
|
|
server = ArrayGet(httpServers, i);
|
2023-03-23 02:12:45 +00:00
|
|
|
serverCfg = HttpServerConfigGet(server);
|
2023-03-22 17:00:48 +00:00
|
|
|
|
|
|
|
if (!HttpServerStart(server))
|
|
|
|
{
|
2023-03-23 02:12:45 +00:00
|
|
|
Log(LOG_ERR, "Unable to start HTTP server %lu on port %hu.", i, serverCfg->port);
|
2023-03-22 17:00:48 +00:00
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Log(LOG_DEBUG, "Started HTTP server %lu.", i);
|
2023-03-23 02:12:45 +00:00
|
|
|
Log(LOG_INFO, "Listening on port: %hu", serverCfg->port);
|
2023-03-22 17:00:48 +00:00
|
|
|
}
|
2023-03-09 02:46:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-19 00:33:38 +00:00
|
|
|
sigAction.sa_handler = SignalHandler;
|
2023-03-09 02:46:04 +00:00
|
|
|
sigfillset(&sigAction.sa_mask);
|
|
|
|
sigAction.sa_flags = SA_RESTART;
|
|
|
|
|
2023-03-23 16:39:15 +00:00
|
|
|
#define SIGACTION(sig, act, oact) \
|
|
|
|
if (sigaction(sig, act, oact) < 0) \
|
|
|
|
{ \
|
|
|
|
Log(LOG_ERR, "Unable to install signal handler: %s", #sig); \
|
|
|
|
exit = EXIT_FAILURE; \
|
|
|
|
goto finish; \
|
|
|
|
} \
|
|
|
|
else \
|
|
|
|
{ \
|
|
|
|
Log(LOG_DEBUG, "Installed signal handler: %s", #sig); \
|
2023-03-23 02:12:45 +00:00
|
|
|
}
|
2023-03-09 02:46:04 +00:00
|
|
|
|
2023-03-23 16:39:15 +00:00
|
|
|
SIGACTION(SIGINT, &sigAction, NULL);
|
2023-04-24 16:23:50 +00:00
|
|
|
SIGACTION(SIGTERM, &sigAction, NULL);
|
2023-03-23 16:39:15 +00:00
|
|
|
SIGACTION(SIGPIPE, &sigAction, NULL);
|
2023-04-19 00:33:38 +00:00
|
|
|
SIGACTION(SIGUSR1, &sigAction, NULL);
|
2023-03-23 16:39:15 +00:00
|
|
|
|
|
|
|
#undef SIGACTION
|
|
|
|
|
2023-03-22 17:00:48 +00:00
|
|
|
/* Block this thread until the servers are terminated by a signal
|
2023-03-09 02:46:04 +00:00
|
|
|
* handler */
|
2023-03-22 17:00:48 +00:00
|
|
|
for (i = 0; i < ArraySize(httpServers); i++)
|
|
|
|
{
|
|
|
|
server = ArrayGet(httpServers, i);
|
|
|
|
HttpServerJoin(server);
|
2023-03-23 02:12:45 +00:00
|
|
|
Log(LOG_DEBUG, "Joined HTTP server %lu.", i);
|
2023-03-22 17:00:48 +00:00
|
|
|
}
|
2023-03-09 02:46:04 +00:00
|
|
|
|
|
|
|
finish:
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_NOTICE, "Shutting down...");
|
2023-03-22 17:00:48 +00:00
|
|
|
if (httpServers)
|
2023-03-09 02:46:04 +00:00
|
|
|
{
|
2023-03-22 17:00:48 +00:00
|
|
|
for (i = 0; i < ArraySize(httpServers); i++)
|
|
|
|
{
|
2023-03-23 02:12:45 +00:00
|
|
|
Log(LOG_DEBUG, "Freeing HTTP server %lu...", i);
|
2023-03-22 17:00:48 +00:00
|
|
|
server = ArrayGet(httpServers, i);
|
|
|
|
HttpServerStop(server);
|
|
|
|
HttpServerFree(server);
|
2023-03-23 02:12:45 +00:00
|
|
|
Log(LOG_DEBUG, "Freed HTTP server %lu.", i);
|
2023-03-22 17:00:48 +00:00
|
|
|
}
|
|
|
|
ArrayFree(httpServers);
|
2023-05-25 18:26:17 +00:00
|
|
|
httpServers = NULL;
|
|
|
|
|
2023-03-23 02:12:45 +00:00
|
|
|
Log(LOG_DEBUG, "Freed HTTP servers array.");
|
2023-03-09 02:46:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cron)
|
|
|
|
{
|
2023-05-27 18:22:38 +00:00
|
|
|
Log(LOG_DEBUG, "Waiting on background jobs...");
|
2023-03-09 02:46:04 +00:00
|
|
|
CronStop(cron);
|
|
|
|
CronFree(cron);
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_DEBUG, "Stopped and freed job scheduler.");
|
2023-03-09 02:46:04 +00:00
|
|
|
}
|
|
|
|
|
2024-01-06 02:00:27 +00:00
|
|
|
ConfigUnlock(&tConfig);
|
2023-04-19 00:33:38 +00:00
|
|
|
Log(LOG_DEBUG, "Unlocked configuration.");
|
|
|
|
|
2023-03-09 02:46:04 +00:00
|
|
|
DbClose(matrixArgs.db);
|
2023-03-23 02:12:45 +00:00
|
|
|
Log(LOG_DEBUG, "Closed database.");
|
2023-03-09 02:46:04 +00:00
|
|
|
|
2023-04-14 21:20:56 +00:00
|
|
|
HttpRouterFree(matrixArgs.router);
|
|
|
|
Log(LOG_DEBUG, "Freed routing tree.");
|
|
|
|
|
2024-01-06 02:00:27 +00:00
|
|
|
if (pidPath)
|
|
|
|
{
|
|
|
|
remove(pidPath);
|
|
|
|
Free(pidPath);
|
|
|
|
}
|
|
|
|
|
2023-03-18 14:32:09 +00:00
|
|
|
/*
|
|
|
|
* Uninstall the memory hook because it uses the Log
|
|
|
|
* API, whose configuration is being freed now, so it
|
|
|
|
* won't work anymore.
|
|
|
|
*/
|
|
|
|
MemoryHook(NULL, NULL);
|
|
|
|
|
2023-04-19 00:33:38 +00:00
|
|
|
StreamClose(logFile);
|
|
|
|
|
|
|
|
if (restart)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Change back into starting directory so initial chdir()
|
|
|
|
* call works.
|
|
|
|
*/
|
2023-04-20 13:49:25 +00:00
|
|
|
if (chdir(startDir) != 0)
|
|
|
|
{
|
|
|
|
/* TODO: Seems problematic, what do we do? */
|
|
|
|
}
|
2023-04-19 00:33:38 +00:00
|
|
|
goto start;
|
|
|
|
}
|
|
|
|
|
2023-03-09 02:46:04 +00:00
|
|
|
return exit;
|
|
|
|
}
|