Fixing .desktop entry.
[quassel.git] / src / core / sslserver.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 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  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #ifndef QT_NO_OPENSSL
22
23 #include "sslserver.h"
24
25 #include <QSslSocket>
26 #include <QFile>
27 #include <QDebug>
28
29 #include "util.h"
30
31 SslServer::SslServer(QObject *parent)
32   : QTcpServer(parent)
33 {
34   QFile certFile(quasselDir().absolutePath() + "/quasselCert.pem");
35   certFile.open(QIODevice::ReadOnly);
36   _cert = QSslCertificate(&certFile);
37   certFile.close();
38
39   certFile.open(QIODevice::ReadOnly);
40   _key = QSslKey(&certFile, QSsl::Rsa);
41   certFile.close();
42
43   _certIsValid = !_cert.isNull() && _cert.isValid() && !_key.isNull();
44   if(!_certIsValid) {
45     qWarning() << "SslServer: SSL Certificate is either missing or has wrong format!";
46     qWarning() << "           make sure that ~/.quassel/quasselCert.pem is pem format and contains the cert and an rsa key!";
47     qWarning() << "SslServer: this Quassel Core cannot provide SSL!";
48   }
49 }
50
51 QTcpSocket *SslServer::nextPendingConnection() {
52   if(_pendingConnections.isEmpty())
53     return 0;
54   else
55     return _pendingConnections.takeFirst();
56 }
57
58 void SslServer::incomingConnection(int socketDescriptor) {
59   QSslSocket *serverSocket = new QSslSocket(this);
60   if(serverSocket->setSocketDescriptor(socketDescriptor)) {
61     if(certIsValid()) {
62       serverSocket->setLocalCertificate(_cert);
63       serverSocket->setPrivateKey(_key);
64     }
65     _pendingConnections << serverSocket;
66     emit newConnection();
67   } else {
68     delete serverSocket;
69   }
70 }
71
72 #endif // QT_NO_OPENSSL