Search in sources :

Example 1 with PeekableInputStream

use of com.fsck.k9.mail.filter.PeekableInputStream in project k-9 by k9mail.

the class ImapConnection method setUpStreamsAndParser.

private void setUpStreamsAndParser(InputStream input, OutputStream output) {
    inputStream = new PeekableInputStream(new BufferedInputStream(input, BUFFER_SIZE));
    responseParser = new ImapResponseParser(inputStream);
    outputStream = new BufferedOutputStream(output, BUFFER_SIZE);
}
Also used : BufferedInputStream(java.io.BufferedInputStream) PeekableInputStream(com.fsck.k9.mail.filter.PeekableInputStream) BufferedOutputStream(java.io.BufferedOutputStream)

Example 2 with PeekableInputStream

use of com.fsck.k9.mail.filter.PeekableInputStream in project k-9 by k9mail.

the class ImapResponseParserTest method createParser.

private ImapResponseParser createParser(String response) {
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(response.getBytes());
    peekableInputStream = new PeekableInputStream(byteArrayInputStream);
    return new ImapResponseParser(peekableInputStream);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) PeekableInputStream(com.fsck.k9.mail.filter.PeekableInputStream)

Example 3 with PeekableInputStream

use of com.fsck.k9.mail.filter.PeekableInputStream in project k-9 by k9mail.

the class RealImapConnection method setUpStreamsAndParser.

private void setUpStreamsAndParser(InputStream input, OutputStream output) {
    inputStream = new PeekableInputStream(new BufferedInputStream(input, BUFFER_SIZE));
    responseParser = new ImapResponseParser(inputStream);
    outputStream = new BufferedOutputStream(output, BUFFER_SIZE);
}
Also used : BufferedInputStream(java.io.BufferedInputStream) PeekableInputStream(com.fsck.k9.mail.filter.PeekableInputStream) BufferedOutputStream(java.io.BufferedOutputStream)

Example 4 with PeekableInputStream

use of com.fsck.k9.mail.filter.PeekableInputStream in project k-9 by k9mail.

the class ImapResponseHelper method createImapResponse.

public static ImapResponse createImapResponse(String response) throws IOException {
    String input = response + "\r\n";
    PeekableInputStream inputStream = new PeekableInputStream(new ByteArrayInputStream(input.getBytes()));
    ImapResponseParser parser = new ImapResponseParser(inputStream);
    return parser.readResponse();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) PeekableInputStream(com.fsck.k9.mail.filter.PeekableInputStream)

Example 5 with PeekableInputStream

use of com.fsck.k9.mail.filter.PeekableInputStream in project k-9 by k9mail.

the class SmtpTransport method open.

