[MOD] Remove ConfigFullyFree by using the stack.

This commit is contained in:
lda 2024-01-05 17:17:06 +01:00
parent 1d93b1c22c
commit da8a64534a
Signed by: lda
GPG key ID: 6898757653ABE3E6
15 changed files with 121 additions and 150 deletions

View file

@ -44,47 +44,48 @@
#define HOST_NAME_MAX _POSIX_HOST_NAME_MAX
#endif
Config *
Config
ConfigParse(HashMap * config)
{
Config *tConfig;
Config tConfig;
size_t i;
if (!config)
{
return NULL;
tConfig.ok = 0;
tConfig.err = "Invalid object given as config.";
return tConfig;
}
tConfig = Malloc(sizeof(Config));
memset(tConfig, 0, sizeof(Config));
memset(&tConfig, 0, sizeof(Config));
tConfig->maxCache = Int64Create(0, 0);
tConfig.maxCache = Int64Create(0, 0);
if (!ConfigFromJson(config, tConfig, &tConfig->err))
if (!ConfigFromJson(config, &tConfig, &tConfig.err))
{
ConfigFree(tConfig);
ConfigFree(&tConfig);
goto error;
}
if (!tConfig->baseUrl)
if (!tConfig.baseUrl)
{
size_t len = strlen(tConfig->serverName) + 10;
size_t len = strlen(tConfig.serverName) + 10;
tConfig->baseUrl = Malloc(len);
if (!tConfig->baseUrl)
tConfig.baseUrl = Malloc(len);
if (!tConfig.baseUrl)
{
tConfig->err = "Couldn't allocate enough memory for 'baseUrl'.";
tConfig.err = "Couldn't allocate enough memory for 'baseUrl'.";
goto error;
}
snprintf(tConfig->baseUrl, len, "https://%s/", tConfig->serverName);
snprintf(tConfig.baseUrl, len, "https://%s/", tConfig.serverName);
}
if (!tConfig->log.timestampFormat)
if (!tConfig.log.timestampFormat)
{
tConfig->log.timestampFormat = StrDuplicate("default");
tConfig.log.timestampFormat = StrDuplicate("default");
}
for (i = 0; i < ArraySize(tConfig->listen); i++)
for (i = 0; i < ArraySize(tConfig.listen); i++)
{
ConfigListener *listener = ArrayGet(tConfig->listen, i);
ConfigListener *listener = ArrayGet(tConfig.listen, i);
if (Int64Eq(listener->maxConnections, Int64Create(0, 0)))
{
listener->maxConnections = Int64Create(0, 32);
@ -98,12 +99,12 @@ ConfigParse(HashMap * config)
listener->port = Int64Create(0, 8008);
}
}
tConfig->ok = 1;
tConfig->err = NULL;
tConfig.ok = 1;
tConfig.err = NULL;
return tConfig;
error:
tConfig->ok = 0;
tConfig.ok = 0;
return tConfig;
}
@ -175,53 +176,44 @@ ConfigCreateDefault(Db * db)
return 1;
}
Config *
Config
ConfigLock(Db * db)
{
Config *config;
Config config;
DbRef *ref = DbLock(db, 1, "config");
if (!ref)
{
return NULL;
config.ok = 0;
config.err = "Couldn't lock configuration.";
}
config = ConfigParse(DbJson(ref));
if (config)
if (config.ok)
{
config->db = db;
config->ref = ref;
config.db = db;
config.ref = ref;
}
return config;
}
void
ConfigFullyFree(Config * config)
{
if (!config)
{
return;
}
ConfigFree(config);
Free(config);
}
int
ConfigUnlock(Config * config)
ConfigUnlock(Config config)
{
Db *db;
DbRef *dbRef;
if (!config)
if (!config.ok)
{
return 0;
}
db = config->db;
dbRef = config->ref;
db = config.db;
dbRef = config.ref;
ConfigFree(&config);
ConfigFullyFree(config);
return DbUnlock(db, dbRef);
}
int

