author | alwin <alwin> | 2004-02-12 22:24:30 (UTC) |
---|---|---|
committer | alwin <alwin> | 2004-02-12 22:24:30 (UTC) |
commit | c7f102b944a5172ca330dfc3c0f83cf0b9c65100 (patch) (side-by-side diff) | |
tree | b4bdfea3a7eeb6a46f949cfdaa7052ac25e1c128 | |
parent | 596d87e16d593cbc52d510973ece99ffe26cd4c4 (diff) | |
download | opie-c7f102b944a5172ca330dfc3c0f83cf0b9c65100.zip opie-c7f102b944a5172ca330dfc3c0f83cf0b9c65100.tar.gz opie-c7f102b944a5172ca330dfc3c0f83cf0b9c65100.tar.bz2 |
TLS support for SMTP
-rw-r--r-- | noncore/net/mail/libmailwrapper/smtpwrapper.cpp | 55 | ||||
-rw-r--r-- | noncore/net/mail/libmailwrapper/smtpwrapper.h | 13 |
2 files changed, 60 insertions, 8 deletions
diff --git a/noncore/net/mail/libmailwrapper/smtpwrapper.cpp b/noncore/net/mail/libmailwrapper/smtpwrapper.cpp index e8db9ca..3ab6b77 100644 --- a/noncore/net/mail/libmailwrapper/smtpwrapper.cpp +++ b/noncore/net/mail/libmailwrapper/smtpwrapper.cpp @@ -50,24 +50,26 @@ QString SMTPwrapper::mailsmtpError( int errnum ) { case MAILSMTP_ERROR_STREAM: return tr( "Stream error" ); case MAILSMTP_ERROR_HOSTNAME: return tr( "gethostname() failed" ); case MAILSMTP_ERROR_NOT_IMPLEMENTED: return tr( "Not implemented" ); case MAILSMTP_ERROR_ACTION_NOT_TAKEN: return tr( "Error, action not taken" ); case MAILSMTP_ERROR_EXCEED_STORAGE_ALLOCATION: return tr( "Data exceeds storage allocation" ); case MAILSMTP_ERROR_IN_PROCESSING: return tr( "Error in processing" ); + case MAILSMTP_ERROR_STARTTLS_NOT_SUPPORTED: + return tr( "Starttls not supported" ); // case MAILSMTP_ERROR_INSUFFISANT_SYSTEM_STORAGE: // return tr( "Insufficient system storage" ); case MAILSMTP_ERROR_MAILBOX_UNAVAILABLE: return tr( "Mailbox unavailable" ); case MAILSMTP_ERROR_MAILBOX_NAME_NOT_ALLOWED: return tr( "Mailbox name not allowed" ); case MAILSMTP_ERROR_BAD_SEQUENCE_OF_COMMAND: return tr( "Bad command sequence" ); case MAILSMTP_ERROR_USER_NOT_LOCAL: return tr( "User not local" ); case MAILSMTP_ERROR_TRANSACTION_FAILED: return tr( "Transaction failed" ); @@ -556,42 +558,70 @@ void SMTPwrapper::smtpSend( mailmime *mail,bool later, SMTPaccount *smtp ) { void SMTPwrapper::storeFailedMail(const char*data,unsigned int size, const char*failuremessage) { if (data) { storeMail(data,size,"Sendfailed"); } if (failuremessage) { QMessageBox::critical(0,tr("Error sending mail"), tr("<center>%1</center>").arg(failuremessage)); } } +int SMTPwrapper::start_smtp_tls(mailsmtp *session) +{ + if (!session) { + return MAILSMTP_ERROR_IN_PROCESSING; + } + int err = mailesmtp_starttls(session); + if (err != MAILSMTP_NO_ERROR) return err; + mailstream_low * low; + mailstream_low * new_low; + low = mailstream_get_low(session->stream); + if (!low) { + return MAILSMTP_ERROR_IN_PROCESSING; + } + int fd = mailstream_low_get_fd(low); + if (fd > -1 && (new_low = mailstream_low_ssl_open(fd))!=0) { + mailstream_low_free(low); + mailstream_set_low(session->stream, new_low); + } else { + return MAILSMTP_ERROR_IN_PROCESSING; + } + return err; +} + int SMTPwrapper::smtpSend(char*from,clist*rcpts,const char*data,size_t size, SMTPaccount *smtp ) { const char *server, *user, *pass; bool ssl; uint16_t port; mailsmtp *session; int err,result; QString failuretext = ""; result = 1; server = user = pass = 0; server = smtp->getServer().latin1(); // FIXME: currently only TLS and Plain work. ssl = false; + bool try_tls = true; + bool force_tls=false; if ( smtp->ConnectionType() == 2 ) { ssl = true; + try_tls = false; + } else if (smtp->ConnectionType() == 1) { + force_tls = true; } port = smtp->getPort().toUInt(); session = mailsmtp_new( 20, &progress ); if ( session == NULL ) { /* no failure message cause this happens when problems with memory - than we we can not display any messagebox */ return 0; } qDebug( "Servername %s at port %i", server, port ); @@ -599,32 +629,47 @@ int SMTPwrapper::smtpSend(char*from,clist*rcpts,const char*data,size_t size, SMT qDebug( "SSL session" ); err = mailsmtp_ssl_connect( session, server, port ); } else { qDebug( "No SSL session" ); err = mailsmtp_socket_connect( session, server, port ); } if ( err != MAILSMTP_NO_ERROR ) { qDebug("Error init connection"); failuretext = tr("Error init SMTP connection: %1").arg(mailsmtpError(err)); result = 0; } + /* switch to tls after init 'cause there it will send the ehlo */ if (result) { err = mailsmtp_init( session ); if (err != MAILSMTP_NO_ERROR) { result = 0; failuretext = tr("Error init SMTP connection: %1").arg(mailsmtpError(err)); } } + if (try_tls) { + err = start_smtp_tls(session); + if (err != MAILSMTP_NO_ERROR) { + try_tls = false; + } else { + err = mailesmtp_ehlo(session); + } + } + + if (!try_tls && force_tls) { + result = 0; + failuretext = tr("Error init SMTP tls: %1").arg(mailsmtpError(err)); + } + if (result==1 && smtp->getLogin() ) { qDebug("smtp with auth"); if ( smtp->getUser().isEmpty() || smtp->getPassword().isEmpty() ) { // get'em LoginDialog login( smtp->getUser(), smtp->getPassword(), NULL, 0, true ); login.show(); if ( QDialog::Accepted == login.exec() ) { // ok user = login.getUser().latin1(); pass = login.getPassword().latin1(); } else { result = 0; @@ -728,64 +773,68 @@ int SMTPwrapper::sendQueuedMail(AbstractMail*wrap,SMTPaccount*smtp,RecMail*which free(from); } if (rcpts) { smtp_address_list_free( rcpts ); } return res; } /* this is a special fun */ bool SMTPwrapper::flushOutbox(SMTPaccount*smtp) { bool returnValue = true; - if (!smtp) + qDebug("Sending the queue"); + if (!smtp) { + qDebug("No smtp account given"); return false; + } bool reset_user_value = false; QString localfolders = AbstractMail::defaultLocalfolder(); AbstractMail*wrap = AbstractMail::getWrapper(localfolders); if (!wrap) { qDebug("memory error"); return false; } QString oldPw, oldUser; QList<RecMail> mailsToSend; QList<RecMail> mailsToRemove; QString mbox("Outgoing"); wrap->listMessages(mbox,mailsToSend); if (mailsToSend.count()==0) { delete wrap; + qDebug("No mails to send"); return false; } - + oldPw = smtp->getPassword(); oldUser = smtp->getUser(); if (smtp->getLogin() && (smtp->getUser().isEmpty() || smtp->getPassword().isEmpty()) ) { // get'em QString user,pass; LoginDialog login( smtp->getUser(), smtp->getPassword(), NULL, 0, true ); login.show(); if ( QDialog::Accepted == login.exec() ) { // ok user = login.getUser().latin1(); pass = login.getPassword().latin1(); reset_user_value = true; smtp->setUser(user); smtp->setPassword(pass); } else { return true; } } - + mailsToSend.setAutoDelete(false); sendProgress = new progressMailSend(); sendProgress->show(); sendProgress->setMaxMails(mailsToSend.count()); while (mailsToSend.count()>0) { if (sendQueuedMail(wrap,smtp,mailsToSend.at(0))==0) { QMessageBox::critical(0,tr("Error sending mail"), tr("Error sending queued mail - breaking")); returnValue = false; break; } diff --git a/noncore/net/mail/libmailwrapper/smtpwrapper.h b/noncore/net/mail/libmailwrapper/smtpwrapper.h index 89826d9..75e4891 100644 --- a/noncore/net/mail/libmailwrapper/smtpwrapper.h +++ b/noncore/net/mail/libmailwrapper/smtpwrapper.h @@ -10,64 +10,67 @@ #include "settings.h" class Mail; class AbstractMail; class RecMail; class Attachment; struct mailimf_fields; struct mailimf_field; struct mailimf_mailbox; struct mailmime; struct mailimf_address_list; class progressMailSend; +struct mailsmtp; class SMTPwrapper : public QObject { Q_OBJECT public: - SMTPwrapper( Settings *s ); + SMTPwrapper( Settings *s ); virtual ~SMTPwrapper(){} void sendMail(const Mail& mail,SMTPaccount*smtp,bool later=false ); bool flushOutbox(SMTPaccount*smtp); static progressMailSend*sendProgress; signals: - void queuedMails( int ); + void queuedMails( int ); protected: mailimf_mailbox *newMailbox(const QString&name,const QString&mail ); mailimf_fields *createImfFields(const Mail &mail ); mailmime *createMimeMail(const Mail&mail ); - + mailimf_address_list *parseAddresses(const QString&addr ); void addFileParts( mailmime *message,const QList<Attachment>&files ); mailmime *buildTxtPart(const QString&str ); mailmime *buildFilePart(const QString&filename,const QString&mimetype,const QString&content); void smtpSend( mailmime *mail,bool later, SMTPaccount *smtp ); clist *createRcptList( mailimf_fields *fields ); - + static void storeMail(const char*mail, size_t length, const QString&box); static QString mailsmtpError( int err ); static void progress( size_t current, size_t maximum ); static void addRcpts( clist *list, mailimf_address_list *addr_list ); static char *getFrom( mailmime *mail ); static char *getFrom( mailimf_field *ffrom); static mailimf_field *getField( mailimf_fields *fields, int type ); + static int start_smtp_tls(mailsmtp *session); + int smtpSend(char*from,clist*rcpts,const char*data,size_t size, SMTPaccount *smtp ); void storeMail(mailmime*mail, const QString&box); Settings *settings; - + int sendQueuedMail(AbstractMail*wrap,SMTPaccount*smtp,RecMail*which); void storeFailedMail(const char*data,unsigned int size, const char*failuremessage); int m_queuedMail; static const char* USER_AGENT; protected slots: void emitQCop( int queued ); }; #endif |