@Override
public void open() throws MessagingException {
    try {
        boolean secureConnection = false;
        InetAddress[] addresses = InetAddress.getAllByName(host);
        for (int i = 0; i < addresses.length; i++) {
            try {
                SocketAddress socketAddress = new InetSocketAddress(addresses[i], port);
                if (connectionSecurity == ConnectionSecurity.SSL_TLS_REQUIRED) {
                    socket = trustedSocketFactory.createSocket(null, host, port, clientCertificateAlias);
                    socket.connect(socketAddress, SOCKET_CONNECT_TIMEOUT);
                    secureConnection = true;
                } else {
                    socket = new Socket();
                    socket.connect(socketAddress, SOCKET_CONNECT_TIMEOUT);
                }
            } catch (SocketException e) {
                if (i < (addresses.length - 1)) {
                    // there are still other addresses for that host to try
                    continue;
                }
                throw new MessagingException("Cannot connect to host", e);
            }
            // connection success
            break;
        }
        // RFC 1047
        socket.setSoTimeout(SOCKET_READ_TIMEOUT);
        inputStream = new PeekableInputStream(new BufferedInputStream(socket.getInputStream(), 1024));
        outputStream = new BufferedOutputStream(socket.getOutputStream(), 1024);
        // Eat the banner
        executeCommand(null);
        String hostnameToReportInHelo = buildHostnameToReport();
        Map<String, String> extensions = sendHello(hostnameToReportInHelo);
        is8bitEncodingAllowed = extensions.containsKey("8BITMIME");
        isEnhancedStatusCodesProvided = extensions.containsKey("ENHANCEDSTATUSCODES");
        isPipeliningSupported = extensions.containsKey("PIPELINING");
        if (connectionSecurity == ConnectionSecurity.STARTTLS_REQUIRED) {
            if (extensions.containsKey("STARTTLS")) {
                executeCommand("STARTTLS");
                socket = trustedSocketFactory.createSocket(socket, host, port, clientCertificateAlias);
                inputStream = new PeekableInputStream(new BufferedInputStream(socket.getInputStream(), 1024));
                outputStream = new BufferedOutputStream(socket.getOutputStream(), 1024);
                /*
                     * Now resend the EHLO. Required by RFC2487 Sec. 5.2, and more specifically,
                     * Exim.
                     */
                extensions = sendHello(hostnameToReportInHelo);
                secureConnection = true;
            } else {
                /*
                     * This exception triggers a "Certificate error"
                     * notification that takes the user to the incoming
                     * server settings for review. This might be needed if
                     * the account was configured with an obsolete
                     * "STARTTLS (if available)" setting.
                     */
                throw new CertificateValidationException("STARTTLS connection security not available");
            }
        }
        boolean authLoginSupported = false;
        boolean authPlainSupported = false;
        boolean authCramMD5Supported = false;
        boolean authExternalSupported = false;
        boolean authXoauth2Supported = false;
        if (extensions.containsKey("AUTH")) {
            List<String> saslMech = Arrays.asList(extensions.get("AUTH").split(" "));
            authLoginSupported = saslMech.contains("LOGIN");
            authPlainSupported = saslMech.contains("PLAIN");
            authCramMD5Supported = saslMech.contains("CRAM-MD5");
            authExternalSupported = saslMech.contains("EXTERNAL");
            authXoauth2Supported = saslMech.contains("XOAUTH2");
        }
        parseOptionalSizeValue(extensions);
        if (!TextUtils.isEmpty(username) && (!TextUtils.isEmpty(password) || AuthType.EXTERNAL == authType || AuthType.XOAUTH2 == authType)) {
            switch(authType) {
                /*
                 * LOGIN is an obsolete option which is unavailable to users,
                 * but it still may exist in a user's settings from a previous
                 * version, or it may have been imported.
                 */
                case LOGIN:
                case PLAIN:
                    // try saslAuthPlain first, because it supports UTF-8 explicitly
                    if (authPlainSupported) {
                        saslAuthPlain();
                    } else if (authLoginSupported) {
                        saslAuthLogin();
                    } else {
                        throw new MessagingException("Authentication methods SASL PLAIN and LOGIN are unavailable.");
                    }
                    break;
                case CRAM_MD5:
                    if (authCramMD5Supported) {
                        saslAuthCramMD5();
                    } else {
                        throw new MessagingException("Authentication method CRAM-MD5 is unavailable.");
                    }
                    break;
                case XOAUTH2:
                    if (authXoauth2Supported && oauthTokenProvider != null) {
                        saslXoauth2();
                    } else {
                        throw new MessagingException("Authentication method XOAUTH2 is unavailable.");
                    }
                    break;
                case EXTERNAL:
                    if (authExternalSupported) {
                        saslAuthExternal();
                    } else {
                        /*
                         * Some SMTP servers are known to provide no error
                         * indication when a client certificate fails to
                         * validate, other than to not offer the AUTH EXTERNAL
                         * capability.
                         *
                         * So, we treat it is an error to not offer AUTH
                         * EXTERNAL when using client certificates. That way, the
                         * user can be notified of a problem during account setup.
                         */
                        throw new CertificateValidationException(MissingCapability);
                    }
                    break;
                /*
                 * AUTOMATIC is an obsolete option which is unavailable to users,
                 * but it still may exist in a user's settings from a previous
                 * version, or it may have been imported.
                 */
                case AUTOMATIC:
                    if (secureConnection) {
                        // try saslAuthPlain first, because it supports UTF-8 explicitly
                        if (authPlainSupported) {
                            saslAuthPlain();
                        } else if (authLoginSupported) {
                            saslAuthLogin();
                        } else if (authCramMD5Supported) {
                            saslAuthCramMD5();
                        } else {
                            throw new MessagingException("No supported authentication methods available.");
                        }
                    } else {
                        if (authCramMD5Supported) {
                            saslAuthCramMD5();
                        } else {
                            /*
                             * We refuse to insecurely transmit the password
                             * using the obsolete AUTOMATIC setting because of
                             * the potential for a MITM attack. Affected users
                             * must choose a different setting.
                             */
                            throw new MessagingException("Update your outgoing server authentication setting. AUTOMATIC auth. is unavailable.");
                        }
                    }
                    break;
                default:
                    throw new MessagingException("Unhandled authentication method found in the server settings (bug).");
            }
        }
    } catch (MessagingException e) {
        close();
        throw e;
    } catch (SSLException e) {
        close();
        throw new CertificateValidationException(e.getMessage(), e);
    } catch (GeneralSecurityException gse) {
        close();
        throw new MessagingException("Unable to open connection to SMTP server due to security error.", gse);
    } catch (IOException ioe) {
        close();
        throw new MessagingException("Unable to open connection to SMTP server.", ioe);
    }
}
Also used : SocketException(java.net.SocketException) MessagingException(com.fsck.k9.mail.MessagingException) InetSocketAddress(java.net.InetSocketAddress) GeneralSecurityException(java.security.GeneralSecurityException) PeekableInputStream(com.fsck.k9.mail.filter.PeekableInputStream) IOException(java.io.IOException) SSLException(javax.net.ssl.SSLException) BufferedInputStream(java.io.BufferedInputStream) CertificateValidationException(com.fsck.k9.mail.CertificateValidationException) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress) BufferedOutputStream(java.io.BufferedOutputStream) Socket(java.net.Socket)

Aggregations

PeekableInputStream (com.fsck.k9.mail.filter.PeekableInputStream)5 BufferedInputStream (java.io.BufferedInputStream)3 BufferedOutputStream (java.io.BufferedOutputStream)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 CertificateValidationException (com.fsck.k9.mail.CertificateValidationException)1 MessagingException (com.fsck.k9.mail.MessagingException)1 IOException (java.io.IOException)1 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 Socket (java.net.Socket)1 SocketAddress (java.net.SocketAddress)1 SocketException (java.net.SocketException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 SSLException (javax.net.ssl.SSLException)1