View file

@ -102,7 +102,7 @@ Main(Array * args)
char *dbPath;
/* Program configuration */
Config *tConfig;
Config tConfig;
Stream *logFile;
Stream *pidFile = NULL;
@ -135,7 +135,7 @@ start:
exit = EXIT_SUCCESS;
flags = 0;
dbPath = NULL;
tConfig = NULL;
/*tConfig = NULL;*/
logFile = NULL;
userInfo = NULL;
groupInfo = NULL;
@ -265,27 +265,19 @@ start:
Log(LOG_NOTICE, "Loading configuration...");
tConfig = ConfigLock(matrixArgs.db);
if (!tConfig)
if (!tConfig.ok)
{
Log(LOG_ERR, "Error locking the configuration.");
Log(LOG_ERR, "The configuration object is corrupted or otherwise invalid.");
Log(LOG_ERR, "Please restore from a backup.");
exit = EXIT_FAILURE;
goto finish;
}
else if (!tConfig->ok)
{
Log(LOG_ERR, tConfig->err);
Log(LOG_ERR, tConfig.err);
exit = EXIT_FAILURE;
goto finish;
}
if (!tConfig->log.timestampFormat || !StrEquals(tConfig->log.timestampFormat, "default"))
if (!tConfig.log.timestampFormat || !StrEquals(tConfig.log.timestampFormat, "default"))
{
LogConfigTimeStampFormatSet(LogConfigGlobal(), tConfig->log.timestampFormat);
LogConfigTimeStampFormatSet(LogConfigGlobal(), tConfig.log.timestampFormat);
}
if (tConfig->log.color)
if (tConfig.log.color)
{
LogConfigFlagSet(LogConfigGlobal(), LOG_FLAG_COLOR);
}
@ -298,9 +290,9 @@ start:
LogConfigGlobal(),
flags & ARG_VERBOSE ?
LOG_DEBUG :
ConfigLogLevelToSyslog(tConfig->log.level));
ConfigLogLevelToSyslog(tConfig.log.level));
if (tConfig->log.output == CONFIG_LOG_OUTPUT_FILE)
if (tConfig.log.output == CONFIG_LOG_OUTPUT_FILE)
{
logFile = StreamOpen("telodendria.log", "a");
@ -308,18 +300,18 @@ start:
{
Log(LOG_ERR, "Unable to open log file for appending.");
exit = EXIT_FAILURE;
tConfig->log.output = CONFIG_LOG_OUTPUT_STDOUT;
tConfig.log.output = CONFIG_LOG_OUTPUT_STDOUT;
goto finish;
}
Log(LOG_INFO, "Logging to the log file. Check there for all future messages.");
LogConfigOutputSet(LogConfigGlobal(), logFile);
}
else if (tConfig->log.output == CONFIG_LOG_OUTPUT_STDOUT)
else if (tConfig.log.output == CONFIG_LOG_OUTPUT_STDOUT)
{
Log(LOG_DEBUG, "Already logging to standard output.");
}
else if (tConfig->log.output == CONFIG_LOG_OUTPUT_SYSLOG)
else if (tConfig.log.output == CONFIG_LOG_OUTPUT_SYSLOG)
{
Log(LOG_INFO, "Logging to the syslog. Check there for all future messages.");
LogConfigFlagSet(LogConfigGlobal(), LOG_FLAG_SYSLOG);
@ -338,30 +330,30 @@ start:
Free(token);
}
if (tConfig->pid)
if (tConfig.pid)
{
pidFile = StreamOpen(tConfig->pid, "w+");
pidFile = StreamOpen(tConfig.pid, "w+");
if (!pidFile)
{
char *msg = "Couldn't lock PID file at '%s'";
Log(LOG_ERR, msg, tConfig->pid);
Log(LOG_ERR, msg, tConfig.pid);
exit = EXIT_FAILURE;
goto finish;
}
pidPath = StrDuplicate(tConfig->pid);
pidPath = StrDuplicate(tConfig.pid);
StreamPrintf(pidFile, "%ld", (long) getpid());
StreamClose(pidFile);
}
Log(LOG_DEBUG, "Configuration:");
LogConfigIndent(LogConfigGlobal());
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");
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");
LogConfigUnindent(LogConfigGlobal());
httpServers = ArrayCreate();
@ -373,9 +365,9 @@ start:
}
/* Bind servers before possibly dropping permissions. */
for (i = 0; i < ArraySize(tConfig->listen); i++)
for (i = 0; i < ArraySize(tConfig.listen); i++)
{
ConfigListener *serverCfg = ArrayGet(tConfig->listen, i);
ConfigListener *serverCfg = ArrayGet(tConfig.listen, i);
HttpServerConfig args;
@ -438,10 +430,10 @@ start:
Log(LOG_DEBUG, "Running as uid:gid: %d:%d.", getuid(), getgid());
if (tConfig->runAs.uid && tConfig->runAs.gid)
if (tConfig.runAs.uid && tConfig.runAs.gid)
{
userInfo = getpwnam(tConfig->runAs.uid);
groupInfo = getgrnam(tConfig->runAs.gid);
userInfo = getpwnam(tConfig.runAs.uid);
groupInfo = getgrnam(tConfig.runAs.gid);
if (!userInfo || !groupInfo)
{
@ -471,7 +463,7 @@ start:
}
else
{
Log(LOG_DEBUG, "Set uid/gid to %s:%s.", tConfig->runAs.uid, tConfig->runAs.gid);
Log(LOG_DEBUG, "Set uid/gid to %s:%s.", tConfig.runAs.uid, tConfig.runAs.gid);
}
}
else
@ -483,7 +475,7 @@ start:
}
else
{
if (tConfig->runAs.uid && tConfig->runAs.gid)
if (tConfig.runAs.uid && tConfig.runAs.gid)
{
if (getuid() != userInfo->pw_uid || getgid() != groupInfo->gr_gid)
{
@ -496,17 +488,18 @@ start:
}
}
if (!tConfig->maxCache)
if (!tConfig.maxCache)
{
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.");
}
DbMaxCacheSet(matrixArgs.db, tConfig->maxCache);
DbMaxCacheSet(matrixArgs.db, tConfig.maxCache);
ConfigUnlock(tConfig);
tConfig = NULL;
tConfig.ok = 0;
cron = CronCreate(60 * 1000); /* 1-minute tick */
if (!cron)

View file

@ -67,9 +67,9 @@ ROUTE_IMPL(RouteChangePwd, path, argp)
char *msg;
Config *config = ConfigLock(db);
Config config = ConfigLock(db);
if (!config)
if (!config.ok)
{
Log(LOG_ERR, "Password endpoint failed to lock configuration.");
HttpResponseStatus(args->context, HTTP_INTERNAL_SERVER_ERROR);

View file

@ -36,10 +36,10 @@ ROUTE_IMPL(RouteConfig, path, argp)
char *msg;
User *user = NULL;
Config *config = NULL;
Config config;
HashMap *request = NULL;
Config *newConf;
Config newConf;
HashMap *newJson = NULL;
(void) path;
@ -67,7 +67,7 @@ ROUTE_IMPL(RouteConfig, path, argp)
}
config = ConfigLock(args->matrixArgs->db);
if (!config)
if (!config.ok)
{
msg = "Internal server error while locking configuration.";
Log(LOG_ERR, "Config endpoint failed to lock configuration.");
@ -79,7 +79,7 @@ ROUTE_IMPL(RouteConfig, path, argp)
switch (HttpRequestMethodGet(args->context))
{
case HTTP_GET:
response = JsonDuplicate(DbJson(config->ref));
response = JsonDuplicate(DbJson(config.ref));
break;
case HTTP_POST:
request = JsonDecode(HttpServerStream(args->context));
@ -91,17 +91,9 @@ ROUTE_IMPL(RouteConfig, path, argp)
}
newConf = ConfigParse(request);
if (!newConf)
if (newConf.ok)
{
msg = "Internal server error while parsing config.";
HttpResponseStatus(args->context, HTTP_INTERNAL_SERVER_ERROR);
response = MatrixErrorCreate(M_UNKNOWN, msg);
break;
}
if (newConf->ok)
{
if (DbJsonSet(config->ref, request))
if (DbJsonSet(config.ref, request))
{
response = HashMapCreate();
/*
@ -120,10 +112,10 @@ ROUTE_IMPL(RouteConfig, path, argp)
else
{
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_BAD_JSON, newConf->err);
response = MatrixErrorCreate(M_BAD_JSON, newConf.err);
}
ConfigFullyFree(newConf);
ConfigFree(&newConf);
JsonFree(request);
break;
case HTTP_PUT:
@ -135,12 +127,12 @@ ROUTE_IMPL(RouteConfig, path, argp)
break;
}
newJson = JsonDuplicate(DbJson(config->ref));
newJson = JsonDuplicate(DbJson(config.ref));
JsonMerge(newJson, request);
newConf = ConfigParse(newJson);
if (!newConf)
if (!newConf.ok)
{
msg = "Internal server error while parsing config.";
HttpResponseStatus(args->context, HTTP_INTERNAL_SERVER_ERROR);
@ -148,9 +140,9 @@ ROUTE_IMPL(RouteConfig, path, argp)
break;
}
if (newConf->ok)
if (newConf.ok)
{
if (DbJsonSet(config->ref, newJson))
if (DbJsonSet(config.ref, newJson))
{
response = HashMapCreate();
/*
@ -169,10 +161,10 @@ ROUTE_IMPL(RouteConfig, path, argp)
else
{
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_BAD_JSON, newConf->err);
response = MatrixErrorCreate(M_BAD_JSON, newConf.err);
}
ConfigFullyFree(newConf);
ConfigFree(&newConf);
JsonFree(request);
JsonFree(newJson);
break;

View file

@ -45,13 +45,13 @@ ROUTE_IMPL(RouteDeactivate, path, argp)
Db *db = args->matrixArgs->db;
User *user = NULL;
Config *config = ConfigLock(db);
Config config = ConfigLock(db);
char *msg;
(void) path;
if (!config)
if (!config.ok)
{
Log(LOG_ERR, "Deactivate endpoint failed to lock configuration.");
HttpResponseStatus(args->context, HTTP_INTERNAL_SERVER_ERROR);

View file

@ -39,14 +39,14 @@ GetServerName(Db * db)
{
char *name;
Config *config = ConfigLock(db);
Config config = ConfigLock(db);
if (!config)
if (!config.ok)
{
return NULL;
}
name = StrDuplicate(config->serverName);
name = StrDuplicate(config.serverName);
ConfigUnlock(config);

View file

@ -64,9 +64,9 @@ ROUTE_IMPL(RouteLogin, path, argp)
char *msg;
Config *config = ConfigLock(db);
Config config = ConfigLock(db);
if (!config)
if (!config.ok)
{
Log(LOG_ERR, "Login endpoint failed to lock configuration.");
HttpResponseStatus(args->context, HTTP_INTERNAL_SERVER_ERROR);
@ -149,7 +149,7 @@ ROUTE_IMPL(RouteLogin, path, argp)
}
userId = UserIdParse(userIdentifier.user, config->serverName);
userId = UserIdParse(userIdentifier.user, config.serverName);
if (!userId)
{
msg = "Invalid user ID.";
@ -158,7 +158,7 @@ ROUTE_IMPL(RouteLogin, path, argp)
break;
}
if (!StrEquals(userId->server, config->serverName)
if (!StrEquals(userId->server, config.serverName)
|| !UserExists(db, userId->localpart))
{
msg = "Unknown user ID.";
@ -221,13 +221,13 @@ ROUTE_IMPL(RouteLogin, path, argp)
}
fullUsername = StrConcat(4, "@", UserGetName(user), ":",
config->serverName);
config.serverName);
HashMapSet(response, "user_id", JsonValueString(fullUsername));
Free(fullUsername);
HashMapSet(response, "well_known",
JsonValueObject(
MatrixClientWellKnown(config->baseUrl, config->identityServer)));
MatrixClientWellKnown(config.baseUrl, config.identityServer)));
UserAccessTokenFree(loginInfo->accessToken);
Free(loginInfo->refreshToken);

View file

@ -72,7 +72,7 @@ ROUTE_IMPL(RouteRegister, path, argp)
char *session;
DbRef *sessionRef;
Config *config = ConfigLock(db);
Config config = ConfigLock(db);
regReq.username = NULL;
regReq.password = NULL;
@ -84,7 +84,7 @@ ROUTE_IMPL(RouteRegister, path, argp)
if (!config)
if (!config.ok)
{
msg = "Internal server error while locking configuration.";
Log(LOG_ERR, "Registration endpoint failed to lock configuration.");
@ -117,7 +117,7 @@ ROUTE_IMPL(RouteRegister, path, argp)
if (regReq.username)
{
if (!UserValidate(regReq.username, config->serverName))
if (!UserValidate(regReq.username, config.serverName))
{
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_INVALID_USERNAME, NULL);
@ -135,7 +135,7 @@ ROUTE_IMPL(RouteRegister, path, argp)
uiaFlows = ArrayCreate();
ArrayAdd(uiaFlows, RouteRegisterRegFlow());
if (config->registration)
if (config.registration)
{
ArrayAdd(uiaFlows, UiaDummyFlow());
}
@ -182,7 +182,7 @@ ROUTE_IMPL(RouteRegister, path, argp)
response = HashMapCreate();
fullUsername = StrConcat(4,
"@", UserGetName(user), ":", config->serverName);
"@", UserGetName(user), ":", config.serverName);
HashMapSet(response, "user_id", JsonValueString(fullUsername));
Free(fullUsername);
@ -259,7 +259,7 @@ finish:
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_MISSING_PARAM, msg);
}
else if (!UserValidate(username, config->serverName))
else if (!UserValidate(username, config.serverName))
{
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_INVALID_USERNAME, NULL);

View file

@ -51,12 +51,12 @@ ROUTE_IMPL(RouteUiaFallback, path, argp)
HashMap *request;
HashMap *response;
int uiaResult;
Config *config;
Config config;
Array *flows;
Array *flow;
config = ConfigLock(args->matrixArgs->db);
if (!config)
if (!config.ok)
{
msg = "Internal server error: failed to lock configuration.";
Log(LOG_ERR, "UIA fallback failed to lock configuration.");

View file

@ -50,16 +50,16 @@ ROUTE_IMPL(RouteUserProfile, path, argp)
char *msg;
Config *config = ConfigLock(db);
Config config = ConfigLock(db);
if (!config)
if (!config.ok)
{
Log(LOG_ERR, "User profile endpoint failed to lock configuration.");
HttpResponseStatus(args->context, HTTP_INTERNAL_SERVER_ERROR);
return MatrixErrorCreate(M_UNKNOWN, NULL);
}
serverName = config->serverName;
serverName = config.serverName;
username = ArrayGet(path, 0);
userId = UserIdParse(username, serverName);

View file

@ -35,11 +35,11 @@ ROUTE_IMPL(RouteWellKnown, path, argp)
RouteArgs *args = argp;
HashMap *response;
Config *config = ConfigLock(args->matrixArgs->db);
Config config = ConfigLock(args->matrixArgs->db);
char *msg;
if (!config)
if (!config.ok)
{
Log(LOG_ERR, "Well-known endpoint failed to lock configuration.");
msg = "Internal server error: couldn't lock database.";
@ -49,7 +49,7 @@ ROUTE_IMPL(RouteWellKnown, path, argp)
if (StrEquals(ArrayGet(path, 0), "client"))
{
response = MatrixClientWellKnown(config->baseUrl, config->identityServer);
response = MatrixClientWellKnown(config.baseUrl, config.identityServer);
}
else
{

View file

@ -44,9 +44,9 @@ ROUTE_IMPL(RouteWhoami, path, argp)
char *deviceID;
char *msg;
Config *config = ConfigLock(db);
Config config = ConfigLock(db);
if (!config)
if (!config.ok)
{
msg = "Internal server error: couldn't lock database.";
Log(LOG_ERR, "Who am I endpoint failed to lock configuration.");
@ -75,7 +75,7 @@ ROUTE_IMPL(RouteWhoami, path, argp)
response = HashMapCreate();
userID = StrConcat(4, "@", UserGetName(user), ":", config->serverName);
userID = StrConcat(4, "@", UserGetName(user), ":", config.serverName);
deviceID = StrDuplicate(UserGetDeviceId(user));
UserUnlock(user);
@ -88,5 +88,6 @@ ROUTE_IMPL(RouteWhoami, path, argp)
finish:
ConfigUnlock(config);
ConfigFree(&config);
return response;
}

View file

@ -205,7 +205,7 @@ UiaStageBuild(char *type, HashMap * params)
int
UiaComplete(Array * flows, HttpServerContext * context, Db * db,
HashMap * request, HashMap ** response, Config * config)
HashMap * request, HashMap ** response, Config config)
{
JsonValue *val;
HashMap *auth;
@ -362,10 +362,10 @@ UiaComplete(Array * flows, HttpServerContext * context, Db * db,
type = JsonValueAsString(HashMapGet(identifier, "type"));
userId = UserIdParse(JsonValueAsString(HashMapGet(identifier, "user")),
config->serverName);
config.serverName);
if (!type || !StrEquals(type, "m.id.user")
|| !userId || !StrEquals(userId->server, config->serverName))
|| !userId || !StrEquals(userId->server, config.serverName))
{
HttpResponseStatus(context, HTTP_UNAUTHORIZED);
ret = BuildResponse(flows, db, response, session, dbRef);

View file

@ -64,14 +64,7 @@
* set the ok flag to 0. The caller should always check the ok flag,
* and if there is an error, it should display the error to the user.
*/
extern Config * ConfigParse(HashMap *);
/**
* Free all the values inside of the given configuration structure,
* as well as the structure itself, such that it is completely invalid
* when this function returns.
*/
extern void ConfigFullyFree(Config *);
extern Config ConfigParse(HashMap *);
/**
* Check whether or not the configuration exists in the database,
@ -94,7 +87,7 @@ extern int ConfigCreateDefault(Db *);
* The return value of this function is the same as
* .Fn ConfigParse .
*/
extern Config * ConfigLock(Db *);
extern Config ConfigLock(Db *);
/**
* Unlock the specified configuration, returning it back to the
@ -102,7 +95,7 @@ extern Config * ConfigLock(Db *);
* this config object, so values that should be retained after this is
* called should be duplicated as necessary.
*/
extern int ConfigUnlock(Config *);
extern int ConfigUnlock(Config);
/**
* Converts a ConfigLogLevel into a valid syslog level.

View file

@ -132,7 +132,7 @@ extern void UiaCleanup(MatrixHttpHandlerArgs *);
* the caller proceed with its logic.
*/
extern int
UiaComplete(Array *, HttpServerContext *, Db *, HashMap *, HashMap **, Config *);
UiaComplete(Array *, HttpServerContext *, Db *, HashMap *, HashMap **, Config);
/**
* Free an array of flows, as described above. Even though the caller