Basic checks on path in SslServer::setCertificate.
[quassel.git] / src / core / sslserver.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 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 #ifdef HAVE_SSL
24 #  include <QSslSocket>
25 #endif
26
27 #include <QFile>
28 #include <QDebug>
29
30 #include "logger.h"
31 #include "util.h"
32
33 #ifdef HAVE_SSL
34
35 SslServer::SslServer(QObject *parent)
36   : QTcpServer(parent)
37 {
38   setCertificate(quasselDir().absolutePath() + "/quasselCert.pem");
39 }
40
41 QTcpSocket *SslServer::nextPendingConnection() {
42   if(_pendingConnections.isEmpty())
43     return 0;
44   else
45     return _pendingConnections.takeFirst();
46 }
47
48 void SslServer::incomingConnection(int socketDescriptor) {
49   QSslSocket *serverSocket = new QSslSocket(this);
50   if(serverSocket->setSocketDescriptor(socketDescriptor)) {
51     if(certIsValid()) {
52       serverSocket->setLocalCertificate(_cert);
53       serverSocket->setPrivateKey(_key);
54     }
55     _pendingConnections << serverSocket;
56     emit newConnection();
57   } else {
58     delete serverSocket;
59   }
60 }
61
62 bool SslServer::setCertificate(const QString &path) {
63   _certIsValid = false;
64
65   if (path.isNull()) {
66     return false;
67   }
68
69   QFile certFile(path);
70   if (! certFile.exists()) {
71     qWarning() << "SslServer: Certificate file" << qPrintable(path) << "does not exist";
72     return false;
73   }
74
75   certFile.open(QIODevice::ReadOnly);
76   _cert = QSslCertificate(&certFile);
77   certFile.close();
78
79   certFile.open(QIODevice::ReadOnly);
80   _key = QSslKey(&certFile, QSsl::Rsa);
81   certFile.close();
82
83   if(_cert.isNull() || !_cert.isValid() || _key.isNull()) {
84     qWarning() << "SslServer: SSL Certificate is either missing or has a wrong format!\n"
85                << "          Quassel Core will still work, but cannot provide SSL for client connections.\n"
86                << "          Please see http://quassel-irc.org/faq/cert to learn how to enable SSL support.";
87   } else {
88     _certIsValid = true;
89   }
90
91   return _certIsValid;
92 }
93
94 #endif // HAVE_SSL