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.
|
|
|
|
*/
|
2024-01-06 02:00:27 +00:00
|
|
|
#include <Schema/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/Log.h>
|
|
|
|
#include <Cytoplasm/Int64.h>
|
2024-01-06 02:00:27 +00:00
|
|
|
#include <Cytoplasm/Util.h>
|
2023-03-22 17:17:30 +00:00
|
|
|
|
2024-01-06 02:00:27 +00:00
|
|
|
#include <sys/types.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>
|
2024-01-06 02:00:27 +00:00
|
|
|
#include <grp.h>
|
|
|
|
#include <pwd.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-04-19 00:33:38 +00:00
|
|
|
void
|
2024-01-06 02:00:27 +00:00
|
|
|
ConfigParse(HashMap * config, Config *tConfig)
|
2023-03-22 17:17:30 +00:00
|
|
|
{
|
2024-01-06 02:00:27 +00:00
|
|
|
size_t i;
|
2023-03-22 17:17:30 +00:00
|
|
|
|
|
|
|
if (!config)
|
|
|
|
{
|
2024-01-06 02:00:27 +00:00
|
|
|
tConfig->ok = 0;
|
|
|
|
tConfig->err = "Invalid object given as config.";
|
|
|
|
return;
|
2023-03-22 17:17:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
memset(tConfig, 0, sizeof(Config));
|
|
|
|
|
2024-01-06 02:00:27 +00:00
|
|
|
tConfig->maxCache = Int64Create(0, 0);
|
2023-03-22 17:17:30 +00:00
|
|
|
|
2024-01-06 02:00:27 +00:00
|
|
|
if (!ConfigFromJson(config, tConfig, &tConfig->err))
|
2023-03-22 17:17:30 +00:00
|
|
|
{
|
2024-01-06 02:00:27 +00:00
|
|
|
ConfigFree(tConfig);
|
|
|
|
goto error;
|
2023-03-22 17:17:30 +00:00
|
|
|
}
|
2024-01-06 02:00:27 +00:00
|
|
|
if (!tConfig->baseUrl)
|
2023-03-22 17:17:30 +00:00
|
|
|
{
|
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)
|
|
|
|
{
|
2024-01-06 02:00:27 +00:00
|
|
|
tConfig->err = "Couldn't allocate enough memory for 'baseUrl'.";
|
2023-03-22 17:17:30 +00:00
|
|
|
goto error;
|
|
|
|
}
|
2024-01-06 02:00:27 +00:00
|
|
|
snprintf(tConfig->baseUrl, len, "https://%s/", tConfig->serverName);
|
2023-03-22 17:17:30 +00:00
|
|
|
}
|
2024-01-06 02:00:27 +00:00
|
|
|
if (!tConfig->log.timestampFormat)
|
2023-03-22 17:17:30 +00:00
|
|
|
{
|
2024-01-06 02:00:27 +00:00
|
|
|
tConfig->log.timestampFormat = StrDuplicate("default");
|
|
|
|
}
|
|
|
|
for (i = 0; i < ArraySize(tConfig->listen); i++)
|
|
|
|
{
|
|
|
|
ConfigListener *listener = ArrayGet(tConfig->listen, i);
|
|
|
|
if (Int64Eq(listener->maxConnections, Int64Create(0, 0)))
|
2023-03-22 17:17:30 +00:00
|
|
|
{
|
2024-01-06 02:00:27 +00:00
|
|
|
listener->maxConnections = Int64Create(0, 32);
|
2023-03-22 17:17:30 +00:00
|
|
|
}
|
2024-01-06 02:00:27 +00:00
|
|
|
if (Int64Eq(listener->threads, Int64Create(0, 0)))
|
2023-03-22 17:17:30 +00:00
|
|
|
{
|
2024-01-06 02:00:27 +00:00
|
|
|
listener->threads = Int64Create(0, 4);
|
|
|
|
}
|
|
|
|
if (Int64Eq(listener->port, Int64Create(0, 0)))
|
|
|
|
{
|
|
|
|
listener->port = Int64Create(0, 8008);
|
2023-03-22 17:17:30 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-19 00:33:38 +00:00
|
|
|
tConfig->ok = 1;
|
|
|
|
tConfig->err = NULL;
|
2024-01-06 02:00:27 +00:00
|
|
|
return;
|
2023-03-22 17:17:30 +00:00
|
|
|
|
|
|
|
error:
|
2023-04-19 00:33:38 +00:00
|
|
|
tConfig->ok = 0;
|
2024-01-06 02:00:27 +00:00
|
|
|
return;
|
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
|
|
|
{
|
2024-01-06 02:00:27 +00:00
|
|
|
Config config;
|
|
|
|
ConfigListener *listener;
|
|
|
|
|
2023-04-19 00:33:38 +00:00
|
|
|
HashMap *json;
|
2024-01-06 02:00:27 +00:00
|
|
|
JsonValue *val;
|
|
|
|
|
|
|
|
DbRef *ref;
|
2023-04-19 00:33:38 +00:00
|
|
|
|
2024-01-06 02:00:27 +00:00
|
|
|
size_t len;
|
2023-04-19 00:33:38 +00:00
|
|
|
|
2024-01-06 02:00:27 +00:00
|
|
|
memset(&config, 0, sizeof(Config));
|
2023-03-22 17:17:30 +00:00
|
|
|
|
|
|
|
|
2024-01-06 02:00:27 +00:00
|
|
|
config.log.output = CONFIG_LOG_OUTPUT_FILE;
|
|
|
|
|
|
|
|
config.runAs.gid = StrDuplicate(getgrgid(getgid())->gr_name);
|
|
|
|
config.runAs.uid = StrDuplicate(getpwuid(getuid())->pw_name);
|
|
|
|
|
|
|
|
config.registration = 0;
|
|
|
|
config.federation = 1;
|
|
|
|
|
|
|
|
/* Create serverName and baseUrl. */
|
|
|
|
config.serverName = Malloc(HOST_NAME_MAX + 1);
|
|
|
|
memset(config.serverName, 0, HOST_NAME_MAX + 1);
|
|
|
|
gethostname(config.serverName, HOST_NAME_MAX);
|
|
|
|
len = strlen(config.serverName) + 10;
|
|
|
|
config.baseUrl = Malloc(len);
|
|
|
|
snprintf(config.baseUrl, len, "https://%s/", config.serverName);
|
2023-03-22 17:17:30 +00:00
|
|
|
|
2024-01-06 02:00:27 +00:00
|
|
|
/* Add simple listener without TLS. */
|
|
|
|
config.listen = ArrayCreate();
|
|
|
|
listener = Malloc(sizeof(ConfigListener));
|
|
|
|
listener->maxConnections = Int64Create(0, 32);
|
|
|
|
listener->port = Int64Create(0, 8008);
|
|
|
|
listener->threads = Int64Create(0, 4);
|
2023-03-22 17:17:30 +00:00
|
|
|
|
2024-01-06 02:00:27 +00:00
|
|
|
ArrayAdd(config.listen, listener);
|
2023-04-19 00:33:38 +00:00
|
|
|
|
2024-01-06 02:00:27 +00:00
|
|
|
/* Write it all out to the configuration file. */
|
|
|
|
json = ConfigToJson(&config);
|
|
|
|
val = JsonGet(json, 1, "listen");
|
|
|
|
val = ArrayGet(JsonValueAsArray(val), 0);
|
|
|
|
JsonValueFree(HashMapDelete(JsonValueAsObject(val), "tls"));
|
|
|
|
|
|
|
|
ref = DbCreate(db, 1, "config");
|
|
|
|
if (!ref)
|
2023-03-23 02:12:45 +00:00
|
|
|
{
|
2024-01-06 02:00:27 +00:00
|
|
|
ConfigFree(&config);
|
|
|
|
return 0;
|
2023-04-19 00:33:38 +00:00
|
|
|
}
|
2024-01-06 02:00:27 +00:00
|
|
|
DbJsonSet(ref, json);
|
|
|
|
DbUnlock(db, ref);
|
2023-03-23 02:12:45 +00:00
|
|
|
|
2024-01-06 02:00:27 +00:00
|
|
|
ConfigFree(&config);
|
|
|
|
JsonFree(json);
|
2023-03-23 02:12:45 +00:00
|
|
|
|
2024-01-06 02:00:27 +00:00
|
|
|
return 1;
|
2023-04-19 00:33:38 +00:00
|
|
|
}
|
2023-03-23 02:12:45 +00:00
|
|
|
|
2024-01-06 02:00:27 +00:00
|
|
|
void
|
|
|
|
ConfigLock(Db * db, Config *config)
|
2023-04-19 00:33:38 +00:00
|
|
|
{
|
|
|
|
DbRef *ref = DbLock(db, 1, "config");
|
|
|
|
|
|
|
|
if (!ref)
|
|
|
|
{
|
2024-01-06 02:00:27 +00:00
|
|
|
config->ok = 0;
|
|
|
|
config->err = "Couldn't lock configuration.";
|
2023-03-23 02:12:45 +00:00
|
|
|
}
|
|
|
|
|
2024-01-06 02:00:27 +00:00
|
|
|
ConfigParse(DbJson(ref), config);
|
|
|
|
if (config->ok)
|
2023-04-19 00:33:38 +00:00
|
|
|
{
|
|
|
|
config->db = db;
|
|
|
|
config->ref = ref;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2024-01-06 02:00:27 +00:00
|
|
|
ConfigUnlock(Config *config)
|
2023-04-19 00:33:38 +00:00
|
|
|
{
|
|
|
|
Db *db;
|
|
|
|
DbRef *dbRef;
|
|
|
|
|
2024-01-06 02:00:27 +00:00
|
|
|
if (!config->ok)
|
2023-04-19 00:33:38 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
db = config->db;
|
|
|
|
dbRef = config->ref;
|
|
|
|
|
|
|
|
ConfigFree(config);
|
2024-01-06 02:00:27 +00:00
|
|
|
config->ok = 0;
|
|
|
|
|
2023-04-19 00:33:38 +00:00
|
|
|
return DbUnlock(db, dbRef);
|
2023-03-22 17:17:30 +00:00
|
|
|
}
|
2024-01-06 02:00:27 +00:00
|
|
|
int
|
|
|
|
ConfigLogLevelToSyslog(ConfigLogLevel level)
|
|
|
|
{
|
|
|
|
switch (level)
|
|
|
|
{
|
|
|
|
case CONFIG_LOG_LEVEL_NOTICE:
|
|
|
|
return LOG_NOTICE;
|
|
|
|
case CONFIG_LOG_LEVEL_ERROR:
|
|
|
|
return LOG_ERR;
|
|
|
|
case CONFIG_LOG_LEVEL_MESSAGE:
|
|
|
|
return LOG_INFO;
|
|
|
|
case CONFIG_LOG_LEVEL_DEBUG:
|
|
|
|
return LOG_DEBUG;
|
|
|
|
case CONFIG_LOG_LEVEL_WARNING:
|
|
|
|
return LOG_WARNING;
|
|
|
|
}
|
|
|
|
return LOG_INFO;
|
|
|
|
}
|