157 γραμμές
4.6 KiB
C++
157 γραμμές
4.6 KiB
C++
/* Copyright (c) 2018 Albert S.
|
|
|
|
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 substantial 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 <iostream>
|
|
#include <fstream>
|
|
#include <exception>
|
|
#include <signal.h>
|
|
#include <unistd.h>
|
|
#include <sys/types.h>
|
|
#include <filesystem>
|
|
#include "gateway/gatewayinterface.h"
|
|
#include "gateway/gatewayfactory.h"
|
|
#include "handlers/handlerfactory.h"
|
|
#include "database/databasefactory.h"
|
|
#include "config.h"
|
|
#include "template.h"
|
|
#include "session.h"
|
|
#include "logger.h"
|
|
#include "urlprovider.h"
|
|
#include "requestworker.h"
|
|
#include "cache/fscache.h"
|
|
#include "sandbox/sandboxfactory.h"
|
|
void sigterm_handler(int arg)
|
|
{
|
|
// TODO: proper shutdown.
|
|
exit(EXIT_SUCCESS);
|
|
}
|
|
|
|
void setup_signal_handlers()
|
|
{
|
|
struct sigaction sigtermaction;
|
|
sigtermaction.sa_handler = &sigterm_handler;
|
|
|
|
int ret = sigaction(SIGTERM, &sigtermaction, NULL);
|
|
if(ret == -1)
|
|
{
|
|
perror("sigaction");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
}
|
|
|
|
std::unique_ptr<ICache> createCache(const ConfigVariableResolver &resolver)
|
|
{
|
|
|
|
std::string path = resolver.getConfig("cache_fs_dir");
|
|
|
|
return std::make_unique<FsCache>(path);
|
|
}
|
|
int main(int argc, char **argv)
|
|
{
|
|
if(geteuid() == 0)
|
|
{
|
|
std::cerr << "Do not run this as root!" << std::endl;
|
|
return 1;
|
|
}
|
|
auto sandbox = createSandbox();
|
|
// TODO: do we want to keep it mandatory or configurable?
|
|
if(!sandbox->supported())
|
|
{
|
|
Logger::error() << "Sandbox is not supported, exiting";
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
if(argc < 2)
|
|
{
|
|
std::cerr << "no path to config file provided" << std::endl;
|
|
return 1;
|
|
}
|
|
std::string configpath = std::filesystem::absolute(argv[1]).string();
|
|
if(!sandbox->enableForInit())
|
|
{
|
|
Logger::error() << "Sandboxing for init mode could not be activated.";
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
try
|
|
{
|
|
ConfigReader configreader(configpath);
|
|
Config config = configreader.readConfig();
|
|
|
|
// TODO: config.connectiontring only works as long as we only support sqlite of course
|
|
if(!sandbox->enablePreWorker({
|
|
config.configVarResolver.getConfig("cache_fs_dir"),
|
|
config.templatepath,
|
|
std::filesystem::path(config.logfile).parent_path(),
|
|
std::filesystem::path(config.connectionstring).parent_path(),
|
|
}))
|
|
{
|
|
Logger::error() << "Sandboxing for pre worker stage could not be activated.";
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
setup_signal_handlers();
|
|
|
|
std::fstream logstream;
|
|
logstream.open(config.logfile, std::fstream::out | std::fstream::app);
|
|
Logger::setStream(&logstream);
|
|
|
|
auto database = createDatabase(config);
|
|
|
|
// TODO: quite ugly, anon-handling must be rethought
|
|
auto userdao = database->createUserDao();
|
|
std::optional<User> anon = userdao->find(config.handlersConfig.anon_username);
|
|
if(!anon)
|
|
{
|
|
throw std::runtime_error("No such anon user in database");
|
|
}
|
|
if(anon->enabled)
|
|
{
|
|
throw std::runtime_error("Anon user cannot be enabled");
|
|
}
|
|
anon->permissions = config.handlersConfig.anon_permissions;
|
|
userdao->save(anon.value());
|
|
User::setAnon(anon.value());
|
|
|
|
Template siteTemplate{config.templateprefix, config.templatepath, config.urls, config.configVarResolver};
|
|
UrlProvider urlProvider{config.urls};
|
|
|
|
auto cache = createCache(config.configVarResolver);
|
|
cache->clear();
|
|
|
|
HandlerFactory handlerFactory{config.handlersConfig, siteTemplate, *database.get(), urlProvider, *cache.get()};
|
|
RequestWorker requestWorker{handlerFactory, database->createSessionDao(), siteTemplate};
|
|
|
|
auto interface = createGateway(config);
|
|
|
|
if(!sandbox->enableForWorker())
|
|
{
|
|
Logger::error() << "Sandboxing for worker could not be enabled!";
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
interface->work(requestWorker);
|
|
}
|
|
catch(const std::exception &e)
|
|
{
|
|
Logger::error() << e.what();
|
|
std::cerr << e.what() << std::endl;
|
|
}
|
|
return 0;
|
|
}
|