Files
@ d94de26489f1
Branch filter:
Location: libtransport.git/include/Swiften/Server/ServerFromClientSession.cpp - annotation
d94de26489f1
7.1 KiB
text/x-c++hdr
Connect just from one resource (can be changed in config)
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 | 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 c6224941ceab 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 808a9067c5f5 e31c07794705 e31c07794705 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 1e8eb7076f17 808a9067c5f5 808a9067c5f5 1e8eb7076f17 1e8eb7076f17 1e8eb7076f17 1e8eb7076f17 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 1e8eb7076f17 808a9067c5f5 808a9067c5f5 1e8eb7076f17 1e8eb7076f17 1e8eb7076f17 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 66105b8dd899 d94de26489f1 d94de26489f1 d94de26489f1 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 9477e3f83882 9477e3f83882 9477e3f83882 9477e3f83882 9477e3f83882 9477e3f83882 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 828ae9d2cb88 | /*
* Copyright (c) 2010 Remko Tronçon
* Licensed under the GNU General Public License v3.
* See Documentation/Licenses/GPLv3.txt for more information.
*/
#include <Swiften/Server/ServerFromClientSession.h>
#include <boost/bind.hpp>
#include <Swiften/Elements/ProtocolHeader.h>
#include <Swiften/Server/UserRegistry.h>
#include <Swiften/Network/Connection.h>
#include <Swiften/StreamStack/XMPPLayer.h>
#include <Swiften/Elements/StreamFeatures.h>
#include <Swiften/Elements/ResourceBind.h>
#include <Swiften/Elements/StartSession.h>
#include <Swiften/Elements/IQ.h>
#include <Swiften/Elements/AuthSuccess.h>
#include <Swiften/Elements/AuthFailure.h>
#include <Swiften/Elements/AuthRequest.h>
#include <Swiften/SASL/PLAINMessage.h>
#include <Swiften/StreamStack/StreamStack.h>
#include <Swiften/StreamStack/TLSServerLayer.h>
#include <Swiften/Elements/StartTLSRequest.h>
#include <Swiften/Elements/TLSProceed.h>
#include <iostream>
namespace Swift {
ServerFromClientSession::ServerFromClientSession(
const std::string& id,
boost::shared_ptr<Connection> connection,
PayloadParserFactoryCollection* payloadParserFactories,
PayloadSerializerCollection* payloadSerializers,
UserRegistry* userRegistry) :
Session(connection, payloadParserFactories, payloadSerializers),
id_(id),
userRegistry_(userRegistry),
authenticated_(false),
initialized(false),
allowSASLEXTERNAL(false),
tlsLayer(0),
tlsConnected(false) {
}
ServerFromClientSession::~ServerFromClientSession() {
std::cout << "DESTRUCTOR;\n";
userRegistry_->onPasswordValid.disconnect(boost::bind(&ServerFromClientSession::handlePasswordValid, this, _1));
userRegistry_->onPasswordInvalid.disconnect(boost::bind(&ServerFromClientSession::handlePasswordInvalid, this, _1));
if (tlsLayer) {
delete tlsLayer;
}
}
void ServerFromClientSession::handlePasswordValid(const std::string &user) {
if (user != JID(user_, getLocalJID().getDomain()).toString())
return;
if (!isInitialized()) {
userRegistry_->onPasswordValid.disconnect(boost::bind(&ServerFromClientSession::handlePasswordValid, this, _1));
userRegistry_->onPasswordInvalid.disconnect(boost::bind(&ServerFromClientSession::handlePasswordInvalid, this, _1));
getXMPPLayer()->writeElement(boost::shared_ptr<AuthSuccess>(new AuthSuccess()));
authenticated_ = true;
getXMPPLayer()->resetParser();
}
}
void ServerFromClientSession::handlePasswordInvalid(const std::string &user) {
if (user != JID(user_, getLocalJID().getDomain()).toString() || authenticated_)
return;
if (!isInitialized()) {
userRegistry_->onPasswordValid.disconnect(boost::bind(&ServerFromClientSession::handlePasswordValid, this, _1));
userRegistry_->onPasswordInvalid.disconnect(boost::bind(&ServerFromClientSession::handlePasswordInvalid, this, _1));
getXMPPLayer()->writeElement(boost::shared_ptr<AuthFailure>(new AuthFailure));
finishSession(AuthenticationFailedError);
}
}
void ServerFromClientSession::handleElement(boost::shared_ptr<Element> element) {
if (isInitialized()) {
onElementReceived(element);
}
else {
if (AuthRequest* authRequest = dynamic_cast<AuthRequest*>(element.get())) {
if (authRequest->getMechanism() == "PLAIN" || (allowSASLEXTERNAL && authRequest->getMechanism() == "EXTERNAL")) {
if (authRequest->getMechanism() == "EXTERNAL") {
getXMPPLayer()->writeElement(boost::shared_ptr<AuthSuccess>(new AuthSuccess()));
authenticated_ = true;
getXMPPLayer()->resetParser();
}
else {
PLAINMessage plainMessage(authRequest->getMessage() ? *authRequest->getMessage() : createSafeByteArray(""));
user_ = plainMessage.getAuthenticationID();
userRegistry_->onPasswordInvalid(JID(plainMessage.getAuthenticationID(), getLocalJID().getDomain()).toBare().toString());
userRegistry_->onPasswordValid.connect(boost::bind(&ServerFromClientSession::handlePasswordValid, this, _1));
userRegistry_->onPasswordInvalid.connect(boost::bind(&ServerFromClientSession::handlePasswordInvalid, this, _1));
if (userRegistry_->isValidUserPassword(JID(plainMessage.getAuthenticationID(), getLocalJID().getDomain()), plainMessage.getPassword())) {
// we're waiting for usermanager signal now
// authenticated_ = true;
// getXMPPLayer()->resetParser();
}
else {
getXMPPLayer()->writeElement(boost::shared_ptr<AuthFailure>(new AuthFailure));
finishSession(AuthenticationFailedError);
}
}
}
else {
getXMPPLayer()->writeElement(boost::shared_ptr<AuthFailure>(new AuthFailure));
finishSession(NoSupportedAuthMechanismsError);
}
}
else if (dynamic_cast<StartTLSRequest*>(element.get()) != NULL) {
getXMPPLayer()->writeElement(boost::shared_ptr<TLSProceed>(new TLSProceed));
getStreamStack()->addLayer(tlsLayer);
tlsLayer->connect();
getXMPPLayer()->resetParser();
}
else if (IQ* iq = dynamic_cast<IQ*>(element.get())) {
if (boost::shared_ptr<ResourceBind> resourceBind = iq->getPayload<ResourceBind>()) {
std::string bucket = "abcdefghijklmnopqrstuvwxyz";
std::string uuid;
for (int i = 0; i < 10; i++) {
uuid += bucket[rand() % bucket.size()];
}
setRemoteJID(JID(user_, getLocalJID().getDomain(), uuid));
boost::shared_ptr<ResourceBind> resultResourceBind(new ResourceBind());
resultResourceBind->setJID(getRemoteJID());
getXMPPLayer()->writeElement(IQ::createResult(JID(), iq->getID(), resultResourceBind));
}
else if (iq->getPayload<StartSession>()) {
getXMPPLayer()->writeElement(IQ::createResult(getRemoteJID(), iq->getID()));
setInitialized();
}
}
}
}
void ServerFromClientSession::handleStreamStart(const ProtocolHeader& incomingHeader) {
setLocalJID(JID("", incomingHeader.getTo()));
ProtocolHeader header;
header.setFrom(incomingHeader.getTo());
header.setID(id_);
getXMPPLayer()->writeHeader(header);
boost::shared_ptr<StreamFeatures> features(new StreamFeatures());
if (!authenticated_) {
if (tlsLayer && !tlsConnected) {
features->setHasStartTLS();
}
features->addAuthenticationMechanism("PLAIN");
if (allowSASLEXTERNAL) {
features->addAuthenticationMechanism("EXTERNAL");
}
}
else {
features->setHasResourceBind();
features->setHasSession();
}
getXMPPLayer()->writeElement(features);
}
void ServerFromClientSession::setInitialized() {
initialized = true;
onSessionStarted();
}
void ServerFromClientSession::setAllowSASLEXTERNAL() {
allowSASLEXTERNAL = true;
}
void ServerFromClientSession::addTLSEncryption(TLSServerContextFactory* tlsContextFactory, const PKCS12Certificate& cert) {
tlsLayer = new TLSServerLayer(tlsContextFactory);
if (!tlsLayer->setServerCertificate(cert)) {
// std::cout << "error\n";
// TODO:
// onClosed(boost::shared_ptr<Error>(new Error(Error::InvalidTLSCertificateError)));
}
else {
tlsLayer->onError.connect(boost::bind(&ServerFromClientSession::handleTLSError, this));
tlsLayer->onConnected.connect(boost::bind(&ServerFromClientSession::handleTLSConnected, this));
// getStreamStack()->addLayer(tlsLayer);
// tlsLayer->onError.connect(boost::bind(&BasicSessionStream::handleTLSError, this));
// tlsLayer->onConnected.connect(boost::bind(&BasicSessionStream::handleTLSConnected, this));
// tlsLayer->connect();
}
}
}
|