cmake: Set -DHAVE_UMASK only where it's needed
[quassel.git] / src / core / oidentdconfiggenerator.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #ifdef HAVE_UMASK
22 #  include <sys/types.h>
23 #  include <sys/stat.h>
24 #endif /* HAVE_UMASK */
25
26 #include <QString>
27
28 #include "corenetwork.h"
29 #include "oidentdconfiggenerator.h"
30
31 OidentdConfigGenerator::OidentdConfigGenerator(QObject *parent) :
32     QObject(parent),
33     _initialized(false)
34 {
35     if (!_initialized)
36         init();
37 }
38
39
40 OidentdConfigGenerator::~OidentdConfigGenerator()
41 {
42     _quasselConfig.clear();
43     writeConfig();
44     _configFile->deleteLater();
45 }
46
47
48 bool OidentdConfigGenerator::init()
49 {
50     _configDir = QDir::homePath();
51     _configFileName = ".oidentd.conf";
52
53     if (Quassel::isOptionSet("oidentd-conffile"))
54         _configPath = Quassel::optionValue("oidentd-conffile");
55     else
56         _configPath = _configDir.absoluteFilePath(_configFileName);
57
58     _configTag = " stanza created by Quassel";
59
60     _configFile = new QFile(_configPath);
61
62     // Rx has to match Template in order for cleanup to work.
63     // Template should be enhanced with the "from" parameter as soon as Quassel gains
64     // the ability to bind to an IP on client sockets.
65
66     _quasselStanzaTemplate = QString("lport %1 { reply \"%2\" } #%3\n");
67     _quasselStanzaRx = QRegExp(QString("^lport .* \\{ .* \\} #%1\\r?\\n").arg(_configTag));
68
69     // initially remove all Quassel stanzas that might be present
70     if (parseConfig(false) && writeConfig())
71         _initialized = true;
72
73     return _initialized;
74 }
75
76
77 QString OidentdConfigGenerator::sysIdentForIdentity(const CoreIdentity *identity) const {
78     // Make sure the identity's ident complies with strict mode if enabled
79     const CoreNetwork *network = qobject_cast<CoreNetwork *>(sender());
80     return network->coreSession()->strictCompliantIdent(identity);
81 }
82
83
84 bool OidentdConfigGenerator::addSocket(const CoreIdentity *identity,
85                                        const QHostAddress &localAddress, quint16 localPort,
86                                        const QHostAddress &peerAddress, quint16 peerPort,
87                                        qint64 socketId)
88 {
89     Q_UNUSED(localAddress)
90     Q_UNUSED(peerAddress)
91     Q_UNUSED(peerPort)
92     Q_UNUSED(socketId)
93
94     const QString ident = sysIdentForIdentity(identity);
95
96     _quasselConfig.append(_quasselStanzaTemplate.arg(localPort).arg(ident).arg(_configTag).toLatin1());
97
98     bool ret = writeConfig();
99
100     return ret;
101 }
102
103
104 //! not yet implemented
105 bool OidentdConfigGenerator::removeSocket(const CoreIdentity *identity,
106                                           const QHostAddress &localAddress, quint16 localPort,
107                                           const QHostAddress &peerAddress, quint16 peerPort,
108                                           qint64 socketId)
109 {
110     Q_UNUSED(identity)
111     Q_UNUSED(localAddress)
112     Q_UNUSED(localPort)
113     Q_UNUSED(peerAddress)
114     Q_UNUSED(peerPort)
115     Q_UNUSED(socketId)
116
117     return true;
118 }
119
120
121 bool OidentdConfigGenerator::parseConfig(bool readQuasselStanzas)
122 {
123     if (!_configFile->exists())
124         return true;
125
126     if (!_configFile->isOpen() && !_configFile->open(QIODevice::ReadOnly))
127         return false;
128     _mutex.lock();
129
130     _parsedConfig.clear();
131     _configFile->seek(0);
132     while (!_configFile->atEnd()) {
133         QByteArray line = _configFile->readLine();
134
135         if (!lineByUs(line))
136             _parsedConfig.append(line);
137         else if (readQuasselStanzas)
138             _quasselConfig.append(line);
139     }
140
141     _configFile->close();
142     _mutex.unlock();
143     return true;
144 }
145
146
147 bool OidentdConfigGenerator::writeConfig()
148 {
149 #ifdef HAVE_UMASK
150     mode_t prev_umask = umask(S_IXUSR | S_IWGRP | S_IXGRP | S_IWOTH | S_IXOTH); // == 0133, rw-r--r--
151 #endif
152     bool not_open = (!_configFile->isOpen() && !_configFile->open(QIODevice::ReadWrite | QIODevice::Text));
153 #ifdef HAVE_UMASK
154     umask(prev_umask);
155 #endif
156
157     if (not_open)
158         return false;
159
160     _mutex.lock();
161
162     _configFile->seek(0);
163     _configFile->resize(0);
164     _configFile->write(_parsedConfig);
165     _configFile->write(_quasselConfig);
166
167     _configFile->close();
168     _mutex.unlock();
169     return true;
170 }
171
172
173 bool OidentdConfigGenerator::lineByUs(const QByteArray &line)
174 {
175     return _quasselStanzaRx.exactMatch(line);
176 }