| 1 | /* Copyright (C) 2015 Dan Chapman <[email protected]> |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or |
| 4 | modify it under the terms of the GNU General Public License as |
| 5 | published by the Free Software Foundation; either version 2 of |
| 6 | the License or (at your option) version 3 |
| 7 | |
| 8 | This program is distributed in the hope that it will be useful, |
| 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | GNU General Public License for more details. |
| 12 | |
| 13 | You should have received a copy of the GNU General Public License |
| 14 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | */ |
| 16 | #include "srvlookup.h" |
| 17 | #include <QStringBuilder> |
| 18 | #include <QDebug> |
| 19 | |
| 20 | SrvLookup::SrvLookup(QObject *parent, ServerConfiguration *config) : |
| 21 | QObject(parent), m_serverConfig(config), m_dns(new QDnsLookup), m_lookupType(INVALID) |
| 22 | { |
| 23 | connect(m_dns, &QDnsLookup::finished, this, &SrvLookup::handleLookUpResult); |
| 24 | } |
| 25 | |
| 26 | void SrvLookup::lookUp(const QString &domain) |
| 27 | { |
| 28 | if (domain.isEmpty()) { |
| 29 | emit failed(); |
| 30 | return; |
| 31 | } |
| 32 | m_dns->setType(QDnsLookup::SRV); |
| 33 | m_domain = domain; |
| 34 | // Ideally we always prefer IMAPS over IMAP even if it |
| 35 | // may get upgraded via STARTTLS. So start with this first. |
| 36 | m_lookupType = IMAPS; |
| 37 | buildNextRequest(); |
| 38 | } |
| 39 | |
| 40 | void SrvLookup::buildNextRequest() |
| 41 | { |
| 42 | switch (m_lookupType) { |
| 43 | case INVALID: |
| 44 | // Shouldn't be here |
| 45 | emit failed(); |
| 46 | return; |
| 47 | case IMAPS: |
| 48 | m_dns->setName(QString("_imaps._tcp." % m_domain)); |
| 49 | m_dns->lookup(); |
| 50 | break; |
| 51 | case IMAP: |
| 52 | m_dns->setName(QString("_imap._tcp." % m_domain)); |
| 53 | m_dns->lookup(); |
| 54 | break; |
| 55 | case SUBMISSION: |
| 56 | m_dns->setName(QString("_submission._tcp." % m_domain)); |
| 57 | m_dns->lookup(); |
| 58 | break; |
| 59 | } |
| 60 | qDebug() << "Looking for SRV record at: " << m_dns->name(); |
| 61 | } |
| 62 | |
| 63 | void SrvLookup::handleLookUpResult() |
| 64 | { |
| 65 | if (m_dns->error() != QDnsLookup::NoError) { |
| 66 | qDebug() << "SRV lookup failed: " << m_dns->errorString(); |
| 67 | switch (m_lookupType) { |
| 68 | case INVALID: |
| 69 | // Shouldn't be here |
| 70 | emit failed(); |
| 71 | return; |
| 72 | case IMAPS: |
| 73 | m_lookupType = IMAP; |
| 74 | buildNextRequest(); |
| 75 | return; |
| 76 | case IMAP: |
| 77 | // If we reached here then we failed to find any IMAP info |
| 78 | // So we need to just fail as we don't currently configure |
| 79 | // SMTP without IMAP info. |
| 80 | case SUBMISSION: |
| 81 | emit failed(); |
| 82 | return; |
| 83 | } |
| 84 | } |
| 85 | // Now deal with the received records |
| 86 | handleReceivedRecords(); |
| 87 | |
| 88 | // Determine the next step. |
| 89 | switch(m_lookupType) { |
| 90 | case INVALID: |
| 91 | emit failed(); |
| 92 | return; |
| 93 | case IMAPS: |
| 94 | case IMAP: |
| 95 | // We found details for IMAP so we want to jump to submission now |
| 96 | m_lookupType = SUBMISSION; |
| 97 | buildNextRequest(); |
| 98 | break; |
| 99 | case SUBMISSION: |
| 100 | // Were complete now |
| 101 | if (m_serverConfig->isValid()) { |
| 102 | emit success(m_serverConfig); |
| 103 | } else { |
| 104 | qWarning("Config isn't valid"); |
| 105 | emit failed(); |
| 106 | } |
| 107 | break; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | void SrvLookup::handleReceivedRecords() |
| 112 | { |
| 113 | if (m_dns->serviceRecords().size() == 1) { |
| 114 | addRecordToConfig(m_dns->serviceRecords().at(0)); |
| 115 | } else { |
| 116 | // rfc6186 section 3.4 states the record with the lowest priority value |
| 117 | // Is the one we should use. |
| 118 | int i = 0; |
| 119 | int idx = -1; |
| 120 | quint16 priority = -1; |
| 121 | |
| 122 | auto updateInterestingRecord = [&](const int &index, const quint16 &recordPriority) { |
| 123 | idx = index; |
| 124 | priority = recordPriority; |
| 125 | }; |
| 126 | |
| 127 | Q_FOREACH (const QDnsServiceRecord &record, m_dns->serviceRecords()) { |
| 128 | if (priority == -1) { |
| 129 | //this is first in the list so is the potential candidate |
| 130 | updateInterestingRecord(i, record.priority()); |
| 131 | } else { |
| 132 | if (record.priority() < priority) { |
| 133 | updateInterestingRecord(i, record.priority()); |
| 134 | } |
| 135 | } |
| 136 | ++i; |
| 137 | } |
| 138 | addRecordToConfig(m_dns->serviceRecords().at(idx)); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | |
| 143 | void SrvLookup::addRecordToConfig(const QDnsServiceRecord &record) |
| 144 | { |
| 145 | // Sort host & port first |
| 146 | switch (m_lookupType) { |
| 147 | case INVALID: |
| 148 | emit failed(); |
| 149 | return; |
| 150 | case IMAPS: |
| 151 | case IMAP: |
| 152 | m_serverConfig->setImapHost(record.target()); |
| 153 | m_serverConfig->setImapPort(record.port()); |
| 154 | if (m_lookupType == IMAPS) { |
| 155 | m_serverConfig->setImapMethod(ServerConfiguration::SSL_TLS); |
| 156 | } else { |
| 157 | // We assume STARTLS as it's very uncommon that it would |
| 158 | // be cleartext and a client usually decide that on the fact of the LOGINDISABLED |
| 159 | // capability being present |
| 160 | m_serverConfig->setImapMethod(ServerConfiguration::STARTTLS); |
| 161 | } |
| 162 | case SUBMISSION: |
| 163 | m_serverConfig->setSmtpHost(record.target()); |
| 164 | m_serverConfig->setSmtpPort(record.port()); |
| 165 | if (record.port() == 465) { |
| 166 | m_serverConfig->setSmtpMethod(ServerConfiguration::SSL_TLS); |
| 167 | } else { |
| 168 | m_serverConfig->setSmtpMethod(ServerConfiguration::STARTTLS); |
| 169 | } |
| 170 | } |
| 171 | } |