telodendria/src/TelodendriaConfig.c

308 lines
7.8 KiB
C
Raw Normal View History

/*
2022-12-26 15:52:52 +00:00
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
*
* 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 <TelodendriaConfig.h>
#include <Memory.h>
2022-12-09 23:57:30 +00:00
#include <Json.h>
#include <HashMap.h>
#include <Log.h>
#include <Array.h>
#include <Str.h>
2022-12-09 23:57:30 +00:00
#include <Db.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
2022-12-09 23:57:30 +00:00
#define CONFIG_REQUIRE(key, type) \
value = HashMapGet(config, key); \
if (!value) \
{ \
Log(lc, LOG_ERR, "Missing required " key " directive."); \
goto error; \
} \
if (JsonValueType(value) == JSON_NULL) \
{ \
Log(lc, LOG_ERR, "Missing value for " key " directive."); \
goto error; \
} \
if (JsonValueType(value) != type) \
{ \
Log(lc, LOG_ERR, "Expected " key " to be of type " #type); \
goto error; \
}
2022-12-09 23:57:30 +00:00
#define CONFIG_COPY_STRING(into) \
into = StrDuplicate(JsonValueAsString(value));
2022-12-09 23:57:30 +00:00
#define CONFIG_OPTIONAL_STRING(into, key, default) \
value = HashMapGet(config, key); \
if (value && JsonValueType(value) != JSON_NULL) \
{ \
if (JsonValueType(value) != JSON_STRING) \
{ \
Log(lc, LOG_ERR, "Expected " key " to be of type JSON_STRING"); \
goto error; \
} \
into = StrDuplicate(JsonValueAsString(value)); \
2022-12-09 23:57:30 +00:00
} \
else \
{ \
Log(lc, LOG_INFO, "Using default value " #default " for " key "."); \
into = default ? StrDuplicate(default) : NULL; \
2022-12-09 23:57:30 +00:00
}
2022-12-09 23:57:30 +00:00
#define CONFIG_OPTIONAL_INTEGER(into, key, default) \
value = HashMapGet(config, key); \
if (value && JsonValueType(value) != JSON_NULL) \
{ \
if (JsonValueType(value) != JSON_INTEGER) \
{ \
Log(lc, LOG_ERR, "Expected " key " to be of type JSON_INTEGER"); \
goto error; \
} \
into = JsonValueAsInteger(value); \
} \
else \
{ \
Log(lc, LOG_INFO, "Using default value " #default " for " key "."); \
into = default; \
}
2022-12-09 23:57:30 +00:00
int
ConfigParseRunAs(LogConfig * lc, TelodendriaConfig * tConfig, HashMap * config)
{
2022-12-09 23:57:30 +00:00
JsonValue *value;
2022-12-09 23:57:30 +00:00
CONFIG_REQUIRE("uid", JSON_STRING);
CONFIG_COPY_STRING(tConfig->uid);
2022-12-09 23:57:30 +00:00
CONFIG_OPTIONAL_STRING(tConfig->gid, "gid", tConfig->uid);
2022-12-09 23:57:30 +00:00
return 1;
2022-12-09 23:57:30 +00:00
error:
return 0;
}
2022-12-09 23:57:30 +00:00
int
ConfigParseLog(LogConfig * lc, TelodendriaConfig * tConfig, HashMap * config)
{
JsonValue *value;
char *str;
2022-12-09 23:57:30 +00:00
CONFIG_REQUIRE("output", JSON_STRING);
str = JsonValueAsString(value);
if (strcmp(str, "stdout") == 0)
{
2022-12-09 23:57:30 +00:00
tConfig->flags |= TELODENDRIA_LOG_STDOUT;
}
2022-12-09 23:57:30 +00:00
else if (strcmp(str, "file") == 0)
{
2022-12-09 23:57:30 +00:00
tConfig->flags |= TELODENDRIA_LOG_FILE;
}
2022-12-09 23:57:30 +00:00
else if (strcmp(str, "syslog") == 0)
{
2022-12-09 23:57:30 +00:00
tConfig->flags |= TELODENDRIA_LOG_SYSLOG;
}
else
{
2022-12-09 23:57:30 +00:00
Log(lc, LOG_ERR, "Invalid value for log.output: '%s'.", str);
goto error;
}
2022-12-09 23:57:30 +00:00
CONFIG_OPTIONAL_STRING(str, "level", "message");
2022-12-09 23:57:30 +00:00
if (strcmp(str, "message") == 0)
{
2022-12-09 23:57:30 +00:00
tConfig->logLevel = LOG_INFO;
}
2022-12-09 23:57:30 +00:00
else if (strcmp(str, "debug") == 0)
{
2022-12-09 23:57:30 +00:00
tConfig->logLevel = LOG_DEBUG;
}
2022-12-09 23:57:30 +00:00
else if (strcmp(str, "notice") == 0)
2022-08-10 15:58:39 +00:00
{
2022-12-09 23:57:30 +00:00
tConfig->logLevel = LOG_NOTICE;
}
2022-12-09 23:57:30 +00:00
else if (strcmp(str, "warning") == 0)
{
2022-12-09 23:57:30 +00:00
tConfig->logLevel = LOG_WARNING;
2022-08-10 15:58:39 +00:00
}
2022-12-09 23:57:30 +00:00
else if (strcmp(str, "error") == 0)
{
2022-12-09 23:57:30 +00:00
tConfig->logLevel = LOG_ERR;
}
else
{
2022-12-09 23:57:30 +00:00
Log(lc, LOG_ERR, "Invalid value for log.level: '%s'.", tConfig->logLevel);
goto error;
}
2022-12-09 23:57:30 +00:00
Free(str);
CONFIG_OPTIONAL_STRING(tConfig->logTimestamp, "timestampFormat", "default");
if (strcmp(tConfig->logTimestamp, "none") == 0)
2022-08-21 16:35:16 +00:00
{
2022-12-09 23:57:30 +00:00
Free(tConfig->logTimestamp);
tConfig->logTimestamp = NULL;
2022-08-21 16:35:16 +00:00
}
2022-12-09 23:57:30 +00:00
value = HashMapGet(config, "color");
if (value && JsonValueType(value) != JSON_NULL)
2022-08-21 16:35:16 +00:00
{
2022-12-09 23:57:30 +00:00
if (JsonValueType(value) != JSON_BOOLEAN)
2022-08-21 16:35:16 +00:00
{
2022-12-09 23:57:30 +00:00
Log(lc, LOG_ERR, "Expected type JSON_BOOLEAN for log.color.");
goto error;
2022-08-21 16:35:16 +00:00
}
2022-12-09 23:57:30 +00:00
if (JsonValueAsBoolean(value))
2022-08-21 16:35:16 +00:00
{
2022-12-09 23:57:30 +00:00
tConfig->flags |= TELODENDRIA_LOG_COLOR;
2022-08-21 16:35:16 +00:00
}
}
2022-12-09 23:57:30 +00:00
return 1;
error:
return 0;
}
2022-08-21 16:35:16 +00:00
2022-12-09 23:57:30 +00:00
TelodendriaConfig *
TelodendriaConfigParse(HashMap * config, LogConfig * lc)
{
TelodendriaConfig *tConfig;
JsonValue *value;
2022-12-09 23:57:30 +00:00
if (!config || !lc)
{
2022-12-09 23:57:30 +00:00
return NULL;
}
2022-12-09 23:57:30 +00:00
tConfig = Malloc(sizeof(TelodendriaConfig));
if (!tConfig)
{
2022-12-09 23:57:30 +00:00
return NULL;
}
2022-12-09 23:57:30 +00:00
memset(tConfig, 0, sizeof(TelodendriaConfig));
CONFIG_OPTIONAL_INTEGER(tConfig->listenPort, "listen", 8008);
CONFIG_REQUIRE("serverName", JSON_STRING);
CONFIG_COPY_STRING(tConfig->serverName);
value = HashMapGet(config, "baseUrl");
if (value)
{
2022-12-09 23:57:30 +00:00
CONFIG_COPY_STRING(tConfig->baseUrl);
}
2022-12-09 23:57:30 +00:00
else
{
2022-12-09 23:57:30 +00:00
Log(lc, LOG_WARNING, "Base URL not specified. Assuming it's 'https://%s'.", tConfig->serverName);
tConfig->baseUrl = Malloc(strlen(tConfig->serverName) + 10);
if (!tConfig->baseUrl)
{
Log(lc, LOG_ERR, "Error allocating memory for default config value 'baseUrl'.");
goto error;
}
sprintf(tConfig->baseUrl, "https://%s", tConfig->serverName);
}
2022-12-09 23:57:30 +00:00
CONFIG_OPTIONAL_STRING(tConfig->identityServer, "identityServer", NULL);
2022-12-09 23:57:30 +00:00
value = HashMapGet(config, "runAs");
if (value && JsonValueType(value) != JSON_NULL)
{
2022-12-09 23:57:30 +00:00
if (JsonValueType(value) == JSON_OBJECT)
{
2022-12-09 23:57:30 +00:00
if (!ConfigParseRunAs(lc, tConfig, JsonValueAsObject(value)))
{
goto error;
}
}
2022-12-09 23:57:30 +00:00
else
{
2022-12-09 23:57:30 +00:00
Log(lc, LOG_ERR, "Config directive 'runAs' should be a JSON object");
Log(lc, LOG_ERR, "that contains a 'uid' and 'gid'.");
goto error;
}
2022-12-09 23:57:30 +00:00
}
2022-12-09 23:57:30 +00:00
CONFIG_REQUIRE("dataDir", JSON_STRING);
CONFIG_COPY_STRING(tConfig->dataDir);
2022-12-09 23:57:30 +00:00
CONFIG_OPTIONAL_INTEGER(tConfig->threads, "threads", 1);
CONFIG_OPTIONAL_INTEGER(tConfig->maxConnections, "maxConnections", 32);
CONFIG_OPTIONAL_INTEGER(tConfig->maxCache, "maxCache", 0);
2022-12-09 23:57:30 +00:00
CONFIG_REQUIRE("federation", JSON_BOOLEAN);
if (JsonValueAsBoolean(value))
2022-10-13 13:09:26 +00:00
{
2022-12-09 23:57:30 +00:00
tConfig->flags |= TELODENDRIA_FEDERATION;
2022-10-13 13:09:26 +00:00
}
2022-12-09 23:57:30 +00:00
CONFIG_REQUIRE("registration", JSON_BOOLEAN);
if (JsonValueAsBoolean(value))
2022-10-13 13:09:26 +00:00
{
2022-12-09 23:57:30 +00:00
tConfig->flags |= TELODENDRIA_REGISTRATION;
}
2022-12-09 23:57:30 +00:00
CONFIG_REQUIRE("log", JSON_OBJECT);
if (!ConfigParseLog(lc, tConfig, JsonValueAsObject(value)))
{
2022-10-13 13:09:26 +00:00
goto error;
}
return tConfig;
2022-12-09 23:57:30 +00:00
error:
TelodendriaConfigFree(tConfig);
return NULL;
}
void
TelodendriaConfigFree(TelodendriaConfig * tConfig)
{
if (!tConfig)
{
return;
}
Free(tConfig->serverName);
Free(tConfig->baseUrl);
Free(tConfig->identityServer);
Free(tConfig->uid);
Free(tConfig->gid);
Free(tConfig->dataDir);
Free(tConfig->logTimestamp);
Free(tConfig);
}