X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fcore%2Foidentdconfiggenerator.cpp;h=08b7636c97f2ebab5482bd0e12974f44289116cd;hp=072b15956b283b1f2dd68ab70a1b06568a8a24d0;hb=158443f71d48215eea8b47b836b61afd77654b78;hpb=2e80003ab40313767d0624e05533a62e2eee387b diff --git a/src/core/oidentdconfiggenerator.cpp b/src/core/oidentdconfiggenerator.cpp index 072b1595..08b7636c 100644 --- a/src/core/oidentdconfiggenerator.cpp +++ b/src/core/oidentdconfiggenerator.cpp @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2012 by the Quassel Project * + * Copyright (C) 2005-2018 by the Quassel Project * * devel@quassel-irc.org * * * * This program is free software; you can redistribute it and/or modify * @@ -15,100 +15,161 @@ * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * ***************************************************************************/ +#ifdef HAVE_UMASK +# include +# include +#endif /* HAVE_UMASK */ + +#include + +#include "corenetwork.h" #include "oidentdconfiggenerator.h" OidentdConfigGenerator::OidentdConfigGenerator(QObject *parent) : - QObject(parent), - _initialized(false) + QObject(parent) { - qDebug() << "OidentdConfigGenerator() checking for being initialized"; - if (!_initialized) - init(); + if (!_initialized) + init(); } -bool OidentdConfigGenerator::init() { - configDir = QDir::homePath(); - configFileName = ".oidentd.conf"; - if(Quassel::isOptionSet("oidentd-conffile")) - configPath = Quassel::optionValue("oidentd-conffile"); - else - configPath = configDir.absoluteFilePath(configFileName); +OidentdConfigGenerator::~OidentdConfigGenerator() +{ + _quasselConfig.clear(); + writeConfig(); + _configFile->deleteLater(); +} - configTag = " stanza created by Quassel"; - _configFile = new QFile(configPath); - qDebug() << "1: _configFile" << _configFile->fileName(); +bool OidentdConfigGenerator::init() +{ + _configDir = QDir::homePath(); + _configFileName = ".oidentd.conf"; - quasselStanza = QRegExp(QString("^lport .* { .* } #%1$").arg(configTag)); + if (Quassel::isOptionSet("oidentd-conffile")) + _configPath = Quassel::optionValue("oidentd-conffile"); + else + _configPath = _configDir.absoluteFilePath(_configFileName); - if (parseConfig(false) && writeConfig()) - _initialized = true; + _configTag = " stanza created by Quassel"; - qDebug() << "OidentdConfigGenerator" << (!_initialized ? "not" : "") << "initialized"; + _configFile = new QFile(_configPath); - return _initialized; -} + // Rx has to match Template in order for cleanup to work. + // Template should be enhanced with the "from" parameter as soon as Quassel gains + // the ability to bind to an IP on client sockets. -bool OidentdConfigGenerator::addSocket(const CoreIdentity *identity, const QHostAddress &localAddress, quint16 localPort, const QHostAddress &peerAddress, quint16 peerPort) { - qDebug() << "localAddress" << localAddress; - qDebug() << "localPort" << localPort; - qDebug() << "peerAddress" << peerAddress; - qDebug() << "peerPort" << peerPort; - qDebug() << "ident" << identity->ident(); + _quasselStanzaTemplate = QString("lport %1 { reply \"%2\" } #%3\n"); + _quasselStanzaRx = QRegExp(QString("^lport .* \\{ .* \\} #%1\\r?\\n").arg(_configTag)); - QString ident = identity->ident(); + // initially remove all Quassel stanzas that might be present + if (parseConfig(false) && writeConfig()) + _initialized = true; - _config.append(QString("lport %1 { reply \"%2\" } #%3\n").arg(localPort).arg(ident).arg(configTag)); + return _initialized; +} - bool ret = writeConfig(); - qDebug() << "config written" << ret; - return ret; +QString OidentdConfigGenerator::sysIdentForIdentity(const CoreIdentity *identity) const { + // Make sure the identity's ident complies with strict mode if enabled + const CoreNetwork *network = qobject_cast(sender()); + return network->coreSession()->strictCompliantIdent(identity); } -// not yet implemented -bool OidentdConfigGenerator::removeSocket(const CoreIdentity *identity, const QHostAddress &localAddress, quint16 localPort, const QHostAddress &peerAddress, quint16 peerPort) { - Q_UNUSED(identity) Q_UNUSED(localAddress) Q_UNUSED(localPort) Q_UNUSED(peerAddress) Q_UNUSED(peerPort) - return true; -} -bool OidentdConfigGenerator::parseConfig(bool keepQuasselStanzas) { - qDebug() << "_configFile name" << _configFile->fileName(); - qDebug() << "open?" << _configFile->isOpen(); - qDebug() << "keeping our stanzas?" << keepQuasselStanzas; - if (!_configFile->isOpen() && !_configFile->open(QIODevice::ReadWrite)) - return false; +bool OidentdConfigGenerator::addSocket(const CoreIdentity *identity, + const QHostAddress &localAddress, quint16 localPort, + const QHostAddress &peerAddress, quint16 peerPort, + qint64 socketId) +{ + Q_UNUSED(localAddress) + Q_UNUSED(peerAddress) + Q_UNUSED(peerPort) + Q_UNUSED(socketId) - QByteArray parsedConfig; - while (!_configFile->atEnd()) { - QByteArray line = _configFile->readLine(); + const QString ident = sysIdentForIdentity(identity); - qDebug() << "line" << line; - qDebug() << "line by us?" << lineByUs(line); - if (keepQuasselStanzas || !lineByUs(line)) - parsedConfig.append(line); - } + _quasselConfig.append(_quasselStanzaTemplate.arg(localPort).arg(ident).arg(_configTag).toLatin1()); - _config = parsedConfig; + bool ret = writeConfig(); - return true; + return ret; } -bool OidentdConfigGenerator::writeConfig() { - if (!_configFile->isOpen() && !_configFile->open(QFile::WriteOnly | QFile::Truncate)) - return false; - //FIXME: thread safety - QTextStream out(_configFile); - out << _config; +//! not yet implemented +bool OidentdConfigGenerator::removeSocket(const CoreIdentity *identity, + const QHostAddress &localAddress, quint16 localPort, + const QHostAddress &peerAddress, quint16 peerPort, + qint64 socketId) +{ + Q_UNUSED(identity) + Q_UNUSED(localAddress) + Q_UNUSED(localPort) + Q_UNUSED(peerAddress) + Q_UNUSED(peerPort) + Q_UNUSED(socketId) + + return true; +} + - return _configFile->flush(); +bool OidentdConfigGenerator::parseConfig(bool readQuasselStanzas) +{ + if (!_configFile->exists()) + return true; + + if (!_configFile->isOpen() && !_configFile->open(QIODevice::ReadOnly)) + return false; + _mutex.lock(); + + _parsedConfig.clear(); + _configFile->seek(0); + while (!_configFile->atEnd()) { + QByteArray line = _configFile->readLine(); + + if (!lineByUs(line)) + _parsedConfig.append(line); + else if (readQuasselStanzas) + _quasselConfig.append(line); + } + + _configFile->close(); + _mutex.unlock(); + return true; } -bool OidentdConfigGenerator::lineByUs(const QByteArray &line) { - return !quasselStanza.exactMatch(line); + +bool OidentdConfigGenerator::writeConfig() +{ +#ifdef HAVE_UMASK + mode_t prev_umask = umask(S_IXUSR | S_IWGRP | S_IXGRP | S_IWOTH | S_IXOTH); // == 0133, rw-r--r-- +#endif + bool not_open = (!_configFile->isOpen() && !_configFile->open(QIODevice::ReadWrite | QIODevice::Text)); +#ifdef HAVE_UMASK + umask(prev_umask); +#endif + + if (not_open) + return false; + + _mutex.lock(); + + _configFile->seek(0); + _configFile->resize(0); + _configFile->write(_parsedConfig); + _configFile->write(_quasselConfig); + + _configFile->close(); + _mutex.unlock(); + return true; +} + + +bool OidentdConfigGenerator::lineByUs(const QByteArray &line) +{ + return _quasselStanzaRx.exactMatch(line); }