1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
/*
* Copyright (C) 2008-2010 The QXmpp developers
*
* Author:
* Jeremy Lainé
*
* Source:
* http://code.google.com/p/qxmpp
*
* This file is a part of QXmpp library.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
*/
#include <QDomElement>
#include <QSslKey>
#include <QSslSocket>
#include "QXmppConstants.h"
#include "QXmppDialback.h"
#include "QXmppIncomingServer.h"
#include "QXmppOutgoingServer.h"
#include "QXmppStreamFeatures.h"
#include "QXmppUtils.h"
class QXmppIncomingServerPrivate
{
public:
QString authenticated;
QString domain;
QString localStreamId;
};
/// Constructs a new incoming server stream.
///
/// \param socket The socket for the XMPP stream.
/// \param domain The local domain.
/// \param parent The parent QObject for the stream (optional).
///
QXmppIncomingServer::QXmppIncomingServer(QSslSocket *socket, const QString &domain, QObject *parent)
: QXmppStream(parent),
d(new QXmppIncomingServerPrivate)
{
setObjectName("S2S-in");
setSocket(socket);
d->domain = domain;
}
/// Destroys the current stream.
QXmppIncomingServer::~QXmppIncomingServer()
{
delete d;
}
/// Returns the stream's identifier.
///
QString QXmppIncomingServer::localStreamId() const
{
return d->localStreamId;
}
void QXmppIncomingServer::handleStream(const QDomElement &streamElement)
{
if (!streamElement.attribute("from").isEmpty())
setObjectName("S2S-in-" + streamElement.attribute("from"));
// start stream
d->localStreamId = generateStanzaHash().toAscii();
QString data = QString("<?xml version='1.0'?><stream:stream"
" xmlns='%1' xmlns:db='%2' xmlns:stream='%3'"
" id='%4' version=\"1.0\">").arg(
ns_server,
ns_server_dialback,
ns_stream,
d->localStreamId);
sendData(data.toUtf8());
// send stream features
QXmppStreamFeatures features;
if (!socket()->isEncrypted() && !socket()->localCertificate().isNull() && !socket()->privateKey().isNull())
features.setTlsMode(QXmppStreamFeatures::Enabled);
sendPacket(features);
}
void QXmppIncomingServer::handleStanza(const QDomElement &stanza)
{
const QString ns = stanza.namespaceURI();
if (ns == ns_tls && stanza.tagName() == "starttls")
{
sendData("<proceed xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
socket()->flush();
socket()->startServerEncryption();
return;
}
else if (QXmppDialback::isDialback(stanza))
{
QXmppDialback request;
request.parse(stanza);
// check the request is valid
if (!request.type().isEmpty() ||
request.from().isEmpty() ||
request.to() != d->domain ||
request.key().isEmpty())
{
warning("Invalid dialback received");
return;
}
const QString domain = request.from();
setObjectName("S2S-in-" + domain);
if (request.command() == QXmppDialback::Result)
{
// establish dialback connection
QXmppOutgoingServer *stream = new QXmppOutgoingServer(d->domain, this);
stream->setLogger(logger());
stream->setObjectName("S2S-dialback-" + domain);
bool check = connect(stream, SIGNAL(dialbackResponseReceived(QXmppDialback)),
this, SLOT(slotDialbackResponseReceived(QXmppDialback)));
Q_ASSERT(check);
Q_UNUSED(check);
stream->setVerify(d->localStreamId, request.key());
stream->connectToHost(domain);
}
else if (request.command() == QXmppDialback::Verify)
{
emit dialbackRequestReceived(request);
}
}
else if (!d->authenticated.isEmpty() &&
jidToDomain(stanza.attribute("from")) == d->authenticated)
{
// relay stanza if the remote party is authenticated
emit elementReceived(stanza);
} else {
warning("Received an element, but remote party is not authenticated");
}
}
/// Returns true if the socket is connected and the remote server is
/// authenticated.
///
bool QXmppIncomingServer::isConnected() const
{
return QXmppStream::isConnected() && !d->authenticated.isEmpty();
}
/// Handles a dialback response received from the authority server.
///
/// \param response
///
void QXmppIncomingServer::slotDialbackResponseReceived(const QXmppDialback &dialback)
{
QXmppOutgoingServer *stream = qobject_cast<QXmppOutgoingServer*>(sender());
if (!stream ||
dialback.command() != QXmppDialback::Verify ||
dialback.id() != d->localStreamId ||
dialback.from() != stream->remoteDomain())
return;
// relay verify response
QXmppDialback response;
response.setCommand(QXmppDialback::Result);
response.setTo(dialback.from());
response.setFrom(d->domain);
response.setType(dialback.type());
sendPacket(response);
// check for success
if (response.type() == "valid")
{
info("Incoming stream is ready");
d->authenticated = dialback.from();
emit connected();
} else {
disconnectFromHost();
}
// disconnect dialback
stream->disconnectFromHost();
stream->deleteLater();
}
|