Search in sources :

Example 1 with ConnectionSecurity

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

the class ImapStoreUriDecoder method decode.

/**
     * Decodes an ImapStore URI.
     *
     * <p>Possible forms:</p>
     * <pre>
     * imap://auth:user:password@server:port ConnectionSecurity.NONE
     * imap+tls+://auth:user:password@server:port ConnectionSecurity.STARTTLS_REQUIRED
     * imap+ssl+://auth:user:password@server:port ConnectionSecurity.SSL_TLS_REQUIRED
     * </pre>
     *
     * NOTE: this method expects the userinfo part of the uri to be encoded twice, due to a bug in
     * {@link ImapStoreUriCreator#create(ServerSettings)}.
     *
     * @param uri the store uri.
     */
public static ImapStoreSettings decode(String uri) {
    String host;
    int port;
    ConnectionSecurity connectionSecurity;
    AuthType authenticationType = null;
    String username = null;
    String password = null;
    String clientCertificateAlias = null;
    String pathPrefix = null;
    boolean autoDetectNamespace = true;
    URI imapUri;
    try {
        imapUri = new URI(uri);
    } catch (URISyntaxException use) {
        throw new IllegalArgumentException("Invalid ImapStore URI", use);
    }
    String scheme = imapUri.getScheme();
    /*
         * Currently available schemes are:
         * imap
         * imap+tls+
         * imap+ssl+
         *
         * The following are obsolete schemes that may be found in pre-existing
         * settings from earlier versions or that may be found when imported. We
         * continue to recognize them and re-map them appropriately:
         * imap+tls
         * imap+ssl
         */
    if (scheme.equals("imap")) {
        connectionSecurity = ConnectionSecurity.NONE;
        port = Type.IMAP.defaultPort;
    } else if (scheme.startsWith("imap+tls")) {
        connectionSecurity = ConnectionSecurity.STARTTLS_REQUIRED;
        port = Type.IMAP.defaultPort;
    } else if (scheme.startsWith("imap+ssl")) {
        connectionSecurity = ConnectionSecurity.SSL_TLS_REQUIRED;
        port = Type.IMAP.defaultTlsPort;
    } else {
        throw new IllegalArgumentException("Unsupported protocol (" + scheme + ")");
    }
    host = imapUri.getHost();
    if (imapUri.getPort() != -1) {
        port = imapUri.getPort();
    }
    if (imapUri.getUserInfo() != null) {
        String userinfo = imapUri.getUserInfo();
        String[] userInfoParts = userinfo.split(":");
        if (userinfo.endsWith(":")) {
            // Or XOAUTH2 where it's a valid config - XOAUTH:username:
            if (userInfoParts.length > 1) {
                authenticationType = AuthType.valueOf(userInfoParts[0]);
                username = decodeUtf8(userInfoParts[1]);
            } else {
                authenticationType = AuthType.PLAIN;
                username = decodeUtf8(userInfoParts[0]);
            }
        } else if (userInfoParts.length == 2) {
            // Old/standard style of encoding - PLAIN auth only:
            // username:password
            authenticationType = AuthType.PLAIN;
            username = decodeUtf8(userInfoParts[0]);
            password = decodeUtf8(userInfoParts[1]);
        } else if (userInfoParts.length == 3) {
            // Standard encoding
            // PLAIN:username:password
            // EXTERNAL:username:certAlias
            authenticationType = AuthType.valueOf(userInfoParts[0]);
            username = decodeUtf8(userInfoParts[1]);
            if (AuthType.EXTERNAL == authenticationType) {
                clientCertificateAlias = decodeUtf8(userInfoParts[2]);
            } else {
                password = decodeUtf8(userInfoParts[2]);
            }
        }
    }
    String path = imapUri.getPath();
    if (path != null && path.length() > 1) {
        // Strip off the leading "/"
        String cleanPath = path.substring(1);
        if (cleanPath.length() >= 2 && cleanPath.charAt(1) == '|') {
            autoDetectNamespace = cleanPath.charAt(0) == '1';
            if (!autoDetectNamespace) {
                pathPrefix = cleanPath.substring(2);
            }
        } else {
            if (cleanPath.length() > 0) {
                pathPrefix = cleanPath;
                autoDetectNamespace = false;
            }
        }
    }
    return new ImapStoreSettings(host, port, connectionSecurity, authenticationType, username, password, clientCertificateAlias, autoDetectNamespace, pathPrefix);
}
Also used : ConnectionSecurity(com.fsck.k9.mail.ConnectionSecurity) AuthType(com.fsck.k9.mail.AuthType) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 2 with ConnectionSecurity

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

the class WebDavStoreUriDecoder method decode.

/**
     * Decodes a WebDavStore URI.
     * <p/>
     * <p>Possible forms:</p>
     * <pre>
     * webdav://user:password@server:port ConnectionSecurity.NONE
     * webdav+ssl+://user:password@server:port ConnectionSecurity.SSL_TLS_REQUIRED
     * </pre>
     */
