-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
74 lines (62 loc) · 1.9 KB
/
main.cpp
File metadata and controls
74 lines (62 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <QCoreApplication>
#include <QSettings>
#include <QFile>
#include <QSsl>
#include <QSslKey>
#include <QSslCertificate>
#include <QSslConfiguration>
#include "PMaildCoreMySQL.hpp"
#include "PMaildCoreSQLite.hpp"
static bool init_ssl(QSettings &settings);
int main(int argc, char *argv[]) {
QCoreApplication app(argc, argv);
QSettings settings("pmaild.ini", QSettings::IniFormat);
if (!init_ssl(settings)) return 1;
// initialize core
PMaildCore *core;
QString core_type = settings.value("core/backend", "SQLITE").toString().toUpper();
if (core_type == "MYSQL") {
if (!PMaildCoreMySQL::check()) {
qDebug("MySQL driver not available in this build of Qt");
return 2;
}
core = new PMaildCoreMySQL(settings);
} else if (core_type == "SQLITE") {
if (!PMaildCoreSQLite::check()) {
qDebug("SQLite3 driver not available in this build of Qt");
return 2;
}
core = new PMaildCoreSQLite(settings);
} else {
qDebug("Invalid core backend selected, please fix configuration");
return 2;
}
core->startDaemons();
return app.exec();
}
static bool init_ssl(QSettings &settings) {
// init SSL
settings.beginGroup("ssl");
QFile key_file(settings.value("key", "ssl/server.key").toString());
if (!key_file.open(QIODevice::ReadOnly)) {
qDebug("Could not open SSL key file");
return false;
}
QSslKey ssl_key = QSslKey(&key_file, QSsl::Rsa);
if (ssl_key.isNull()) {
qDebug("Failed to read SSL key");
return false;
}
key_file.close();
QSslCertificate ssl_cert = QSslCertificate::fromPath(settings.value("crt", "ssl/server.crt").toString(), QSsl::Pem, QRegExp::FixedString).at(0);
if (ssl_cert.isNull()) {
qDebug("failed to locate certificate");
return false;
}
QSslConfiguration config = QSslConfiguration::defaultConfiguration();
config.setLocalCertificate(ssl_cert);
config.setPrivateKey(ssl_key);
QSslConfiguration::setDefaultConfiguration(config);
settings.endGroup();
return true;
}