You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1344 lines
36 KiB
1344 lines
36 KiB
15 years ago
|
/***************************************************************************
|
||
|
sieve.cpp - description
|
||
|
-------------------
|
||
|
begin : Thu Dec 20 18:47:08 EST 2001
|
||
|
copyright : (C) 2001 by Hamish Rodda
|
||
|
email : meddie@yoyo.cc.monash.edu.au
|
||
|
***************************************************************************/
|
||
|
|
||
|
/***************************************************************************
|
||
|
* *
|
||
|
* This program is free software; you can redistribute it and/or modify *
|
||
|
* it under the terms of the GNU General Public License version 2 as *
|
||
|
* published by the Free Software Foundation. *
|
||
|
* *
|
||
|
***************************************************************************/
|
||
|
|
||
|
/**
|
||
|
* Portions adapted from the SMTP ioslave.
|
||
|
* Copyright (c) 2000, 2001 Alex Zepeda <jazepeda@pacbell.net>
|
||
|
* Copyright (c) 2001 Michael Häckel <Michael@Haeckel.Net>
|
||
|
* All rights reserved.
|
||
|
*
|
||
|
* Policy: the function where the error occurs calls error(). A result of
|
||
|
* false, where it signifies an error, thus doesn't need to call error() itself.
|
||
|
*/
|
||
|
|
||
|
#ifdef HAVE_CONFIG_H
|
||
|
# include <config.h>
|
||
|
#endif
|
||
|
|
||
|
extern "C" {
|
||
|
#include <sasl/sasl.h>
|
||
|
}
|
||
|
#include "sieve.h"
|
||
|
|
||
|
#include <kdebug.h>
|
||
|
#include <kinstance.h>
|
||
12 years ago
|
#include <tdelocale.h>
|
||
15 years ago
|
#include <kurl.h>
|
||
|
#include <kmdcodec.h>
|
||
12 years ago
|
#include <tdeglobal.h>
|
||
|
#include <tdemessagebox.h>
|
||
15 years ago
|
|
||
14 years ago
|
#include <tqcstring.h>
|
||
|
#include <tqregexp.h>
|
||
15 years ago
|
|
||
|
#include <cstdlib>
|
||
|
using std::exit;
|
||
|
#include <sys/stat.h>
|
||
14 years ago
|
#include <cassert>
|
||
15 years ago
|
|
||
13 years ago
|
#include <tdepimmacros.h>
|
||
15 years ago
|
|
||
|
static const int debugArea = 7122;
|
||
|
|
||
|
static inline
|
||
|
#ifdef NDEBUG
|
||
|
kndbgstream ksDebug() { return kdDebug( debugArea ); }
|
||
|
kndbgstream ksDebug( bool cond ) { return kdDebug( cond, debugArea ); }
|
||
|
#else
|
||
|
kdbgstream ksDebug() { return kdDebug( debugArea ); }
|
||
|
kdbgstream ksDebug( bool cond ) { return kdDebug( cond, debugArea ); }
|
||
|
#endif
|
||
|
|
||
|
#define SIEVE_DEFAULT_PORT 2000
|
||
|
|
||
|
static sasl_callback_t callbacks[] = {
|
||
|
{ SASL_CB_ECHOPROMPT, NULL, NULL },
|
||
|
{ SASL_CB_NOECHOPROMPT, NULL, NULL },
|
||
|
{ SASL_CB_GETREALM, NULL, NULL },
|
||
|
{ SASL_CB_USER, NULL, NULL },
|
||
|
{ SASL_CB_AUTHNAME, NULL, NULL },
|
||
|
{ SASL_CB_PASS, NULL, NULL },
|
||
|
{ SASL_CB_CANON_USER, NULL, NULL },
|
||
|
{ SASL_CB_LIST_END, NULL, NULL }
|
||
|
};
|
||
|
|
||
|
static const unsigned int SIEVE_DEFAULT_RECIEVE_BUFFER = 512;
|
||
|
|
||
12 years ago
|
using namespace TDEIO;
|
||
15 years ago
|
extern "C"
|
||
|
{
|
||
|
KDE_EXPORT int kdemain(int argc, char **argv)
|
||
|
{
|
||
12 years ago
|
TDEInstance instance("tdeio_sieve" );
|
||
15 years ago
|
|
||
12 years ago
|
ksDebug() << "*** Starting tdeio_sieve " << endl;
|
||
15 years ago
|
|
||
|
if (argc != 4) {
|
||
12 years ago
|
ksDebug() << "Usage: tdeio_sieve protocol domain-socket1 domain-socket2" << endl;
|
||
15 years ago
|
exit(-1);
|
||
|
}
|
||
|
|
||
|
if ( sasl_client_init( NULL ) != SASL_OK ) {
|
||
|
fprintf(stderr, "SASL library initialization failed!\n");
|
||
|
::exit (-1);
|
||
|
}
|
||
|
|
||
12 years ago
|
tdeio_sieveProtocol slave(argv[2], argv[3]);
|
||
15 years ago
|
slave.dispatchLoop();
|
||
|
|
||
|
sasl_done();
|
||
|
|
||
12 years ago
|
ksDebug() << "*** tdeio_sieve Done" << endl;
|
||
15 years ago
|
return 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/* ---------------------------------------------------------------------------------- */
|
||
12 years ago
|
tdeio_sieveResponse::tdeio_sieveResponse()
|
||
15 years ago
|
{
|
||
|
clear();
|
||
|
}
|
||
|
|
||
|
/* ---------------------------------------------------------------------------------- */
|
||
12 years ago
|
const uint& tdeio_sieveResponse::getType() const
|
||
15 years ago
|
{
|
||
|
return rType;
|
||
|
}
|
||
|
|
||
|
/* ---------------------------------------------------------------------------------- */
|
||
12 years ago
|
const uint tdeio_sieveResponse::getQuantity() const
|
||
15 years ago
|
{
|
||
|
return quantity;
|
||
|
}
|
||
|
|
||
|
/* ---------------------------------------------------------------------------------- */
|
||
12 years ago
|
const TQCString& tdeio_sieveResponse::getAction() const
|
||
15 years ago
|
{
|
||
|
return key;
|
||
|
}
|
||
|
|
||
|
/* ---------------------------------------------------------------------------------- */
|
||
12 years ago
|
const TQCString& tdeio_sieveResponse::getKey() const
|
||
15 years ago
|
{
|
||
|
return key;
|
||
|
}
|
||
|
|
||
|
/* ---------------------------------------------------------------------------------- */
|
||
12 years ago
|
const TQCString& tdeio_sieveResponse::getVal() const
|
||
15 years ago
|
{
|
||
|
return val;
|
||
|
}
|
||
|
|
||
|
/* ---------------------------------------------------------------------------------- */
|
||
12 years ago
|
const TQCString& tdeio_sieveResponse::getExtra() const
|
||
15 years ago
|
{
|
||
|
return extra;
|
||
|
}
|
||
|
|
||
|
/* ---------------------------------------------------------------------------------- */
|
||
12 years ago
|
void tdeio_sieveResponse::setQuantity(const uint& newTQty)
|
||
15 years ago
|
{
|
||
13 years ago
|
rType = QUANTITY;
|
||
14 years ago
|
quantity = newTQty;
|
||
15 years ago
|
}
|
||
|
|
||
|
/* ---------------------------------------------------------------------------------- */
|
||
12 years ago
|
void tdeio_sieveResponse::setAction(const TQCString& newAction)
|
||
15 years ago
|
{
|
||
|
rType = ACTION;
|
||
|
key = newAction.copy();
|
||
|
}
|
||
|
|
||
|
/* ---------------------------------------------------------------------------------- */
|
||
12 years ago
|
void tdeio_sieveResponse::setKey(const TQCString& newKey)
|
||
15 years ago
|
{
|
||
|
rType = KEY_VAL_PAIR;
|
||
|
key = newKey.copy();
|
||
|
}
|
||
|
|
||
|
/* ---------------------------------------------------------------------------------- */
|
||
12 years ago
|
void tdeio_sieveResponse::setVal(const TQCString& newVal)
|
||
15 years ago
|
{
|
||
|
val = newVal.copy();
|
||
|
}
|
||
|
|
||
|
/* ---------------------------------------------------------------------------------- */
|
||
12 years ago
|
void tdeio_sieveResponse::setExtra(const TQCString& newExtra)
|
||
15 years ago
|
{
|
||
|
extra = newExtra.copy();
|
||
|
}
|
||
|
|
||
|
/* ---------------------------------------------------------------------------------- */
|
||
12 years ago
|
void tdeio_sieveResponse::clear()
|
||
15 years ago
|
{
|
||
|
rType = NONE;
|
||
14 years ago
|
extra = key = val = TQCString("");
|
||
15 years ago
|
quantity = 0;
|
||
|
}
|
||
|
|
||
|
/* ---------------------------------------------------------------------------------- */
|
||
12 years ago
|
tdeio_sieveProtocol::tdeio_sieveProtocol(const TQCString &pool_socket, const TQCString &app_socket)
|
||
15 years ago
|
: TCPSlaveBase( SIEVE_DEFAULT_PORT, "sieve", pool_socket, app_socket, false)
|
||
|
, m_connMode(NORMAL)
|
||
|
, m_supportsTLS(false)
|
||
|
, m_shouldBeConnected(false)
|
||
14 years ago
|
, m_allowUnencrypted(false)
|
||
15 years ago
|
{
|
||
|
}
|
||
|
|
||
|
/* ---------------------------------------------------------------------------------- */
|
||
12 years ago
|
tdeio_sieveProtocol::~tdeio_sieveProtocol()
|
||
15 years ago
|
{
|
||
|
if ( isConnectionValid() )
|
||
|
disconnect();
|
||
|
}
|
||
|
|
||
|
/* ---------------------------------------------------------------------------------- */
|
||
12 years ago
|
void tdeio_sieveProtocol::setHost (const TQString &host, int port, const TQString &user, const TQString &pass)
|
||
15 years ago
|
{
|
||
|
if ( isConnectionValid() &&
|
||
|
( m_sServer != host ||
|
||
|
m_iPort != port ||
|
||
|
m_sUser != user ||
|
||
|
m_sPass != pass ) ) {
|
||
|
disconnect();
|
||
|
}
|
||
|
m_sServer = host;
|
||
|
m_iPort = port ? port : m_iDefaultPort;
|
||
|
m_sUser = user;
|
||
|
m_sPass = pass;
|
||
|
m_supportsTLS = false;
|
||
|
}
|
||
|
|
||
|
/* ---------------------------------------------------------------------------------- */
|
||
12 years ago
|
void tdeio_sieveProtocol::openConnection()
|
||
15 years ago
|
{
|
||
|
m_connMode = CONNECTION_ORIENTED;
|
||
|
connect();
|
||
|
}
|
||
|
|
||
12 years ago
|
bool tdeio_sieveProtocol::parseCapabilities(bool requestCapabilities/* = false*/)
|
||
15 years ago
|
{
|
||
|
ksDebug() << k_funcinfo << endl;
|
||
|
|
||
|
// Setup...
|
||
|
bool ret = false;
|
||
|
|
||
|
if (requestCapabilities) {
|
||
|
sendData("CAPABILITY");
|
||
|
}
|
||
|
|
||
|
while (receiveData()) {
|
||
|
ksDebug() << "Looping receive" << endl;
|
||
|
|
||
12 years ago
|
if (r.getType() == tdeio_sieveResponse::ACTION) {
|
||
13 years ago
|
if ( r.getAction().contains("ok", false) != -1 ) {
|
||
15 years ago
|
ksDebug() << "Sieve server ready & awaiting authentication." << endl;
|
||
|
break;
|
||
|
} else
|
||
|
ksDebug() << "Unknown action " << r.getAction() << "." << endl;
|
||
|
|
||
|
} else if (r.getKey() == "IMPLEMENTATION") {
|
||
13 years ago
|
if (r.getVal().contains("sieve", false) != -1) {
|
||
15 years ago
|
ksDebug() << "Connected to Sieve server: " << r.getVal() << endl;
|
||
|
ret = true;
|
||
|
setMetaData("implementation", r.getVal());
|
||
|
m_implementation = r.getVal();
|
||
|
}
|
||
|
|
||
|
} else if (r.getKey() == "SASL") {
|
||
|
// Save list of available SASL methods
|
||
14 years ago
|
m_sasl_caps = TQStringList::split(' ', r.getVal());
|
||
15 years ago
|
ksDebug() << "Server SASL authentication methods: " << m_sasl_caps.join(", ") << endl;
|
||
|
setMetaData("saslMethods", r.getVal());
|
||
|
|
||
|
} else if (r.getKey() == "SIEVE") {
|
||
|
// Save script capabilities; report back as meta data:
|
||
14 years ago
|
ksDebug() << "Server script capabilities: " << TQStringList::split(' ', r.getVal()).join(", ") << endl;
|
||
15 years ago
|
setMetaData("sieveExtensions", r.getVal());
|
||
|
|
||
|
} else if (r.getKey() == "STARTTLS") {
|
||
|
// The server supports TLS
|
||
|
ksDebug() << "Server supports TLS" << endl;
|
||
|
m_supportsTLS = true;
|
||
|
setMetaData("tlsSupported", "true");
|
||
|
|
||
|
} else {
|
||
14 years ago
|
ksDebug() << "Unrecognised key." << endl;
|
||
15 years ago
|
}
|
||
|
}
|
||
|
|
||
|
if (!m_supportsTLS) {
|
||
|
setMetaData("tlsSupported", "false");
|
||
|
}
|
||
|
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
|
|
||
|
/* ---------------------------------------------------------------------------------- */
|
||
|
/**
|
||
14 years ago
|
* Checks if connection parameters have changed.
|
||
15 years ago
|
* If it it, close the current connection
|
||
|
*/
|
||
12 years ago
|
void tdeio_sieveProtocol::changeCheck( const KURL &url )
|
||
15 years ago
|
{
|
||
14 years ago
|
TQString auth;
|
||
15 years ago
|
|
||
|
if (!metaData("sasl").isEmpty())
|
||
|
auth = metaData("sasl").upper();
|
||
|
else {
|
||
14 years ago
|
TQString query = url.query();
|
||
15 years ago
|
if ( query.startsWith("?") ) query.remove( 0, 1 );
|
||
14 years ago
|
TQStringList q = TQStringList::split( ",", query );
|
||
|
TQStringList::iterator it;
|
||
15 years ago
|
|
||
|
for ( it = q.begin(); it != q.end(); ++it ) {
|
||
14 years ago
|
if ( TQString( (*it).section('=',0,0) ).lower() == "x-mech" ) {
|
||
|
auth = TQString( (*it).section('=',1) ).upper();
|
||
15 years ago
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
ksDebug() << "auth: " << auth << " m_sAuth: " << m_sAuth << endl;
|
||
|
if ( m_sAuth != auth ) {
|
||
|
m_sAuth = auth;
|
||
|
if ( isConnectionValid() )
|
||
|
disconnect();
|
||
|
}
|
||
14 years ago
|
|
||
|
// For TLS, only disconnect if we are unencrypted and are
|
||
|
// no longer allowed (otherwise, it's still fine):
|
||
|
const bool allowUnencryptedNow = url.queryItem("x-allow-unencrypted") == "true" ;
|
||
|
if ( m_allowUnencrypted && !allowUnencryptedNow )
|
||
|
if ( isConnectionValid() )
|
||
|
disconnect();
|
||
|
m_allowUnencrypted = allowUnencryptedNow;
|
||
15 years ago
|
}
|
||
|
|
||
|
/* ---------------------------------------------------------------------------------- */
|
||
|
/**
|
||
|
* Connects to the server.
|
||
|
* returns false and calls error() if an error occurred.
|
||
|
*/
|
||
12 years ago
|
bool tdeio_sieveProtocol::connect(bool useTLSIfAvailable)
|
||
15 years ago
|
{
|
||
|
ksDebug() << k_funcinfo << endl;
|
||
|
|
||
|
if (isConnectionValid()) return true;
|
||
|
|
||
13 years ago
|
infoMessage(i18n("Connecting to %1...").arg( m_sServer));
|
||
15 years ago
|
|
||
|
if (m_connMode == CONNECTION_ORIENTED && m_shouldBeConnected) {
|
||
|
error(ERR_CONNECTION_BROKEN, i18n("The connection to the server was lost."));
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
setBlockConnection(true);
|
||
|
|
||
|
if (!connectToHost(m_sServer, m_iPort, true)) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
if (!parseCapabilities()) {
|
||
|
closeDescriptor();
|
||
|
error(ERR_UNSUPPORTED_PROTOCOL, i18n("Server identification failed."));
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
// Attempt to start TLS
|
||
14 years ago
|
if ( !m_allowUnencrypted && !canUseTLS() ) {
|
||
13 years ago
|
error( ERR_SLAVE_DEFINED, i18n("Can not use TLS. Please enable TLS in the TDE cryptography setting.") );
|
||
14 years ago
|
disconnect();
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
if ( !m_allowUnencrypted && useTLSIfAvailable && canUseTLS() && !m_supportsTLS &&
|
||
|
messageBox( WarningContinueCancel,
|
||
|
i18n("TLS encryption was requested, but your Sieve server does not advertise TLS in its capabilities.\n"
|
||
|
"You can choose to try to initiate TLS negotiations nonetheless, or cancel the operation."),
|
||
|
i18n("Server Does Not Advertise TLS"), i18n("&Start TLS nonetheless"), i18n("&Cancel") ) != KMessageBox::Continue )
|
||
|
{
|
||
|
error( ERR_USER_CANCELED, i18n("TLS encryption requested, but not supported by server.") );
|
||
|
disconnect();
|
||
|
return false;
|
||
|
}
|
||
|
|
||
15 years ago
|
// FIXME find a test server and test that this works
|
||
14 years ago
|
if (useTLSIfAvailable && canUseTLS()) {
|
||
15 years ago
|
sendData("STARTTLS");
|
||
|
if (operationSuccessful()) {
|
||
|
ksDebug() << "TLS has been accepted. Starting TLS..." << endl
|
||
|
<< "WARNING this is untested and may fail." << endl;
|
||
|
int retval = startTLS();
|
||
|
if (retval == 1) {
|
||
|
ksDebug() << "TLS enabled successfully." << endl;
|
||
|
// reparse capabilities:
|
||
|
parseCapabilities( requestCapabilitiesAfterStartTLS() );
|
||
|
} else {
|
||
|
ksDebug() << "TLS initiation failed, code " << retval << endl;
|
||
14 years ago
|
if ( m_allowUnencrypted ) {
|
||
|
disconnect(true);
|
||
|
return connect(false);
|
||
|
}
|
||
|
if ( retval != -3 )
|
||
|
messageBox( Information,
|
||
|
i18n("Your Sieve server claims to support TLS, "
|
||
|
"but negotiation was unsuccessful."),
|
||
|
i18n("Connection Failed") );
|
||
|
disconnect(true);
|
||
|
return false;
|
||
15 years ago
|
}
|
||
14 years ago
|
} else if ( !m_allowUnencrypted ) {
|
||
|
ksDebug() << "Server incapable of TLS." << endl;
|
||
|
disconnect();
|
||
|
error( ERR_SLAVE_DEFINED, i18n("The server does not seem to support TLS. "
|
||
|
"Disable TLS if you want to connect without encryption.") );
|
||
|
return false;
|
||
15 years ago
|
} else
|
||
|
ksDebug() << "Server incapable of TLS. Transmitted documents will be unencrypted." << endl;
|
||
|
} else
|
||
|
ksDebug() << "We are incapable of TLS. Transmitted documents will be unencrypted." << endl;
|
||
|
|
||
14 years ago
|
assert( m_allowUnencrypted || usingTLS() );
|
||
|
|
||
15 years ago
|
infoMessage(i18n("Authenticating user..."));
|
||
|
if (!authenticate()) {
|
||
|
disconnect();
|
||
|
error(ERR_COULD_NOT_AUTHENTICATE, i18n("Authentication failed."));
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
m_shouldBeConnected = true;
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
/* ---------------------------------------------------------------------------------- */
|
||
12 years ago
|
void tdeio_sieveProtocol::closeConnection()
|
||
15 years ago
|
{
|
||
|
m_connMode = CONNECTION_ORIENTED;
|
||
|
disconnect();
|
||
|
}
|
||
|
|
||
|
/* ---------------------------------------------------------------------------------- */
|
||
12 years ago
|
void tdeio_sieveProtocol::disconnect(bool forcibly)
|
||
15 years ago
|
{
|
||
|
if (!forcibly) {
|
||
|
sendData("LOGOUT");
|
||
|
|
||
|
// This crashes under certain conditions as described in
|
||
|
// http://intevation.de/roundup/kolab/issue2442
|
||
12 years ago
|
// Fixing TDEIO::TCPSlaveBase::atEnd() for !fd would also work but 3.x is on life support.
|
||
15 years ago
|
//if (!operationSuccessful())
|
||
|
// ksDebug() << "Server did not logout cleanly." << endl;
|
||
|
}
|
||
|
|
||
|
closeDescriptor();
|
||
|
m_shouldBeConnected = false;
|
||
|
}
|
||
|
|
||
|
/* ---------------------------------------------------------------------------------- */
|
||
12 years ago
|
/*void tdeio_sieveProtocol::slave_status()
|
||
15 years ago
|
{
|
||
13 years ago
|
slaveStatus(isConnectionValid() ? m_sServer : "", isConnectionValid());
|
||
15 years ago
|
|
||
|
finished();
|
||
|
}*/
|
||
|
|
||
|
/* ---------------------------------------------------------------------------------- */
|
||
12 years ago
|
void tdeio_sieveProtocol::special(const TQByteArray &data)
|
||
15 years ago
|
{
|
||
|
int tmp;
|
||
14 years ago
|
TQDataStream stream(data, IO_ReadOnly);
|
||
15 years ago
|
KURL url;
|
||
|
|
||
|
stream >> tmp;
|
||
|
|
||
|
switch (tmp) {
|
||
|
case 1:
|
||
|
stream >> url;
|
||
|
if (!activate(url))
|
||
|
return;
|
||
|
break;
|
||
|
case 2:
|
||
|
if (!deactivate())
|
||
|
return;
|
||
|
break;
|
||
|
case 3:
|
||
|
parseCapabilities(true);
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
infoMessage(i18n("Done."));
|
||
|
|
||
|
finished();
|
||
|
}
|
||
|
|
||
|
/* ---------------------------------------------------------------------------------- */
|
||
12 years ago
|
bool tdeio_sieveProtocol::activate(const KURL& url)
|
||
15 years ago
|
{
|
||
|
changeCheck( url );
|
||
|
if (!connect())
|
||
|
return false;
|
||
|
|
||
|
infoMessage(i18n("Activating script..."));
|
||
|
|
||
14 years ago
|
TQString filename = url.fileName(false);
|
||
15 years ago
|
|
||
|
if (filename.isEmpty()) {
|
||
|
error(ERR_DOES_NOT_EXIST, url.prettyURL());
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
if (!sendData("SETACTIVE \"" + filename.utf8() + "\""))
|
||
|
return false;
|
||
|
|
||
|
if (operationSuccessful()) {
|
||
|
ksDebug() << "Script activation complete." << endl;
|
||
|
return true;
|
||
|
} else {
|
||
|
error(ERR_INTERNAL_SERVER, i18n("There was an error activating the script."));
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/* ---------------------------------------------------------------------------------- */
|
||
12 years ago
|
bool tdeio_sieveProtocol::deactivate()
|
||
15 years ago
|
{
|
||
|
if (!connect())
|
||
|
return false;
|
||
|
|
||
|
if (!sendData("SETACTIVE \"\""))
|
||
|
return false;
|
||
|
|
||
|
if (operationSuccessful()) {
|
||
|
ksDebug() << "Script deactivation complete." << endl;
|
||
|
return true;
|
||
|
} else {
|
||
|
error(ERR_INTERNAL_SERVER, i18n("There was an error deactivating the script."));
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
14 years ago
|
static void append_lf2crlf( TQByteArray & out, const TQByteArray & in ) {
|
||
15 years ago
|
if ( in.isEmpty() )
|
||
|
return;
|
||
|
const unsigned int oldOutSize = out.size();
|
||
|
out.resize( oldOutSize + 2 * in.size() );
|
||
|
const char * s = in.begin();
|
||
|
const char * const end = in.end();
|
||
|
char * d = out.begin() + oldOutSize;
|
||
|
char last = '\0';
|
||
|
while ( s < end ) {
|
||
|
if ( *s == '\n' && last != '\r' )
|
||
|
*d++ = '\r';
|
||
|
*d++ = last = *s++;
|
||
|
}
|
||
|
out.resize( d - out.begin() );
|
||
|
}
|
||
|
|
||
12 years ago
|
void tdeio_sieveProtocol::put(const KURL& url, int /*permissions*/, bool /*overwrite*/, bool /*resume*/)
|
||
15 years ago
|
{
|
||
|
changeCheck( url );
|
||
|
if (!connect())
|
||
|
return;
|
||
|
|
||
|
infoMessage(i18n("Sending data..."));
|
||
|
|
||
14 years ago
|
TQString filename = url.fileName(false);
|
||
15 years ago
|
|
||
|
if (filename.isEmpty()) {
|
||
|
error(ERR_MALFORMED_URL, url.prettyURL());
|
||
|
return;
|
||
|
}
|
||
|
|
||
14 years ago
|
TQByteArray data;
|
||
15 years ago
|
for (;;) {
|
||
|
dataReq();
|
||
14 years ago
|
TQByteArray buffer;
|
||
15 years ago
|
const int newSize = readData(buffer);
|
||
|
append_lf2crlf( data, buffer );
|
||
|
if ( newSize < 0 ) {
|
||
|
// read error: network in unknown state so disconnect
|
||
11 years ago
|
error(ERR_COULD_NOT_READ, i18n("TDEIO data supply error."));
|
||
15 years ago
|
return;
|
||
|
}
|
||
|
if ( newSize == 0 )
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
// script size
|
||
|
int bufLen = (int)data.size();
|
||
|
totalSize(bufLen);
|
||
|
|
||
|
// timsieved 1.1.0:
|
||
|
// C: HAVESPACE "rejected" 74
|
||
|
// S: NO "Number expected"
|
||
|
// C: HAVESPACE 74
|
||
|
// S: NO "Missing script name"
|
||
|
// S: HAVESPACE "rejected" "74"
|
||
|
// C: NO "Number expected"
|
||
|
// => broken, we can't use it :-(
|
||
|
// (will be fixed in Cyrus 2.1.10)
|
||
|
#ifndef HAVE_BROKEN_TIMSIEVED
|
||
|
// first, check quota (it's a SHOULD in draft std)
|
||
|
if (!sendData("HAVESPACE \"" + filename.utf8() + "\" "
|
||
14 years ago
|
+ TQCString().setNum( bufLen )))
|
||
15 years ago
|
return;
|
||
|
|
||
|
if (!operationSuccessful()) {
|
||
|
error(ERR_DISK_FULL, i18n("Quota exceeded"));
|
||
|
return;
|
||
|
}
|
||
|
#endif
|
||
|
|
||
|
if (!sendData("PUTSCRIPT \"" + filename.utf8() + "\" {"
|
||
14 years ago
|
+ TQCString().setNum( bufLen ) + "+}"))
|
||
15 years ago
|
return;
|
||
|
|
||
|
// atEnd() lies so the code below doesn't work.
|
||
|
/*if (!atEnd()) {
|
||
|
// We are not expecting any data here, so if the server has responded
|
||
|
// with anything but OK we treat it as an error.
|
||
|
char * buf = new char[2];
|
||
|
while (!atEnd()) {
|
||
|
ksDebug() << "Reading..." << endl;
|
||
|
read(buf, 1);
|
||
|
ksDebug() << "Trailing [" << buf[0] << "]" << endl;
|
||
|
}
|
||
|
ksDebug() << "End of data." << endl;
|
||
|
delete[] buf;
|
||
|
|
||
|
if (!operationSuccessful()) {
|
||
|
error(ERR_UNSUPPORTED_PROTOCOL, i18n("A protocol error occurred "
|
||
|
"while trying to negotiate script uploading.\n"
|
||
|
"The server responded:\n%1")
|
||
13 years ago
|
.arg(r.getAction().right(r.getAction().length() - 3)));
|
||
15 years ago
|
return;
|
||
|
}
|
||
|
}*/
|
||
|
|
||
|
// upload data to the server
|
||
|
if (write(data, bufLen) != bufLen) {
|
||
|
error(ERR_COULD_NOT_WRITE, i18n("Network error."));
|
||
|
disconnect(true);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
// finishing CR/LF
|
||
|
if (!sendData(""))
|
||
|
return;
|
||
|
|
||
|
processedSize(bufLen);
|
||
|
|
||
|
infoMessage(i18n("Verifying upload completion..."));
|
||
|
|
||
|
if (operationSuccessful())
|
||
|
ksDebug() << "Script upload complete." << endl;
|
||
|
|
||
|
else {
|
||
|
/* The managesieve server parses received scripts and rejects
|
||
|
* scripts which are not syntactically correct. Here we expect
|
||
|
* to receive a message detailing the error (only the first
|
||
|
* error is reported. */
|
||
|
if (r.getAction().length() > 3) {
|
||
|
// make a copy of the extra info
|
||
14 years ago
|
TQCString extra = r.getAction().right(r.getAction().length() - 3);
|
||
15 years ago
|
|
||
|
// send the extra message off for re-processing
|
||
|
receiveData(false, &extra);
|
||
|
|
||
12 years ago
|
if (r.getType() == tdeio_sieveResponse::QUANTITY) {
|
||
15 years ago
|
// length of the error message
|
||
|
uint len = r.getQuantity();
|
||
|
|
||
14 years ago
|
TQCString errmsg(len + 1);
|
||
15 years ago
|
|
||
|
read(errmsg.data(), len);
|
||
|
|
||
|
error(ERR_INTERNAL_SERVER,
|
||
|
i18n("The script did not upload successfully.\n"
|
||
|
"This is probably due to errors in the script.\n"
|
||
13 years ago
|
"The server responded:\n%1").arg(TQString(errmsg)));
|
||
15 years ago
|
|
||
|
// clear the rest of the incoming data
|
||
|
receiveData();
|
||
12 years ago
|
} else if (r.getType() == tdeio_sieveResponse::KEY_VAL_PAIR) {
|
||
15 years ago
|
error(ERR_INTERNAL_SERVER,
|
||
|
i18n("The script did not upload successfully.\n"
|
||
|
"This is probably due to errors in the script.\n"
|
||
13 years ago
|
"The server responded:\n%1").arg(TQString(r.getKey())));
|
||
14 years ago
|
} else
|
||
15 years ago
|
error(ERR_INTERNAL_SERVER,
|
||
|
i18n("The script did not upload successfully.\n"
|
||
|
"The script may contain errors."));
|
||
|
} else
|
||
|
error(ERR_INTERNAL_SERVER,
|
||
|
i18n("The script did not upload successfully.\n"
|
||
|
"The script may contain errors."));
|
||
|
}
|
||
|
|
||
|
//if ( permissions != -1 )
|
||
|
// chmod( url, permissions );
|
||
|
|
||
|
infoMessage(i18n("Done."));
|
||
|
|
||
|
finished();
|
||
|
}
|
||
|
|
||
14 years ago
|
static void inplace_crlf2lf( TQByteArray & in ) {
|
||
15 years ago
|
if ( in.isEmpty() )
|
||
|
return;
|
||
14 years ago
|
TQByteArray & out = in; // inplace
|
||
15 years ago
|
const char * s = in.begin();
|
||
|
const char * const end = in.end();
|
||
|
char * d = out.begin();
|
||
|
char last = '\0';
|
||
|
while ( s < end ) {
|
||
|
if ( *s == '\n' && last == '\r' )
|
||
|
--d;
|
||
|
*d++ = last = *s++;
|
||
|
}
|
||
|
out.resize( d - out.begin() );
|
||
|
}
|
||
|
|
||
|
/* ---------------------------------------------------------------------------------- */
|
||
12 years ago
|
void tdeio_sieveProtocol::get(const KURL& url)
|
||
15 years ago
|
{
|
||
|
changeCheck( url );
|
||
|
if (!connect())
|
||
|
return;
|
||
|
|
||
|
infoMessage(i18n("Retrieving data..."));
|
||
|
|
||
14 years ago
|
TQString filename = url.fileName(false);
|
||
15 years ago
|
|
||
|
if (filename.isEmpty()) {
|
||
|
error(ERR_MALFORMED_URL, url.prettyURL());
|
||
|
return;
|
||
|
}
|
||
|
|
||
14 years ago
|
//SlaveBase::mimetype( TQString("text/plain") ); // "application/sieve");
|
||
15 years ago
|
|
||
|
if (!sendData("GETSCRIPT \"" + filename.utf8() + "\""))
|
||
|
return;
|
||
|
|
||
12 years ago
|
if (receiveData() && r.getType() == tdeio_sieveResponse::QUANTITY) {
|
||
15 years ago
|
// determine script size
|
||
|
ssize_t total_len = r.getQuantity();
|
||
|
totalSize( total_len );
|
||
|
|
||
|
int recv_len = 0;
|
||
|
do {
|
||
|
// wait for data...
|
||
|
if ( !waitForResponse( 600 ) ) {
|
||
12 years ago
|
error( TDEIO::ERR_SERVER_TIMEOUT, m_sServer );
|
||
15 years ago
|
disconnect( true );
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
// ...read data...
|
||
|
// Only read as much as we need, otherwise we slurp in the OK that
|
||
|
// operationSuccessful() is expecting below.
|
||
14 years ago
|
TQByteArray dat( kMin( total_len - recv_len, ssize_t(64 * 1024 )) );
|
||
15 years ago
|
ssize_t this_recv_len = read( dat.data(), dat.size() );
|
||
|
|
||
|
if ( this_recv_len < 1 && !isConnectionValid() ) {
|
||
12 years ago
|
error( TDEIO::ERR_CONNECTION_BROKEN, m_sServer );
|
||
15 years ago
|
disconnect( true );
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
dat.resize( this_recv_len );
|
||
|
inplace_crlf2lf( dat );
|
||
|
// send data to slaveinterface
|
||
|
data( dat );
|
||
|
|
||
|
recv_len += this_recv_len;
|
||
|
processedSize( recv_len );
|
||
|
} while ( recv_len < total_len );
|
||
|
|
||
|
infoMessage(i18n("Finishing up...") );
|
||
14 years ago
|
data(TQByteArray());
|
||
15 years ago
|
|
||
|
if (operationSuccessful())
|
||
|
ksDebug() << "Script retrieval complete." << endl;
|
||
|
else
|
||
|
ksDebug() << "Script retrieval failed." << endl;
|
||
|
} else {
|
||
|
error(ERR_UNSUPPORTED_PROTOCOL, i18n("A protocol error occurred "
|
||
|
"while trying to negotiate script downloading."));
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
infoMessage(i18n("Done."));
|
||
|
finished();
|
||
|
}
|
||
|
|
||
12 years ago
|
void tdeio_sieveProtocol::del(const KURL &url, bool isfile)
|
||
15 years ago
|
{
|
||
|
if (!isfile) {
|
||
|
error(ERR_INTERNAL, i18n("Folders are not supported."));
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
changeCheck( url );
|
||
|
if (!connect())
|
||
|
return;
|
||
|
|
||
|
infoMessage(i18n("Deleting file..."));
|
||
|
|
||
14 years ago
|
TQString filename = url.fileName(false);
|
||
15 years ago
|
|
||
|
if (filename.isEmpty()) {
|
||
|
error(ERR_MALFORMED_URL, url.prettyURL());
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (!sendData("DELETESCRIPT \"" + filename.utf8() + "\""))
|
||
|
return;
|
||
|
|
||
|
if (operationSuccessful())
|
||
|
ksDebug() << "Script deletion successful." << endl;
|
||
|
else {
|
||
|
error(ERR_INTERNAL_SERVER, i18n("The server would not delete the file."));
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
infoMessage(i18n("Done."));
|
||
|
|
||
|
finished();
|
||
|
}
|
||
|
|
||
12 years ago
|
void tdeio_sieveProtocol::chmod(const KURL& url, int permissions)
|
||
15 years ago
|
{
|
||
|
switch ( permissions ) {
|
||
|
case 0700: // activate
|
||
|
activate(url);
|
||
|
break;
|
||
|
case 0600: // deactivate
|
||
|
deactivate();
|
||
|
break;
|
||
|
default: // unsupported
|
||
|
error(ERR_CANNOT_CHMOD, i18n("Cannot chmod to anything but 0700 (active) or 0600 (inactive script)."));
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
finished();
|
||
|
}
|
||
|
|
||
|
#if defined(_AIX) && defined(stat)
|
||
|
#undef stat
|
||
|
#endif
|
||
|
|
||
12 years ago
|
void tdeio_sieveProtocol::stat(const KURL& url)
|
||
15 years ago
|
{
|
||
|
changeCheck( url );
|
||
|
if (!connect())
|
||
|
return;
|
||
|
|
||
|
UDSEntry entry;
|
||
|
|
||
14 years ago
|
TQString filename = url.fileName(false);
|
||
15 years ago
|
|
||
|
if (filename.isEmpty()) {
|
||
|
UDSAtom atom;
|
||
12 years ago
|
atom.m_uds = TDEIO::UDS_NAME;
|
||
15 years ago
|
atom.m_str = "/";
|
||
|
entry.append(atom);
|
||
|
|
||
12 years ago
|
atom.m_uds = TDEIO::UDS_FILE_TYPE;
|
||
15 years ago
|
atom.m_long = S_IFDIR;
|
||
|
entry.append(atom);
|
||
|
|
||
12 years ago
|
atom.m_uds = TDEIO::UDS_ACCESS;
|
||
15 years ago
|
atom.m_long = 0700;
|
||
|
entry.append(atom);
|
||
|
|
||
|
statEntry(entry);
|
||
|
|
||
|
} else {
|
||
|
if (!sendData("LISTSCRIPTS"))
|
||
|
return;
|
||
|
|
||
|
while(receiveData()) {
|
||
12 years ago
|
if (r.getType() == tdeio_sieveResponse::ACTION) {
|
||
13 years ago
|
if (r.getAction().contains("OK", false) == 1)
|
||
15 years ago
|
// Script list completed
|
||
|
break;
|
||
|
|
||
|
} else
|
||
14 years ago
|
if (filename == TQString::fromUtf8(r.getKey())) {
|
||
15 years ago
|
entry.clear();
|
||
|
|
||
|
UDSAtom atom;
|
||
12 years ago
|
atom.m_uds = TDEIO::UDS_NAME;
|
||
14 years ago
|
atom.m_str = TQString::fromUtf8(r.getKey());
|
||
15 years ago
|
entry.append(atom);
|
||
|
|
||
12 years ago
|
atom.m_uds = TDEIO::UDS_FILE_TYPE;
|
||
15 years ago
|
atom.m_long = S_IFREG;
|
||
|
entry.append(atom);
|
||
|
|
||
12 years ago
|
atom.m_uds = TDEIO::UDS_ACCESS;
|
||
15 years ago
|
if ( r.getExtra() == "ACTIVE" )
|
||
|
atom.m_long = 0700; // mark exec'able
|
||
|
else
|
||
|
atom.m_long = 0600;
|
||
|
entry.append(atom);
|
||
|
|
||
12 years ago
|
atom.m_uds = TDEIO::UDS_MIME_TYPE;
|
||
15 years ago
|
atom.m_str = "application/sieve";
|
||
|
entry.append(atom);
|
||
|
|
||
|
//setMetaData("active", (r.getExtra() == "ACTIVE") ? "yes" : "no");
|
||
|
|
||
|
statEntry(entry);
|
||
|
// cannot break here because we need to clear
|
||
|
// the rest of the incoming data.
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
finished();
|
||
|
}
|
||
|
|
||
12 years ago
|
void tdeio_sieveProtocol::listDir(const KURL& url)
|
||
15 years ago
|
{
|
||
|
changeCheck( url );
|
||
|
if (!connect())
|
||
|
return;
|
||
|
|
||
|
if (!sendData("LISTSCRIPTS"))
|
||
|
return;
|
||
|
|
||
|
UDSEntry entry;
|
||
|
|
||
|
while(receiveData()) {
|
||
12 years ago
|
if (r.getType() == tdeio_sieveResponse::ACTION) {
|
||
13 years ago
|
if (r.getAction().contains("OK", false) == 1)
|
||
15 years ago
|
// Script list completed.
|
||
|
break;
|
||
|
|
||
|
} else {
|
||
|
entry.clear();
|
||
|
|
||
|
UDSAtom atom;
|
||
12 years ago
|
atom.m_uds = TDEIO::UDS_NAME;
|
||
14 years ago
|
atom.m_str = TQString::fromUtf8(r.getKey());
|
||
15 years ago
|
entry.append(atom);
|
||
|
|
||
12 years ago
|
atom.m_uds = TDEIO::UDS_FILE_TYPE;
|
||
15 years ago
|
atom.m_long = S_IFREG;
|
||
|
entry.append(atom);
|
||
|
|
||
12 years ago
|
atom.m_uds = TDEIO::UDS_ACCESS;
|
||
15 years ago
|
if ( r.getExtra() == "ACTIVE" )
|
||
|
atom.m_long = 0700; // mark exec'able
|
||
|
else
|
||
|
atom.m_long = 0600;
|
||
|
entry.append(atom);
|
||
|
|
||
12 years ago
|
atom.m_uds = TDEIO::UDS_MIME_TYPE;
|
||
15 years ago
|
atom.m_str = "application/sieve";
|
||
|
entry.append(atom);
|
||
|
|
||
|
//asetMetaData("active", (r.getExtra() == "ACTIVE") ? "true" : "false");
|
||
|
|
||
|
ksDebug() << "Listing script " << r.getKey() << endl;
|
||
|
listEntry(entry , false);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
listEntry(entry, true);
|
||
|
|
||
|
finished();
|
||
|
}
|
||
|
|
||
|
/* ---------------------------------------------------------------------------------- */
|
||
12 years ago
|
bool tdeio_sieveProtocol::saslInteract( void *in, AuthInfo &ai )
|
||
15 years ago
|
{
|
||
|
ksDebug() << "sasl_interact" << endl;
|
||
|
sasl_interact_t *interact = ( sasl_interact_t * ) in;
|
||
|
|
||
|
//some mechanisms do not require username && pass, so it doesn't need a popup
|
||
|
//window for getting this info
|
||
|
for ( ; interact->id != SASL_CB_LIST_END; interact++ ) {
|
||
|
if ( interact->id == SASL_CB_AUTHNAME ||
|
||
|
interact->id == SASL_CB_PASS ) {
|
||
|
|
||
|
if (m_sUser.isEmpty() || m_sPass.isEmpty()) {
|
||
|
if (!openPassDlg(ai)) {
|
||
|
error(ERR_ABORTED, i18n("No authentication details supplied."));
|
||
|
return false;
|
||
|
}
|
||
|
m_sUser = ai.username;
|
||
|
m_sPass = ai.password;
|
||
|
}
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
interact = ( sasl_interact_t * ) in;
|
||
|
while( interact->id != SASL_CB_LIST_END ) {
|
||
|
ksDebug() << "SASL_INTERACT id: " << interact->id << endl;
|
||
|
switch( interact->id ) {
|
||
|
case SASL_CB_USER:
|
||
|
case SASL_CB_AUTHNAME:
|
||
|
ksDebug() << "SASL_CB_[AUTHNAME|USER]: '" << m_sUser << "'" << endl;
|
||
|
interact->result = strdup( m_sUser.utf8() );
|
||
|
interact->len = strlen( (const char *) interact->result );
|
||
|
break;
|
||
|
case SASL_CB_PASS:
|
||
|
ksDebug() << "SASL_CB_PASS: [hidden] " << endl;
|
||
|
interact->result = strdup( m_sPass.utf8() );
|
||
|
interact->len = strlen( (const char *) interact->result );
|
||
|
break;
|
||
|
default:
|
||
|
interact->result = NULL; interact->len = 0;
|
||
|
break;
|
||
|
}
|
||
|
interact++;
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
13 years ago
|
#define SASLERROR error(ERR_COULD_NOT_AUTHENTICATE, i18n("An error occurred during authentication: %1").arg( \
|
||
14 years ago
|
TQString::fromUtf8( sasl_errdetail( conn ) )));
|
||
15 years ago
|
|
||
12 years ago
|
bool tdeio_sieveProtocol::authenticate()
|
||
15 years ago
|
{
|
||
|
int result;
|
||
|
sasl_conn_t *conn = NULL;
|
||
|
sasl_interact_t *client_interact = NULL;
|
||
|
const char *out = NULL;
|
||
|
uint outlen;
|
||
|
const char *mechusing = NULL;
|
||
14 years ago
|
TQByteArray challenge, tmp;
|
||
15 years ago
|
|
||
|
/* Retrieve authentication details from user.
|
||
|
* Note: should this require realm as well as user & pass details
|
||
|
* before it automatically skips the prompt?
|
||
|
* Note2: encoding issues with PLAIN login? */
|
||
|
AuthInfo ai;
|
||
|
ai.url.setProtocol("sieve");
|
||
|
ai.url.setHost(m_sServer);
|
||
|
ai.url.setPort(m_iPort);
|
||
|
ai.username = m_sUser;
|
||
|
ai.password = m_sPass;
|
||
|
ai.keepPassword = true;
|
||
|
ai.caption = i18n("Sieve Authentication Details");
|
||
|
ai.comment = i18n("Please enter your authentication details for your sieve account "
|
||
|
"(usually the same as your email password):");
|
||
|
|
||
|
result = sasl_client_new( "sieve",
|
||
|
m_sServer.latin1(),
|
||
|
0, 0, callbacks, 0, &conn );
|
||
|
|
||
|
if ( result != SASL_OK ) {
|
||
|
ksDebug() << "sasl_client_new failed with: " << result << endl;
|
||
|
SASLERROR
|
||
|
return false;
|
||
|
}
|
||
|
|
||
14 years ago
|
TQStringList strList;
|
||
15 years ago
|
// strList.append("NTLM");
|
||
|
|
||
|
if ( !m_sAuth.isEmpty() )
|
||
|
strList.append( m_sAuth );
|
||
|
else
|
||
|
strList = m_sasl_caps;
|
||
|
|
||
|
do {
|
||
|
result = sasl_client_start(conn, strList.join(" ").latin1(), &client_interact,
|
||
|
&out, &outlen, &mechusing);
|
||
|
|
||
|
if (result == SASL_INTERACT)
|
||
|
if ( !saslInteract( client_interact, ai ) ) {
|
||
|
sasl_dispose( &conn );
|
||
|
return false;
|
||
|
};
|
||
|
} while ( result == SASL_INTERACT );
|
||
|
|
||
|
if ( result != SASL_CONTINUE && result != SASL_OK ) {
|
||
|
ksDebug() << "sasl_client_start failed with: " << result << endl;
|
||
|
SASLERROR
|
||
|
sasl_dispose( &conn );
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
ksDebug() << "Preferred authentication method is " << mechusing << "." << endl;
|
||
|
|
||
13 years ago
|
TQString firstCommand = "AUTHENTICATE \"" + TQString::fromLatin1( mechusing ) + "\"";
|
||
15 years ago
|
tmp.setRawData( out, outlen );
|
||
|
KCodecs::base64Encode( tmp, challenge );
|
||
|
tmp.resetRawData( out, outlen );
|
||
|
if ( !challenge.isEmpty() ) {
|
||
|
firstCommand += " \"";
|
||
13 years ago
|
firstCommand += TQString::fromLatin1( challenge.data(), challenge.size() );
|
||
15 years ago
|
firstCommand += "\"";
|
||
|
}
|
||
|
|
||
|
if (!sendData( firstCommand.latin1() ))
|
||
|
return false;
|
||
|
|
||
14 years ago
|
TQCString command;
|
||
15 years ago
|
|
||
|
do {
|
||
|
receiveData();
|
||
|
|
||
|
if (operationResult() != OTHER)
|
||
|
break;
|
||
|
|
||
|
ksDebug() << "Challenge len " << r.getQuantity() << endl;
|
||
|
|
||
12 years ago
|
if (r.getType() != tdeio_sieveResponse::QUANTITY) {
|
||
15 years ago
|
sasl_dispose( &conn );
|
||
|
error(ERR_SLAVE_DEFINED,
|
||
|
i18n("A protocol error occurred during authentication.\n"
|
||
13 years ago
|
"Choose a different authentication method to %1.").arg(mechusing));
|
||
15 years ago
|
return false;
|
||
|
}
|
||
|
|
||
|
uint qty = r.getQuantity();
|
||
|
|
||
|
receiveData();
|
||
|
|
||
12 years ago
|
if (r.getType() != tdeio_sieveResponse::ACTION && r.getAction().length() != qty) {
|
||
15 years ago
|
sasl_dispose( &conn );
|
||
|
error(ERR_UNSUPPORTED_PROTOCOL,
|
||
|
i18n("A protocol error occurred during authentication.\n"
|
||
13 years ago
|
"Choose a different authentication method to %1.").arg(mechusing));
|
||
15 years ago
|
return false;
|
||
|
}
|
||
|
|
||
|
tmp.setRawData( r.getAction().data(), qty );
|
||
|
KCodecs::base64Decode( tmp, challenge );
|
||
|
tmp.resetRawData( r.getAction().data(), qty );
|
||
|
// ksDebug() << "S: [" << r.getAction() << "]." << endl;
|
||
14 years ago
|
// ksDebug() << "S-1: [" << TQCString(challenge.data(), challenge.size()+1) << "]." << endl;
|
||
15 years ago
|
|
||
|
do {
|
||
|
result = sasl_client_step(conn, challenge.isEmpty() ? 0 : challenge.data(),
|
||
|
challenge.size(),
|
||
|
&client_interact,
|
||
|
&out, &outlen);
|
||
|
|
||
|
if (result == SASL_INTERACT)
|
||
|
if ( !saslInteract( client_interact, ai ) ) {
|
||
|
sasl_dispose( &conn );
|
||
|
return false;
|
||
|
};
|
||
|
} while ( result == SASL_INTERACT );
|
||
|
|
||
|
ksDebug() << "sasl_client_step: " << result << endl;
|
||
|
if ( result != SASL_CONTINUE && result != SASL_OK ) {
|
||
|
ksDebug() << "sasl_client_step failed with: " << result << endl;
|
||
|
SASLERROR
|
||
|
sasl_dispose( &conn );
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
tmp.setRawData( out, outlen );
|
||
|
KCodecs::base64Encode( tmp, challenge );
|
||
|
tmp.resetRawData( out, outlen );
|
||
14 years ago
|
sendData("\"" + TQCString( challenge.data(), challenge.size()+1 ) + "\"");
|
||
|
// ksDebug() << "C: [" << TQCString(challenge.data(), challenge.size()+1) << "]." << endl;
|
||
15 years ago
|
// ksDebug() << "C-1: [" << out << "]." << endl;
|
||
|
} while ( true );
|
||
|
|
||
|
ksDebug() << "Challenges finished." << endl;
|
||
|
sasl_dispose( &conn );
|
||
|
|
||
|
if (operationResult() == OK) {
|
||
|
// Authentication succeeded.
|
||
|
return true;
|
||
|
} else {
|
||
|
// Authentication failed.
|
||
13 years ago
|
error(ERR_COULD_NOT_AUTHENTICATE, i18n("Authentication failed.\nMost likely the password is wrong.\nThe server responded:\n%1").arg( TQString(r.getAction()) ) );
|
||
15 years ago
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/* --------------------------------------------------------------------------- */
|
||
12 years ago
|
void tdeio_sieveProtocol::mimetype(const KURL & url)
|
||
15 years ago
|
{
|
||
|
ksDebug() << "Requesting mimetype for " << url.prettyURL() << endl;
|
||
|
|
||
|
if (url.fileName(false).isEmpty())
|
||
|
mimeType( "inode/directory" );
|
||
|
else
|
||
|
mimeType( "application/sieve" );
|
||
|
|
||
|
finished();
|
||
|
}
|
||
|
|
||
|
|
||
|
/* --------------------------------------------------------------------------- */
|
||
12 years ago
|
bool tdeio_sieveProtocol::sendData(const TQCString &data)
|
||
15 years ago
|
{
|
||
14 years ago
|
TQCString write_buf = data + "\r\n";
|
||
15 years ago
|
|
||
|
//ksDebug() << "C: " << data << endl;
|
||
|
|
||
|
// Write the command
|
||
|
ssize_t write_buf_len = write_buf.length();
|
||
|
if (write(write_buf.data(), write_buf_len) != write_buf_len) {
|
||
|
error(ERR_COULD_NOT_WRITE, i18n("Network error."));
|
||
|
disconnect(true);
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
/* --------------------------------------------------------------------------- */
|
||
12 years ago
|
bool tdeio_sieveProtocol::receiveData(bool waitForData, TQCString *reparse)
|
||
15 years ago
|
{
|
||
14 years ago
|
TQCString interpret;
|
||
15 years ago
|
int start, end;
|
||
|
|
||
|
if (!reparse) {
|
||
|
if (!waitForData)
|
||
|
// is there data waiting?
|
||
|
if (atEnd()) return false;
|
||
|
|
||
|
// read data from the server
|
||
|
char buffer[SIEVE_DEFAULT_RECIEVE_BUFFER];
|
||
|
readLine(buffer, SIEVE_DEFAULT_RECIEVE_BUFFER - 1);
|
||
|
buffer[SIEVE_DEFAULT_RECIEVE_BUFFER-1] = '\0';
|
||
|
|
||
|
// strip LF/CR
|
||
14 years ago
|
interpret = TQCString(buffer).left(tqstrlen(buffer) - 2);
|
||
15 years ago
|
|
||
|
} else {
|
||
|
interpret = reparse->copy();
|
||
|
}
|
||
|
|
||
|
r.clear();
|
||
|
|
||
|
//ksDebug() << "S: " << interpret << endl;
|
||
|
|
||
|
switch(interpret[0]) {
|
||
|
case '{':
|
||
|
{
|
||
|
// expecting {quantity}
|
||
|
start = 0;
|
||
13 years ago
|
end = interpret.find("+}", start + 1);
|
||
15 years ago
|
// some older versions of Cyrus enclose the literal size just in { } instead of { +}
|
||
|
if ( end == -1 )
|
||
13 years ago
|
end = interpret.find('}', start + 1);
|
||
15 years ago
|
|
||
|
bool ok = false;
|
||
|
r.setQuantity(interpret.mid(start + 1, end - start - 1).toUInt( &ok ));
|
||
|
if (!ok) {
|
||
|
disconnect();
|
||
|
error(ERR_INTERNAL_SERVER, i18n("A protocol error occurred."));
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
case '"':
|
||
|
// expecting "key" "value" pairs
|
||
|
break;
|
||
|
default:
|
||
|
// expecting single string
|
||
|
r.setAction(interpret);
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
start = 0;
|
||
|
|
||
13 years ago
|
end = interpret.find(34, start + 1);
|
||
15 years ago
|
if (end == -1) {
|
||
|
ksDebug() << "Possible insufficient buffer size." << endl;
|
||
|
r.setKey(interpret.right(interpret.length() - start));
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
r.setKey(interpret.mid(start + 1, end - start - 1));
|
||
|
|
||
13 years ago
|
start = interpret.find(34, end + 1);
|
||
15 years ago
|
if (start == -1) {
|
||
|
if ((int)interpret.length() > end)
|
||
|
// skip " and space
|
||
|
r.setExtra(interpret.right(interpret.length() - end - 2));
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
13 years ago
|
end = interpret.find(34, start + 1);
|
||
15 years ago
|
if (end == -1) {
|
||
|
ksDebug() << "Possible insufficient buffer size." << endl;
|
||
|
r.setVal(interpret.right(interpret.length() - start));
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
r.setVal(interpret.mid(start + 1, end - start - 1));
|
||
|
return true;
|
||
|
}
|
||
|
|
||
12 years ago
|
bool tdeio_sieveProtocol::operationSuccessful()
|
||
15 years ago
|
{
|
||
|
while (receiveData(false)) {
|
||
12 years ago
|
if (r.getType() == tdeio_sieveResponse::ACTION) {
|
||
14 years ago
|
TQCString response = r.getAction().left(2);
|
||
15 years ago
|
if (response == "OK") {
|
||
|
return true;
|
||
|
} else if (response == "NO") {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
12 years ago
|
int tdeio_sieveProtocol::operationResult()
|
||
15 years ago
|
{
|
||
12 years ago
|
if (r.getType() == tdeio_sieveResponse::ACTION) {
|
||
14 years ago
|
TQCString response = r.getAction().left(2);
|
||
15 years ago
|
if (response == "OK") {
|
||
|
return OK;
|
||
|
} else if (response == "NO") {
|
||
|
return NO;
|
||
|
} else if (response == "BY"/*E*/) {
|
||
|
return BYE;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return OTHER;
|
||
|
}
|
||
|
|
||
12 years ago
|
bool tdeio_sieveProtocol::requestCapabilitiesAfterStartTLS() const
|
||
15 years ago
|
{
|
||
|
// Cyrus didn't send CAPABILITIES after STARTTLS until 2.3.11, which is
|
||
|
// not standard conform, but we need to support that anyway.
|
||
|
// m_implementation looks like this 'Cyrus timsieved v2.2.12' for Cyrus btw.
|
||
14 years ago
|
TQRegExp regExp( "Cyrus\\stimsieved\\sv(\\d+)\\.(\\d+)\\.(\\d+)([-\\w]*)", false );
|
||
15 years ago
|
if ( regExp.search( m_implementation ) >= 0 ) {
|
||
|
const int major = regExp.cap( 1 ).toInt();
|
||
|
const int minor = regExp.cap( 2 ).toInt();
|
||
|
const int patch = regExp.cap( 3 ).toInt();
|
||
14 years ago
|
const TQString vendor = regExp.cap( 4 );
|
||
15 years ago
|
if ( major < 2 || (major == 2 && (minor < 3 || (minor == 3 && patch < 11))) || (vendor == "-kolab-nocaps") ) {
|
||
|
ksDebug() << k_funcinfo << "Enabling compat mode for Cyrus < 2.3.11 or Cyrus marked as \"kolab-nocaps\"" << endl;
|
||
|
return true;
|
||
|
}
|
||
|
}
|
||
|
return false;
|
||
|
}
|