public static WebDavStoreSettings decode(String uri) {
    String host;
    int port;
    ConnectionSecurity connectionSecurity;
    String username = null;
    String password = null;
    String alias = null;
    String path = null;
    String authPath = null;
    String mailboxPath = null;
    URI webDavUri;
    try {
        webDavUri = new URI(uri);
    } catch (URISyntaxException use) {
        throw new IllegalArgumentException("Invalid WebDavStore URI", use);
    }
    String scheme = webDavUri.getScheme();
    /*
         * Currently available schemes are:
         * webdav
         * webdav+ssl+
         *
         * The following are obsolete schemes that may be found in pre-existing
         * settings from earlier versions or that may be found when imported. We
         * continue to recognize them and re-map them appropriately:
         * webdav+tls
         * webdav+tls+
         * webdav+ssl
         */
    if (scheme.equals("webdav")) {
        connectionSecurity = ConnectionSecurity.NONE;
    } else if (scheme.startsWith("webdav+")) {
        connectionSecurity = ConnectionSecurity.SSL_TLS_REQUIRED;
    } else {
        throw new IllegalArgumentException("Unsupported protocol (" + scheme + ")");
    }
    host = webDavUri.getHost();
    if (host.startsWith("http")) {
        String[] hostParts = host.split("://", 2);
        if (hostParts.length > 1) {
            host = hostParts[1];
        }
    }
    port = webDavUri.getPort();
    String userInfo = webDavUri.getUserInfo();
    if (userInfo != null) {
        String[] userInfoParts = userInfo.split(":");
        username = decodeUtf8(userInfoParts[0]);
        String[] userParts = username.split("\\\\", 2);
        if (userParts.length > 1) {
            alias = userParts[1];
        } else {
            alias = username;
        }
        if (userInfoParts.length > 1) {
            password = decodeUtf8(userInfoParts[1]);
        }
    }
    String[] pathParts = webDavUri.getPath().split("\\|");
    for (int i = 0, count = pathParts.length; i < count; i++) {
        if (i == 0) {
            if (pathParts[0] != null && pathParts[0].length() > 1) {
                path = pathParts[0];
            }
        } else if (i == 1) {
            if (pathParts[1] != null && pathParts[1].length() > 1) {
                authPath = pathParts[1];
            }
        } else if (i == 2) {
            if (pathParts[2] != null && pathParts[2].length() > 1) {
                mailboxPath = pathParts[2];
            }
        }
    }
    return new WebDavStoreSettings(host, port, connectionSecurity, null, username, password, null, alias, path, authPath, mailboxPath);
}
Also used : ConnectionSecurity(com.fsck.k9.mail.ConnectionSecurity) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 3 with ConnectionSecurity

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

the class WebDavStoreUriDecoder method decode.

/**
 * Decodes a WebDavStore URI.
 * <p/>
 * <p>Possible forms:</p>
 * <pre>
 * webdav://user:password@server:port ConnectionSecurity.NONE
 * webdav+ssl+://user:password@server:port ConnectionSecurity.SSL_TLS_REQUIRED
 * </pre>
 */
public static ServerSettings decode(String uri) {
    String host;
    int port;
    ConnectionSecurity connectionSecurity;
    String username = null;
    String password = null;
    String alias = null;
    String path = null;
    String authPath = null;
    String mailboxPath = null;
    URI webDavUri;
    try {
        webDavUri = new URI(uri);
    } catch (URISyntaxException use) {
        throw new IllegalArgumentException("Invalid WebDavStore URI", use);
    }
    String scheme = webDavUri.getScheme();
    /*
         * Currently available schemes are:
         * webdav
         * webdav+ssl+
         *
         * The following are obsolete schemes that may be found in pre-existing
         * settings from earlier versions or that may be found when imported. We
         * continue to recognize them and re-map them appropriately:
         * webdav+tls
         * webdav+tls+
         * webdav+ssl
         */
    if (scheme.equals("webdav")) {
        connectionSecurity = ConnectionSecurity.NONE;
    } else if (scheme.startsWith("webdav+")) {
        connectionSecurity = ConnectionSecurity.SSL_TLS_REQUIRED;
    } else {
        throw new IllegalArgumentException("Unsupported protocol (" + scheme + ")");
    }
    host = webDavUri.getHost();
    if (host.startsWith("http")) {
        String[] hostParts = host.split("://", 2);
        if (hostParts.length > 1) {
            host = hostParts[1];
        }
    }
    port = webDavUri.getPort();
    String userInfo = webDavUri.getUserInfo();
    if (userInfo != null) {
        String[] userInfoParts = userInfo.split(":");
        username = decodeUtf8(userInfoParts[0]);
        String[] userParts = username.split("\\\\", 2);
        if (userParts.length > 1) {
            alias = userParts[1];
        } else {
            alias = username;
        }
        if (userInfoParts.length > 1) {
            password = decodeUtf8(userInfoParts[1]);
        }
    }
    String[] pathParts = webDavUri.getPath().split("\\|");
    for (int i = 0, count = pathParts.length; i < count; i++) {
        if (i == 0) {
            if (pathParts[0] != null && pathParts[0].length() > 1) {
                path = pathParts[0];
            }
        } else if (i == 1) {
            if (pathParts[1] != null && pathParts[1].length() > 1) {
                authPath = pathParts[1];
            }
        } else if (i == 2) {
            if (pathParts[2] != null && pathParts[2].length() > 1) {
                mailboxPath = pathParts[2];
            }
        }
    }
    Map<String, String> extra = new HashMap<>();
    extra.put("alias", alias);
    extra.put("path", path);
    extra.put("authPath", authPath);
    extra.put("mailboxPath", mailboxPath);
    return new ServerSettings("webdav", host, port, connectionSecurity, AuthType.PLAIN, username, password, null, extra);
}
Also used : ConnectionSecurity(com.fsck.k9.mail.ConnectionSecurity) HashMap(java.util.HashMap) ServerSettings(com.fsck.k9.mail.ServerSettings) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 4 with ConnectionSecurity

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

