2023-03-22 17:17:30 +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-22 17:17:30 +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 <Config.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/Memory.h>
|
|
|
|
#include <Cytoplasm/Json.h>
|
|
|
|
#include <Cytoplasm/HashMap.h>
|
|
|
|
#include <Cytoplasm/Array.h>
|
|
|
|
#include <Cytoplasm/Str.h>
|
|
|
|
#include <Cytoplasm/Db.h>
|
|
|
|
#include <Cytoplasm/HttpServer.h>
|
|
|
|
#include <Cytoplasm/Log.h>
|
|
|
|
#include <Cytoplasm/Int64.h>
|
2023-03-22 17:17:30 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <string.h>
|
2023-04-19 00:33:38 +00:00
|
|
|
#include <limits.h>
|
2023-03-22 17:17:30 +00:00
|
|
|
|
2023-09-25 20:02:09 +00:00
|
|
|
#ifndef HOST_NAME_MAX
|
|
|
|
#define HOST_NAME_MAX _POSIX_HOST_NAME_MAX
|
|
|
|
#endif
|
|
|
|
|
2023-03-22 17:17:30 +00:00
|
|
|
#define CONFIG_REQUIRE(key, type) \
|
|
|
|
value = HashMapGet(config, key); \
|
|
|
|
if (!value) \
|
|
|
|
{ \
|
2023-04-19 00:33:38 +00:00
|
|
|
tConfig->err = "Missing required " key " directive."; \
|
2023-03-22 17:17:30 +00:00
|
|
|
goto error; \
|
|
|
|
} \
|
|
|
|
if (JsonValueType(value) == JSON_NULL) \
|
|
|
|
{ \
|
2023-04-19 00:33:38 +00:00
|
|
|
tConfig->err = "Missing value for " key " directive."; \
|
2023-03-22 17:17:30 +00:00
|
|
|
goto error; \
|
|
|
|
} \
|
|
|
|
if (JsonValueType(value) != type) \
|
|
|
|
{ \
|
2023-04-19 00:33:38 +00:00
|
|
|
tConfig->err = "Expected " key " to be of type " #type; \
|
2023-03-22 17:17:30 +00:00
|
|
|
goto error; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define CONFIG_COPY_STRING(into) \
|
|
|
|
into = StrDuplicate(JsonValueAsString(value));
|
|
|
|
|
|
|
|
#define CONFIG_OPTIONAL_STRING(into, key, default) \
|
|
|
|
value = HashMapGet(config, key); \
|
|
|
|
if (value && JsonValueType(value) != JSON_NULL) \
|
|
|
|
{ \
|
|
|
|
if (JsonValueType(value) != JSON_STRING) \
|
|
|
|
{ \
|
2023-04-19 00:33:38 +00:00
|
|
|
tConfig->err = "Expected " key " to be of type JSON_STRING"; \
|
2023-03-22 17:17:30 +00:00
|
|
|
goto error; \
|
|
|
|
} \
|
|
|
|
into = StrDuplicate(JsonValueAsString(value)); \
|
|
|
|
} \
|
|
|
|
else \
|
|
|
|
{ \
|
|
|
|
into = default ? StrDuplicate(default) : NULL; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define CONFIG_OPTIONAL_INTEGER(into, key, default) \
|
|
|
|
value = HashMapGet(config, key); \
|
|
|
|
if (value && JsonValueType(value) != JSON_NULL) \
|
|
|
|
{ \
|
|
|
|
if (JsonValueType(value) != JSON_INTEGER) \
|
|
|
|
{ \
|
2023-04-19 00:33:38 +00:00
|
|
|
tConfig->err = "Expected " key " to be of type JSON_INTEGER"; \
|
2023-03-22 17:17:30 +00:00
|
|
|
goto error; \
|
|
|
|
} \
|
2023-08-13 03:11:40 +00:00
|
|
|
into = Int64Low(JsonValueAsInteger(value)); \
|
2023-03-22 17:17:30 +00:00
|
|
|
} \
|
|
|
|
else \
|
|
|
|
{ \
|
|
|
|
into = default; \
|
|
|
|
}
|
|
|
|
|
2023-03-23 02:12:45 +00:00
|
|
|
static int
|
2023-03-22 17:17:30 +00:00
|
|
|
ConfigParseRunAs(Config * tConfig, HashMap * config)
|
|
|
|
{
|
|
|
|
JsonValue *value;
|
|
|
|
|
|
|
|
CONFIG_REQUIRE("uid", JSON_STRING);
|
|
|
|
CONFIG_COPY_STRING(tConfig->uid);
|
|
|
|
|
|
|
|
CONFIG_OPTIONAL_STRING(tConfig->gid, "gid", tConfig->uid);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
error:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-03-23 02:12:45 +00:00
|
|
|
static int
|
|
|
|
ConfigParseListen(Config * tConfig, Array * listen)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (!ArraySize(listen))
|
|
|
|
{
|
2023-04-19 00:33:38 +00:00
|
|
|
tConfig->err = "Listen array cannot be empty; you must specify at least one listener.";
|
2023-03-23 02:12:45 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!tConfig->servers)
|
|
|
|
{
|
|
|
|
tConfig->servers = ArrayCreate();
|
|
|
|
if (!tConfig->servers)
|
|
|
|
{
|
2023-04-19 00:33:38 +00:00
|
|
|
tConfig->err = "Unable to allocate memory for listener configurations.";
|
2023-03-23 02:12:45 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < ArraySize(listen); i++)
|
|
|
|
{
|
|
|
|
JsonValue *val = ArrayGet(listen, i);
|
|
|
|
HashMap *obj;
|
|
|
|
HttpServerConfig *serverCfg = Malloc(sizeof(HttpServerConfig));
|
|
|
|
|
|
|
|
if (!serverCfg)
|
|
|
|
{
|
2023-04-19 00:33:38 +00:00
|
|
|
tConfig->err = "Unable to allocate memory for listener configuration.";
|
2023-03-23 02:12:45 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (JsonValueType(val) != JSON_OBJECT)
|
|
|
|
{
|
2023-04-19 00:33:38 +00:00
|
|
|
tConfig->err = "Invalid value in listener array. All listeners must be objects.";
|
2023-03-23 02:12:45 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
obj = JsonValueAsObject(val);
|
|
|
|
|
2023-08-13 03:11:40 +00:00
|
|
|
serverCfg->port = Int64Low(JsonValueAsInteger(HashMapGet(obj, "port")));
|
|
|
|
serverCfg->threads = Int64Low(JsonValueAsInteger(HashMapGet(obj, "threads")));
|
|
|
|
serverCfg->maxConnections = Int64Low(JsonValueAsInteger(HashMapGet(obj, "maxConnections")));
|
2023-03-23 02:12:45 +00:00
|
|
|
|
|
|
|
if (!serverCfg->port)
|
|
|
|
{
|
|
|
|
Free(serverCfg);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!serverCfg->threads)
|
|
|
|
{
|
|
|
|
serverCfg->threads = 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!serverCfg->maxConnections)
|
|
|
|
{
|
|
|
|
serverCfg->maxConnections = 32;
|
|
|
|
}
|
|
|
|
|
|
|
|
val = HashMapGet(obj, "tls");
|
|
|
|
if ((JsonValueType(val) == JSON_BOOLEAN && !JsonValueAsBoolean(val)) || JsonValueType(val) == JSON_NULL)
|
|
|
|
{
|
|
|
|
serverCfg->flags = HTTP_FLAG_NONE;
|
|
|
|
serverCfg->tlsCert = NULL;
|
|
|
|
serverCfg->tlsKey = NULL;
|
|
|
|
}
|
|
|
|
else if (JsonValueType(val) != JSON_OBJECT)
|
|
|
|
{
|
2023-04-19 00:33:38 +00:00
|
|
|
tConfig->err = "Invalid value for listener.tls. It must be an object.";
|
2023-03-23 02:12:45 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
serverCfg->flags = HTTP_FLAG_TLS;
|
|
|
|
|
|
|
|
obj = JsonValueAsObject(val);
|
|
|
|
serverCfg->tlsCert = StrDuplicate(JsonValueAsString(HashMapGet(obj, "cert")));
|
|
|
|
serverCfg->tlsKey = StrDuplicate(JsonValueAsString(HashMapGet(obj, "key")));
|
|
|
|
|
|
|
|
if (!serverCfg->tlsCert || !serverCfg->tlsKey)
|
|
|
|
{
|
2023-04-19 00:33:38 +00:00
|
|
|
tConfig->err = "TLS cert and key must both be valid file names.";
|
2023-03-23 02:12:45 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ArrayAdd(tConfig->servers, serverCfg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
error:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2023-03-22 17:17:30 +00:00
|
|
|
ConfigParseLog(Config * tConfig, HashMap * config)
|
|
|
|
{
|
|
|
|
JsonValue *value;
|
|
|
|
char *str;
|
|
|
|
|
|
|
|
CONFIG_REQUIRE("output", JSON_STRING);
|
|
|
|
str = JsonValueAsString(value);
|
|
|
|
|
2023-05-06 22:34:36 +00:00
|
|
|
if (StrEquals(str, "stdout"))
|
2023-03-22 17:17:30 +00:00
|
|
|
{
|
|
|
|
tConfig->flags |= CONFIG_LOG_STDOUT;
|
|
|
|
}
|
2023-05-06 22:34:36 +00:00
|
|
|
else if (StrEquals(str, "file"))
|
2023-03-22 17:17:30 +00:00
|
|
|
{
|
|
|
|
tConfig->flags |= CONFIG_LOG_FILE;
|
|
|
|
}
|
2023-05-06 22:34:36 +00:00
|
|
|
else if (StrEquals(str, "syslog"))
|
2023-03-22 17:17:30 +00:00
|
|
|
{
|
|
|
|
tConfig->flags |= CONFIG_LOG_SYSLOG;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-04-19 00:33:38 +00:00
|
|
|
tConfig->err = "Invalid value for log.output";
|
2023-03-22 17:17:30 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
CONFIG_OPTIONAL_STRING(str, "level", "message");
|
|
|
|
|
2023-05-06 22:34:36 +00:00
|
|
|
if (StrEquals(str, "message"))
|
2023-03-22 17:17:30 +00:00
|
|
|
{
|
|
|
|
tConfig->logLevel = LOG_INFO;
|
|
|
|
}
|
2023-05-06 22:34:36 +00:00
|
|
|
else if (StrEquals(str, "debug"))
|
2023-03-22 17:17:30 +00:00
|
|
|
{
|
|
|
|
tConfig->logLevel = LOG_DEBUG;
|
|
|
|
}
|
2023-05-06 22:34:36 +00:00
|
|
|
else if (StrEquals(str, "notice"))
|
2023-03-22 17:17:30 +00:00
|
|
|
{
|
|
|
|
tConfig->logLevel = LOG_NOTICE;
|
|
|
|
}
|
2023-05-06 22:34:36 +00:00
|
|
|
else if (StrEquals(str, "warning"))
|
2023-03-22 17:17:30 +00:00
|
|
|
{
|
|
|
|
tConfig->logLevel = LOG_WARNING;
|
|
|
|
}
|
2023-05-06 22:34:36 +00:00
|
|
|
else if (StrEquals(str, "error"))
|
2023-03-22 17:17:30 +00:00
|
|
|
{
|
|
|
|
tConfig->logLevel = LOG_ERR;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-04-19 00:33:38 +00:00
|
|
|
tConfig->err = "Invalid value for log.level.";
|
2023-03-22 17:17:30 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
Free(str);
|
|
|
|
|
|
|
|
CONFIG_OPTIONAL_STRING(tConfig->logTimestamp, "timestampFormat", "default");
|
|
|
|
|
2023-05-06 22:34:36 +00:00
|
|
|
if (StrEquals(tConfig->logTimestamp, "none"))
|
2023-03-22 17:17:30 +00:00
|
|
|
{
|
|
|
|
Free(tConfig->logTimestamp);
|
|
|
|
tConfig->logTimestamp = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
value = HashMapGet(config, "color");
|
|
|
|
if (value && JsonValueType(value) != JSON_NULL)
|
|
|
|
{
|
|
|
|
if (JsonValueType(value) != JSON_BOOLEAN)
|
|
|
|
{
|
2023-04-19 00:33:38 +00:00
|
|
|
tConfig->err = "Expected type JSON_BOOLEAN for log.color.";
|
2023-03-22 17:17:30 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (JsonValueAsBoolean(value))
|
|
|
|
{
|
|
|
|
tConfig->flags |= CONFIG_LOG_COLOR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
error:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-04-19 00:33:38 +00:00
|
|
|
void
|
|
|
|
ConfigFree(Config * tConfig)
|
|
|
|
{
|
|
|
|
if (!tConfig)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Free(tConfig->serverName);
|
|
|
|
Free(tConfig->baseUrl);
|
|
|
|
Free(tConfig->identityServer);
|
|
|
|
|
|
|
|
Free(tConfig->uid);
|
|
|
|
Free(tConfig->gid);
|
|
|
|
|
|
|
|
Free(tConfig->logTimestamp);
|
|
|
|
|
|
|
|
if (tConfig->servers)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < ArraySize(tConfig->servers); i++)
|
|
|
|
{
|
|
|
|
HttpServerConfig *serverCfg = ArrayGet(tConfig->servers, i);
|
|
|
|
|
|
|
|
Free(serverCfg->tlsCert);
|
|
|
|
Free(serverCfg->tlsKey);
|
|
|
|
Free(serverCfg);
|
|
|
|
}
|
|
|
|
|
|
|
|
ArrayFree(tConfig->servers);
|
|
|
|
}
|
|
|
|
|
|
|
|
Free(tConfig);
|
|
|
|
}
|
|
|
|
|
2023-03-22 17:17:30 +00:00
|
|
|
Config *
|
2023-04-19 02:07:38 +00:00
|
|
|
ConfigParse(HashMap * config)
|
2023-03-22 17:17:30 +00:00
|
|
|
{
|
|
|
|
Config *tConfig;
|
|
|
|
JsonValue *value;
|
|
|
|
|
|
|
|
if (!config)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
tConfig = Malloc(sizeof(Config));
|
|
|
|
if (!tConfig)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(tConfig, 0, sizeof(Config));
|
|
|
|
|
2023-03-23 02:12:45 +00:00
|
|
|
CONFIG_REQUIRE("listen", JSON_ARRAY);
|
|
|
|
if (!ConfigParseListen(tConfig, JsonValueAsArray(value)))
|
|
|
|
{
|
|
|
|
goto error;
|
|
|
|
}
|
2023-03-22 17:17:30 +00:00
|
|
|
|
|
|
|
CONFIG_REQUIRE("serverName", JSON_STRING);
|
|
|
|
CONFIG_COPY_STRING(tConfig->serverName);
|
|
|
|
|
|
|
|
value = HashMapGet(config, "baseUrl");
|
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
CONFIG_COPY_STRING(tConfig->baseUrl);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-04-25 22:01:58 +00:00
|
|
|
size_t len = strlen(tConfig->serverName) + 10;
|
2023-04-25 22:13:28 +00:00
|
|
|
|
2023-04-25 22:01:58 +00:00
|
|
|
tConfig->baseUrl = Malloc(len);
|
2023-03-22 17:17:30 +00:00
|
|
|
if (!tConfig->baseUrl)
|
|
|
|
{
|
2023-04-19 00:33:38 +00:00
|
|
|
tConfig->err = "Error allocating memory for default config value 'baseUrl'.";
|
2023-03-22 17:17:30 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2023-04-25 22:01:58 +00:00
|
|
|
snprintf(tConfig->baseUrl, len, "https://%s", tConfig->serverName);
|
2023-03-22 17:17:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CONFIG_OPTIONAL_STRING(tConfig->identityServer, "identityServer", NULL);
|
|
|
|
|
|
|
|
value = HashMapGet(config, "runAs");
|
|
|
|
if (value && JsonValueType(value) != JSON_NULL)
|
|
|
|
{
|
|
|
|
if (JsonValueType(value) == JSON_OBJECT)
|
|
|
|
{
|
|
|
|
if (!ConfigParseRunAs(tConfig, JsonValueAsObject(value)))
|
|
|
|
{
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-04-19 00:33:38 +00:00
|
|
|
tConfig->err = "Config directive 'runAs' should be a JSON object that contains a 'uid' and 'gid'.";
|
2023-03-22 17:17:30 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CONFIG_OPTIONAL_INTEGER(tConfig->maxCache, "maxCache", 0);
|
|
|
|
|
|
|
|
CONFIG_REQUIRE("federation", JSON_BOOLEAN);
|
|
|
|
if (JsonValueAsBoolean(value))
|
|
|
|
{
|
|
|
|
tConfig->flags |= CONFIG_FEDERATION;
|
|
|
|
}
|
|
|
|
|
|
|
|
CONFIG_REQUIRE("registration", JSON_BOOLEAN);
|
|
|
|
if (JsonValueAsBoolean(value))
|
|
|
|
{
|
|
|
|
tConfig->flags |= CONFIG_REGISTRATION;
|
|
|
|
}
|
|
|
|
|
|
|
|
CONFIG_REQUIRE("log", JSON_OBJECT);
|
|
|
|
if (!ConfigParseLog(tConfig, JsonValueAsObject(value)))
|
|
|
|
{
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2023-04-19 00:33:38 +00:00
|
|
|
tConfig->ok = 1;
|
|
|
|
tConfig->err = NULL;
|
2023-03-22 17:17:30 +00:00
|
|
|
return tConfig;
|
|
|
|
|
|
|
|
error:
|
2023-04-19 00:33:38 +00:00
|
|
|
tConfig->ok = 0;
|
|
|
|
return tConfig;
|
2023-03-22 17:17:30 +00:00
|
|
|
}
|
|
|
|
|
2023-04-19 00:33:38 +00:00
|
|
|
int
|
2023-04-19 02:07:38 +00:00
|
|
|
ConfigExists(Db * db)
|
2023-03-22 17:17:30 +00:00
|
|
|
{
|
2023-04-19 00:33:38 +00:00
|
|
|
return DbExists(db, 1, "config");
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2023-04-19 02:07:38 +00:00
|
|
|
ConfigCreateDefault(Db * db)
|
2023-04-19 00:33:38 +00:00
|
|
|
{
|
|
|
|
DbRef *ref;
|
|
|
|
HashMap *json;
|
|
|
|
Array *listeners;
|
|
|
|
HashMap *listen;
|
|
|
|
|
|
|
|
char hostname[HOST_NAME_MAX + 1];
|
|
|
|
|
|
|
|
if (!db)
|
2023-03-22 17:17:30 +00:00
|
|
|
{
|
2023-04-19 00:33:38 +00:00
|
|
|
return 0;
|
2023-03-22 17:17:30 +00:00
|
|
|
}
|
|
|
|
|
2023-04-19 00:33:38 +00:00
|
|
|
ref = DbCreate(db, 1, "config");
|
|
|
|
if (!ref)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2023-03-22 17:17:30 +00:00
|
|
|
|
2023-04-19 00:33:38 +00:00
|
|
|
json = DbJson(ref);
|
2023-03-22 17:17:30 +00:00
|
|
|
|
2023-04-19 00:33:38 +00:00
|
|
|
JsonSet(json, JsonValueString("file"), 2, "log", "output");
|
2023-03-22 17:17:30 +00:00
|
|
|
|
2023-04-19 00:33:38 +00:00
|
|
|
listeners = ArrayCreate();
|
|
|
|
listen = HashMapCreate();
|
2023-08-13 03:11:40 +00:00
|
|
|
HashMapSet(listen, "port", JsonValueInteger(Int64Create(0, 8008)));
|
2023-04-19 00:33:38 +00:00
|
|
|
HashMapSet(listen, "tls", JsonValueBoolean(0));
|
|
|
|
ArrayAdd(listeners, JsonValueObject(listen));
|
|
|
|
HashMapSet(json, "listen", JsonValueArray(listeners));
|
|
|
|
|
|
|
|
if (gethostname(hostname, HOST_NAME_MAX + 1) < 0)
|
2023-03-23 02:12:45 +00:00
|
|
|
{
|
2023-05-29 23:53:17 +00:00
|
|
|
strncpy(hostname, "localhost", HOST_NAME_MAX);
|
2023-04-19 00:33:38 +00:00
|
|
|
}
|
|
|
|
HashMapSet(json, "serverName", JsonValueString(hostname));
|
2023-03-23 02:12:45 +00:00
|
|
|
|
2023-04-19 00:33:38 +00:00
|
|
|
HashMapSet(json, "federation", JsonValueBoolean(1));
|
|
|
|
HashMapSet(json, "registration", JsonValueBoolean(0));
|
2023-03-23 02:12:45 +00:00
|
|
|
|
2023-04-19 00:33:38 +00:00
|
|
|
return DbUnlock(db, ref);
|
|
|
|
}
|
2023-03-23 02:12:45 +00:00
|
|
|
|
2023-04-19 00:33:38 +00:00
|
|
|
Config *
|
2023-04-19 02:07:38 +00:00
|
|
|
ConfigLock(Db * db)
|
2023-04-19 00:33:38 +00:00
|
|
|
{
|
|
|
|
Config *config;
|
|
|
|
DbRef *ref = DbLock(db, 1, "config");
|
|
|
|
|
|
|
|
if (!ref)
|
|
|
|
{
|
|
|
|
return NULL;
|
2023-03-23 02:12:45 +00:00
|
|
|
}
|
|
|
|
|
2023-04-19 00:33:38 +00:00
|
|
|
config = ConfigParse(DbJson(ref));
|
|
|
|
if (config)
|
|
|
|
{
|
|
|
|
config->db = db;
|
|
|
|
config->ref = ref;
|
|
|
|
}
|
|
|
|
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2023-04-19 02:07:38 +00:00
|
|
|
ConfigUnlock(Config * config)
|
2023-04-19 00:33:38 +00:00
|
|
|
{
|
|
|
|
Db *db;
|
|
|
|
DbRef *dbRef;
|
|
|
|
|
|
|
|
if (!config)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
db = config->db;
|
|
|
|
dbRef = config->ref;
|
|
|
|
|
|
|
|
ConfigFree(config);
|
|
|
|
return DbUnlock(db, dbRef);
|
2023-03-22 17:17:30 +00:00
|
|
|
}
|