Telodendria/src/Config.c

234 lines
5.7 KiB
C
Raw Normal View History

2023-03-22 17:17:30 +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 <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&mdash;including all personal information I submit with it&mdash;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>
#include <Cytoplasm/Util.h>
2023-03-22 17:17:30 +00:00
#include <sys/types.h>
2023-03-22 17:17:30 +00:00
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <limits.h>
#include <grp.h>
#include <pwd.h>
2023-03-22 17:17:30 +00:00
#ifndef HOST_NAME_MAX
#define HOST_NAME_MAX _POSIX_HOST_NAME_MAX
#endif
void
ConfigParse(HashMap * config, Config *tConfig)
2023-03-22 17:17:30 +00:00
{
size_t i;
2023-03-22 17:17:30 +00:00
if (!config)
{
tConfig->ok = 0;
tConfig->err = "Invalid object given as config.";
return;
2023-03-22 17:17:30 +00:00
}
memset(tConfig, 0, sizeof(Config));
tConfig->maxCache = Int64Create(0, 0);
2023-03-22 17:17:30 +00:00
if (!ConfigFromJson(config, tConfig, &tConfig->err))
2023-03-22 17:17:30 +00:00
{
ConfigFree(tConfig);
goto error;
2023-03-22 17:17:30 +00:00
}
if (!tConfig->baseUrl)
2023-03-22 17:17:30 +00:00
{
size_t len = strlen(tConfig->serverName) + 10;
2023-04-25 22:13:28 +00:00
tConfig->baseUrl = Malloc(len);
2023-03-22 17:17:30 +00:00
if (!tConfig->baseUrl)
{
tConfig->err = "Couldn't allocate enough memory for 'baseUrl'.";
2023-03-22 17:17:30 +00:00
goto error;
}
snprintf(tConfig->baseUrl, len, "https://%s/", tConfig->serverName);
2023-03-22 17:17:30 +00:00
}
if (!tConfig->log.timestampFormat)
2023-03-22 17:17:30 +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
{
listener->maxConnections = Int64Create(0, 32);
2023-03-22 17:17:30 +00:00
}
if (Int64Eq(listener->threads, Int64Create(0, 0)))
2023-03-22 17:17:30 +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
}
}
tConfig->ok = 1;
tConfig->err = NULL;
return;
2023-03-22 17:17:30 +00:00
error:
tConfig->ok = 0;
return;
2023-03-22 17:17:30 +00:00
}
int
ConfigExists(Db * db)
2023-03-22 17:17:30 +00:00
{
return DbExists(db, 1, "config");
}
int
ConfigCreateDefault(Db * db)
{
Config config;
ConfigListener *listener;
HashMap *json;
JsonValue *val;
DbRef *ref;
size_t len;
memset(&config, 0, sizeof(Config));
2023-03-22 17:17:30 +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
/* 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
ArrayAdd(config.listen, listener);
/* 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)
{
ConfigFree(&config);
return 0;
}
DbJsonSet(ref, json);
DbUnlock(db, ref);
ConfigFree(&config);
JsonFree(json);
return 1;
}
void
ConfigLock(Db * db, Config *config)
{
DbRef *ref = DbLock(db, 1, "config");
if (!ref)
{
config->ok = 0;
config->err = "Couldn't lock configuration.";
}
ConfigParse(DbJson(ref), config);
if (config->ok)
{
config->db = db;
config->ref = ref;
}
}
int
ConfigUnlock(Config *config)
{
Db *db;
DbRef *dbRef;
if (!config->ok)
{
return 0;
}
db = config->db;
dbRef = config->ref;
ConfigFree(config);
config->ok = 0;
return DbUnlock(db, dbRef);
2023-03-22 17:17:30 +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;
}