the class SettingsImporter method createServerSettings.

private static ServerSettings createServerSettings(ImportedServer importedServer) {
    String type = ServerTypeConverter.toServerSettingsType(importedServer.type);
    int port = convertPort(importedServer.port);
    ConnectionSecurity connectionSecurity = convertConnectionSecurity(importedServer.connectionSecurity);
    Map<String, String> extra = importedServer.extras != null ? unmodifiableMap(importedServer.extras.settings) : emptyMap();
    return new ServerSettings(type, importedServer.host, port, connectionSecurity, importedServer.authenticationType, importedServer.username, importedServer.password, importedServer.clientCertificateAlias, extra);
}
Also used : ConnectionSecurity(com.fsck.k9.mail.ConnectionSecurity) ServerSettings(com.fsck.k9.mail.ServerSettings)

Example 5 with ConnectionSecurity

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

the class AccountSetupOutgoing method onNext.

protected void onNext() {
    ConnectionSecurity securityType = getSelectedSecurity();
    String username = "";
    String password = null;
    String clientCertificateAlias = null;
    AuthType authType = AuthType.AUTOMATIC;
    if ((ConnectionSecurity.STARTTLS_REQUIRED == securityType) || (ConnectionSecurity.SSL_TLS_REQUIRED == securityType)) {
        clientCertificateAlias = mClientCertificateSpinner.getAlias();
    }
    if (mRequireLoginView.isChecked()) {
        username = mUsernameView.getText().toString().trim();
        authType = getSelectedAuthType();
        if (AuthType.EXTERNAL != authType) {
            password = mPasswordView.getText().toString();
        }
    }
    String newHost = mServerView.getText().toString();
    int newPort = Integer.parseInt(mPortView.getText().toString());
    ServerSettings server = new ServerSettings(Protocols.SMTP, newHost, newPort, securityType, authType, username, password, clientCertificateAlias);
    DI.get(LocalKeyStoreManager.class).deleteCertificate(mAccount, newHost, newPort, MailServerDirection.OUTGOING);
    mAccount.setOutgoingServerSettings(server);
    AccountSetupCheckSettings.actionCheckSettings(this, mAccount, CheckDirection.OUTGOING);
}
Also used : ConnectionSecurity(com.fsck.k9.mail.ConnectionSecurity) ServerSettings(com.fsck.k9.mail.ServerSettings) LocalKeyStoreManager(com.fsck.k9.LocalKeyStoreManager) AuthType(com.fsck.k9.mail.AuthType)

Aggregations

ConnectionSecurity (com.fsck.k9.mail.ConnectionSecurity)15 ServerSettings (com.fsck.k9.mail.ServerSettings)10 AuthType (com.fsck.k9.mail.AuthType)9 URI (java.net.URI)7 URISyntaxException (java.net.URISyntaxException)7 OnItemSelectedListener (android.widget.AdapterView.OnItemSelectedListener)2 LocalKeyStoreManager (com.fsck.k9.LocalKeyStoreManager)2 HashMap (java.util.HashMap)2 CompoundButton (android.widget.CompoundButton)1 OnCheckedChangeListener (android.widget.CompoundButton.OnCheckedChangeListener)1 CertificateValidationException (com.fsck.k9.mail.CertificateValidationException)1 MessagingException (com.fsck.k9.mail.MessagingException)1 PeekableInputStream (com.fsck.k9.mail.filter.PeekableInputStream)1 TextInputLayout (com.google.android.material.textfield.TextInputLayout)1 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 IOException (java.io.IOException)1 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 Socket (java.net.Socket)1