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