k-9/k9mail/src/main/java/com/fsck/k9/activity/setup/AccountSetupIncoming.java

680 lines
30 KiB
Java
Raw Normal View History

package com.fsck.k9.activity.setup;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.DigitsKeyListener;
Complete merge of DAmail functionality into K9mail. Following features are added to K9mail: 1) Show unread message count on each folder 2) Sum unread count of all shown folders in an account to the account display 3) Periodically check selected folders for new mail, not just Inbox 4) Don't refresh folder when opened (unless folder is empty) 5) Show date and time of last sync for each folder 6) Fix timer for automatic periodic sync (use wakelock to assure completion) 7) Optimize local folder queries (speeds up account and folder lists) 8) Show Loading... message in status bar indicating which folder is being synced 9) Eliminate redundant sync of new messages (performance enhancement) 10) Improve notification text for multiple accounts 11) Do not automatically sync folders more often than the account-specific period 12) Use user-configured date and time formats 13) Select which folders are shown, using configurable Classes 14) Select which folders are synced, using configurable Classes 15) Added context (long press) menu to folders, to provide for Refresh and Folder Settings 16) Status light flashes purple when there are unread messages 17) Folder list more quickly eliminates display of deleted and out-of-Class folders. 18) Delete works 19) Mark all messages as read (in the folder context menu) 20) Notifications only for new unread messages 21) One minute synchronization frequency 22) Deleting an unread message decrements unread counter 23) Notifications work for POP3 accounts 24) Message deletes work for POP3 accounts 25) Explicit errors show in folder list 26) Stack traces saved to folder K9mail-errors 27) Clear pending actions (danger, for emergencies only!) 28) Delete policy in Account settings 29) DNS cache in InetAddress disabled 30) Trapped some crash-causing error conditions 31) Eliminate duplicate copies to Sent folder 32) Prevent crashes due to message listener concurrency 33) Empty Trash 34) Nuclear "Mark all messages as read" (marks all messages as read in server-side folder, irrespective of which messages have been downloaded) 35) Forward (alternate) to allow forwarding email through other programs 36) Accept text/plain Intents to allow other programs to send email through K9mail 37) Displays Outbox sending status 38) Manual retry of outbox sending when "Refresh"ing Outbox 39) Folder error status is persisted 40) Ability to log to arbitrary file Fixes K9 issues 11, 23, 24, 65, 69, 71, 79, 81, 82, 83, 87, 101, 104, 107, 120, 148, 154
2008-12-30 22:49:09 -05:00
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.CompoundButton.OnCheckedChangeListener;
import com.fsck.k9.*;
import com.fsck.k9.Account.DeletePolicy;
import com.fsck.k9.Account.FolderMode;
import com.fsck.k9.Account.NetworkType;
import com.fsck.k9.activity.K9Activity;
Fix inadequate certificate validation Proper host name validation was not being performed for certificates kept in the local keystore. If an attacker could convince a user to accept and store an attacker's certificate, then that certificate could be used for MITM attacks, giving the attacker access to all connections to all servers in all accounts in K-9. This commit changes how the certificates are stored. Previously, an entire certificate chain was stored for a server (and any of those certificates in the chain were available for validating signatures on certificates received when connecting). Now just the single certificate for the server is stored. This commit changes how locally stored certificates are retrieved. They can only be retrieved using the host:port that the user configured for the server. This also fixes issue 1326. Users can now use different certificates for different servers on the same host (listening to different ports). The above changes mean that users might have to re-accept certificates that they had previously accepted and are still using (but only if the certificate's Subject doesn't match the host that they are connecting to). This commit modifies AccountSetupBasics so that it now calls AccountSetupCheckSettings twice -- once for checking the incoming settings and once for the outgoing settings. Otherwise, an exception could occur while checking incoming settings, the user could say continue (or the user could accept a certificate key), and the outgoing settings would not be checked. This also helps with determining if a certificate exception was for the incoming or outgoing server, which is needed if the user decides to add the certificate to the keystore.
2013-11-23 13:26:57 -05:00
import com.fsck.k9.activity.setup.AccountSetupCheckSettings.CheckDirection;
import com.fsck.k9.helper.Utility;
import com.fsck.k9.mail.AuthType;
import com.fsck.k9.mail.ConnectionSecurity;
import com.fsck.k9.mail.ServerSettings;
import com.fsck.k9.mail.Store;
2014-05-25 16:45:14 -04:00
import com.fsck.k9.mail.Transport;
2014-12-18 03:33:09 -05:00
import com.fsck.k9.mail.store.imap.ImapStore;
import com.fsck.k9.mail.store.pop3.Pop3Store;
import com.fsck.k9.mail.store.RemoteStore;
2014-12-18 03:33:09 -05:00
import com.fsck.k9.mail.store.webdav.WebDavStore;
import com.fsck.k9.mail.store.imap.ImapStore.ImapStoreSettings;
import com.fsck.k9.mail.store.webdav.WebDavStore.WebDavStoreSettings;
2014-05-25 16:45:14 -04:00
import com.fsck.k9.mail.transport.SmtpTransport;
import com.fsck.k9.service.MailService;
2014-05-25 16:45:14 -04:00
import com.fsck.k9.view.ClientCertificateSpinner;
import com.fsck.k9.view.ClientCertificateSpinner.OnClientCertificateChangedListener;
Merge branch 'mail-on-sd' * mail-on-sd: (40 commits) Added more comments to explain how the locking mecanism works for LocalStore Fixed wrong method being called during experimental provider initialization (since provider isn't enabled, that didn't harm) Add more comments about how the various StorageProviders work and how they're enabled find src/com/fsck/ -name \*.java|xargs astyle --style=ansi --mode=java --indent-switches --indent=spaces=4 --convert-tabs French localization for storage related settings Remove unused SD card strings (replaced with storage indirection) Merge mail-on-sd branch from trunk Reset mail service on storage mount (even if no account uses the storage, to be improved) find src/com/fsck/ -name \*.java|xargs astyle --style=ansi --mode=java --indent-switches --indent=spaces=4 --convert-tabs Migraion -> Migration move the Storage location preference into preferences rather than the wizard. Made LocalStore log less verbose Added @Override compile checks Added ACTION_SHUTDOWN broadcast receiver to properly initiate shutdown sequence (not yet implemented) and cancel any scheduled Intent Be more consistent about which SQLiteDatabase variable is used (from instance variable to argument variable) to make code more refactoring-friendly (class is already big, code extraction should be easier if not referencing the instance variable). Added transaction timing logging Factorised storage lock/transaction handling code for regular operations. Use DB transactions to batch modifications (makes code more robust / could improve performances) Merge mail-on-sd branch from trunk Update issue 888 Added DB close on unmount / DB open on mount Update issue 888 Back to account list when underlying storage not available/unmounting in MessageView / MessageList ...
2010-11-13 16:40:56 -05:00
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
public class AccountSetupIncoming extends K9Activity implements OnClickListener {
private static final String EXTRA_ACCOUNT = "account";
private static final String EXTRA_MAKE_DEFAULT = "makeDefault";
private static final String STATE_SECURITY_TYPE_POSITION = "stateSecurityTypePosition";
private static final String STATE_AUTH_TYPE_POSITION = "authTypePosition";
private static final String POP3_PORT = "110";
private static final String POP3_SSL_PORT = "995";
private static final String IMAP_PORT = "143";
private static final String IMAP_SSL_PORT = "993";
private static final String WEBDAV_PORT = "80";
private static final String WEBDAV_SSL_PORT = "443";
private String mStoreType;
private EditText mUsernameView;
private EditText mPasswordView;
2014-05-25 16:45:14 -04:00
private ClientCertificateSpinner mClientCertificateSpinner;
private TextView mClientCertificateLabelView;
private TextView mPasswordLabelView;
private EditText mServerView;
private EditText mPortView;
private String mCurrentPortViewSetting;
private Spinner mSecurityTypeView;
private int mCurrentSecurityTypeViewPosition;
private Spinner mAuthTypeView;
private int mCurrentAuthTypeViewPosition;
private CheckBox mImapAutoDetectNamespaceView;
private EditText mImapPathPrefixView;
private EditText mWebdavPathPrefixView;
private EditText mWebdavAuthPathView;
private EditText mWebdavMailboxPathView;
private Button mNextButton;
private Account mAccount;
private boolean mMakeDefault;
private CheckBox mCompressionMobile;
private CheckBox mCompressionWifi;
private CheckBox mCompressionOther;
private CheckBox mSubscribedFoldersOnly;
private AuthTypeAdapter mAuthTypeAdapter;
private String mDefaultPort = "";
private String mDefaultSslPort = "";
private ConnectionSecurity[] mConnectionSecurityChoices = ConnectionSecurity.values();
public static void actionIncomingSettings(Activity context, Account account, boolean makeDefault) {
Intent i = new Intent(context, AccountSetupIncoming.class);
i.putExtra(EXTRA_ACCOUNT, account.getUuid());
i.putExtra(EXTRA_MAKE_DEFAULT, makeDefault);
context.startActivity(i);
}
public static void actionEditIncomingSettings(Activity context, Account account) {
context.startActivity(intentActionEditIncomingSettings(context, account));
}
public static Intent intentActionEditIncomingSettings(Context context, Account account) {
Intent i = new Intent(context, AccountSetupIncoming.class);
i.setAction(Intent.ACTION_EDIT);
i.putExtra(EXTRA_ACCOUNT, account.getUuid());
return i;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.account_setup_incoming);
mUsernameView = (EditText)findViewById(R.id.account_username);
mPasswordView = (EditText)findViewById(R.id.account_password);
2014-05-25 16:45:14 -04:00
mClientCertificateSpinner = (ClientCertificateSpinner)findViewById(R.id.account_client_certificate_spinner);
mClientCertificateLabelView = (TextView)findViewById(R.id.account_client_certificate_label);
mPasswordLabelView = (TextView)findViewById(R.id.account_password_label);
TextView serverLabelView = (TextView) findViewById(R.id.account_server_label);
mServerView = (EditText)findViewById(R.id.account_server);
mPortView = (EditText)findViewById(R.id.account_port);
mSecurityTypeView = (Spinner)findViewById(R.id.account_security_type);
mAuthTypeView = (Spinner)findViewById(R.id.account_auth_type);
mImapAutoDetectNamespaceView = (CheckBox)findViewById(R.id.imap_autodetect_namespace);
mImapPathPrefixView = (EditText)findViewById(R.id.imap_path_prefix);
mWebdavPathPrefixView = (EditText)findViewById(R.id.webdav_path_prefix);
mWebdavAuthPathView = (EditText)findViewById(R.id.webdav_auth_path);
mWebdavMailboxPathView = (EditText)findViewById(R.id.webdav_mailbox_path);
mNextButton = (Button)findViewById(R.id.next);
mCompressionMobile = (CheckBox)findViewById(R.id.compression_mobile);
mCompressionWifi = (CheckBox)findViewById(R.id.compression_wifi);
mCompressionOther = (CheckBox)findViewById(R.id.compression_other);
mSubscribedFoldersOnly = (CheckBox)findViewById(R.id.subscribed_folders_only);
2010-05-11 22:51:59 -04:00
mNextButton.setOnClickListener(this);
mImapAutoDetectNamespaceView.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mImapPathPrefixView.setEnabled(!isChecked);
if (isChecked && mImapPathPrefixView.hasFocus()) {
mImapPathPrefixView.focusSearch(View.FOCUS_UP).requestFocus();
} else if (!isChecked) {
mImapPathPrefixView.requestFocus();
}
}
});
mAuthTypeAdapter = AuthTypeAdapter.get(this);
2014-05-25 16:45:14 -04:00
mAuthTypeView.setAdapter(mAuthTypeAdapter);
/*
* Only allow digits in the port field.
*/
mPortView.setKeyListener(DigitsKeyListener.getInstance("0123456789"));
String accountUuid = getIntent().getStringExtra(EXTRA_ACCOUNT);
mAccount = Preferences.getPreferences(this).getAccount(accountUuid);
mMakeDefault = getIntent().getBooleanExtra(EXTRA_MAKE_DEFAULT, false);
/*
* If we're being reloaded we override the original account with the one
* we saved
*/
if (savedInstanceState != null && savedInstanceState.containsKey(EXTRA_ACCOUNT)) {
accountUuid = savedInstanceState.getString(EXTRA_ACCOUNT);
mAccount = Preferences.getPreferences(this).getAccount(accountUuid);
}
try {
ServerSettings settings = RemoteStore.decodeStoreUri(mAccount.getStoreUri());
if (savedInstanceState == null) {
// The first item is selected if settings.authenticationType is null or is not in mAuthTypeAdapter
mCurrentAuthTypeViewPosition = mAuthTypeAdapter.getAuthPosition(settings.authenticationType);
} else {
mCurrentAuthTypeViewPosition = savedInstanceState.getInt(STATE_AUTH_TYPE_POSITION);
}
mAuthTypeView.setSelection(mCurrentAuthTypeViewPosition, false);
updateViewFromAuthType();
2014-05-25 16:45:14 -04:00
if (settings.username != null) {
mUsernameView.setText(settings.username);
}
if (settings.password != null) {
mPasswordView.setText(settings.password);
}
2014-05-25 16:45:14 -04:00
if (settings.clientCertificateAlias != null) {
mClientCertificateSpinner.setAlias(settings.clientCertificateAlias);
}
mStoreType = settings.type;
if (Pop3Store.STORE_TYPE.equals(settings.type)) {
serverLabelView.setText(R.string.account_setup_incoming_pop_server_label);
mDefaultPort = POP3_PORT;
mDefaultSslPort = POP3_SSL_PORT;
2011-01-28 17:45:10 -05:00
findViewById(R.id.imap_path_prefix_section).setVisibility(View.GONE);
findViewById(R.id.webdav_advanced_header).setVisibility(View.GONE);
findViewById(R.id.webdav_mailbox_alias_section).setVisibility(View.GONE);
findViewById(R.id.webdav_owa_path_section).setVisibility(View.GONE);
findViewById(R.id.webdav_auth_path_section).setVisibility(View.GONE);
findViewById(R.id.compression_section).setVisibility(View.GONE);
findViewById(R.id.compression_label).setVisibility(View.GONE);
mSubscribedFoldersOnly.setVisibility(View.GONE);
mAccount.setDeletePolicy(DeletePolicy.NEVER);
} else if (ImapStore.STORE_TYPE.equals(settings.type)) {
serverLabelView.setText(R.string.account_setup_incoming_imap_server_label);
mDefaultPort = IMAP_PORT;
mDefaultSslPort = IMAP_SSL_PORT;
ImapStoreSettings imapSettings = (ImapStoreSettings) settings;
mImapAutoDetectNamespaceView.setChecked(imapSettings.autoDetectNamespace);
if (imapSettings.pathPrefix != null) {
mImapPathPrefixView.setText(imapSettings.pathPrefix);
}
2011-01-28 17:45:10 -05:00
findViewById(R.id.webdav_advanced_header).setVisibility(View.GONE);
findViewById(R.id.webdav_mailbox_alias_section).setVisibility(View.GONE);
findViewById(R.id.webdav_owa_path_section).setVisibility(View.GONE);
findViewById(R.id.webdav_auth_path_section).setVisibility(View.GONE);
mAccount.setDeletePolicy(DeletePolicy.ON_DELETE);
if (!Intent.ACTION_EDIT.equals(getIntent().getAction())) {
findViewById(R.id.imap_folder_setup_section).setVisibility(View.GONE);
}
} else if (WebDavStore.STORE_TYPE.equals(settings.type)) {
Merge into 'trunk' r51837@31b (orig r127): ismarc31 | 2008-11-10 19:10:50 -0500 Experimental branch for Exchange WebDAV support r51838@31b (orig r128): ismarc31 | 2008-11-10 19:24:52 -0500 Initial proof-of-concept code for WebDav support r51839@31b (orig r129): ismarc31 | 2008-11-10 22:02:37 -0500 Fixed a couple of migration issues and enabled WebDav as a mail type r53269@31b (orig r132): ismarc31 | 2008-11-21 21:55:55 -0500 Mostly rewritten class and organization. Better implementation of message fetching. Consolidated response parsing. Removed a large number of redundant calls. There is still some unused functions needing cleaning up, and some unimplemented actions r53338@31b (orig r133): ismarc31 | 2008-11-22 16:50:02 -0500 Removed more redundant and unused calls. Implemented checking read status r53453@31b (orig r134): ismarc31 | 2008-11-24 20:13:24 -0500 Added support for marking messages as read. r53454@31b (orig r135): ismarc31 | 2008-11-24 22:04:04 -0500 Added support for deleting messages server side r53455@31b (orig r136): ismarc31 | 2008-11-25 01:32:19 -0500 Improved flag setting functionality, do bulk HTTP request instead of lots of little ones r53589@31b (orig r138): young.bradley | 2008-11-29 16:18:25 -0500 Missing some ports (webDavPorts); this causes an array index out of bounds exception when anything other than "None" or "SSL (Optional)" are selected. Adding the three additional ports solves this issue. r53590@31b (orig r139): young.bradley | 2008-11-30 00:47:42 -0500 Initial support for sending via WebDav r53591@31b (orig r140): ismarc31 | 2008-11-30 20:12:41 -0500 Fix for display names being URL Encoded for folders. Initial support of Uid Hashmaps instead of plain arrays. r53592@31b (orig r141): ismarc31 | 2008-11-30 21:46:06 -0500 Fix to constructor of HttpGeneric(final String uri). URLs returned from Exchange aren't always fully encoded, this fixes the encoding before creating the method. r53593@31b (orig r142): ismarc31 | 2008-12-01 02:22:16 -0500 Completed support for using hashmaps instead of arrays for indexing urls to emails and read status. Delete is safe again and read status is correct the first time through. r53594@31b (orig r143): ismarc31 | 2008-12-01 22:20:50 -0500 Fix for double-Inbox display issue. Removed volumous amounts of Log.d messages. r53644@31b (orig r157): young.bradley | 2008-12-04 15:14:28 -0500 Fix for wildcard certificates (e.g. issued to *.example.com). Only checking the trust of the certificate itself, since apparently the full chain causes it to not work. r53765@31b (orig r161): ismarc31 | 2008-12-06 18:55:08 -0500 Implemented new functionality for pulling message envelope. Uses a WebDAV call for all messages rather than parsing the stream. Message size is properly set now as well. r54055@31b (orig r163): jessev | 2008-12-06 19:28:24 -0500 * merge fixes
2008-12-06 19:29:11 -05:00
serverLabelView.setText(R.string.account_setup_incoming_webdav_server_label);
mDefaultPort = WEBDAV_PORT;
mDefaultSslPort = WEBDAV_SSL_PORT;
mConnectionSecurityChoices = new ConnectionSecurity[] {
ConnectionSecurity.NONE,
ConnectionSecurity.SSL_TLS_REQUIRED };
2011-11-30 23:17:32 -05:00
// Hide the unnecessary fields
findViewById(R.id.imap_path_prefix_section).setVisibility(View.GONE);
findViewById(R.id.account_auth_type_label).setVisibility(View.GONE);
findViewById(R.id.account_auth_type).setVisibility(View.GONE);
findViewById(R.id.compression_section).setVisibility(View.GONE);
findViewById(R.id.compression_label).setVisibility(View.GONE);
mSubscribedFoldersOnly.setVisibility(View.GONE);
WebDavStoreSettings webDavSettings = (WebDavStoreSettings) settings;
if (webDavSettings.path != null) {
mWebdavPathPrefixView.setText(webDavSettings.path);
}
if (webDavSettings.authPath != null) {
mWebdavAuthPathView.setText(webDavSettings.authPath);
}
if (webDavSettings.mailboxPath != null) {
mWebdavMailboxPathView.setText(webDavSettings.mailboxPath);
}
mAccount.setDeletePolicy(DeletePolicy.ON_DELETE);
} else {
throw new Exception("Unknown account type: " + mAccount.getStoreUri());
}
// Note that mConnectionSecurityChoices is configured above based on server type
ConnectionSecurityAdapter securityTypesAdapter =
ConnectionSecurityAdapter.get(this, mConnectionSecurityChoices);
mSecurityTypeView.setAdapter(securityTypesAdapter);
// Select currently configured security type
if (savedInstanceState == null) {
mCurrentSecurityTypeViewPosition = securityTypesAdapter.getConnectionSecurityPosition(settings.connectionSecurity);
} else {
/*
* Restore the spinner state now, before calling
* setOnItemSelectedListener(), thus avoiding a call to
* onItemSelected(). Then, when the system restores the state
* (again) in onRestoreInstanceState(), The system will see that
* the new state is the same as the current state (set here), so
* once again onItemSelected() will not be called.
*/
mCurrentSecurityTypeViewPosition = savedInstanceState.getInt(STATE_SECURITY_TYPE_POSITION);
}
mSecurityTypeView.setSelection(mCurrentSecurityTypeViewPosition, false);
updateAuthPlainTextFromSecurityType(settings.connectionSecurity);
mCompressionMobile.setChecked(mAccount.useCompression(NetworkType.MOBILE));
mCompressionWifi.setChecked(mAccount.useCompression(NetworkType.WIFI));
mCompressionOther.setChecked(mAccount.useCompression(NetworkType.OTHER));
if (settings.host != null) {
mServerView.setText(settings.host);
}
if (settings.port != -1) {
mPortView.setText(Integer.toString(settings.port));
} else {
updatePortFromSecurityType();
}
mCurrentPortViewSetting = mPortView.getText().toString();
mSubscribedFoldersOnly.setChecked(mAccount.subscribedFoldersOnly());
} catch (Exception e) {
failure(e);
}
}
/**
* Called at the end of either {@code onCreate()} or
* {@code onRestoreInstanceState()}, after the views have been initialized,
* so that the listeners are not triggered during the view initialization.
* This avoids needless calls to {@code validateFields()} which is called
* immediately after this is called.
*/
private void initializeViewListeners() {
/*
* Updates the port when the user changes the security type. This allows
* us to show a reasonable default which the user can change.
*/
mSecurityTypeView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
/*
* We keep our own record of the spinner state so we
* know for sure that onItemSelected() was called
* because of user input, not because of spinner
* state initialization. This assures that the port
* will not be replaced with a default value except
* on user input.
*/
if (mCurrentSecurityTypeViewPosition != position) {
updatePortFromSecurityType();
validateFields();
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) { /* unused */ }
});
mAuthTypeView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
if (mCurrentAuthTypeViewPosition == position) {
return;
}
updateViewFromAuthType();
validateFields();
AuthType selection = getSelectedAuthType();
// Have the user select (or confirm) the client certificate
if (AuthType.EXTERNAL == selection) {
// This may again invoke validateFields()
mClientCertificateSpinner.chooseCertificate();
} else {
mPasswordView.requestFocus();
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) { /* unused */ }
});
mClientCertificateSpinner.setOnClientCertificateChangedListener(clientCertificateChangedListener);
mUsernameView.addTextChangedListener(validationTextWatcher);
mPasswordView.addTextChangedListener(validationTextWatcher);
mServerView.addTextChangedListener(validationTextWatcher);
mPortView.addTextChangedListener(validationTextWatcher);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(EXTRA_ACCOUNT, mAccount.getUuid());
outState.putInt(STATE_SECURITY_TYPE_POSITION, mCurrentSecurityTypeViewPosition);
outState.putInt(STATE_AUTH_TYPE_POSITION, mCurrentAuthTypeViewPosition);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
/*
* We didn't want the listeners active while the state was being restored
* because they could overwrite the restored port with a default port when
* the security type was restored.
*/
initializeViewListeners();
validateFields();
}
2014-05-25 16:45:14 -04:00
/**
* Shows/hides password field and client certificate spinner
*/
private void updateViewFromAuthType() {
AuthType authType = getSelectedAuthType();
boolean isAuthTypeExternal = (AuthType.EXTERNAL == authType);
2014-05-25 16:45:14 -04:00
if (isAuthTypeExternal) {
// hide password fields, show client certificate fields
mPasswordView.setVisibility(View.GONE);
mPasswordLabelView.setVisibility(View.GONE);
mClientCertificateLabelView.setVisibility(View.VISIBLE);
mClientCertificateSpinner.setVisibility(View.VISIBLE);
} else {
// show password fields, hide client certificate fields
mPasswordView.setVisibility(View.VISIBLE);
mPasswordLabelView.setVisibility(View.VISIBLE);
mClientCertificateLabelView.setVisibility(View.GONE);
mClientCertificateSpinner.setVisibility(View.GONE);
}
}
/**
* This is invoked only when the user makes changes to a widget, not when
* widgets are changed programmatically. (The logic is simpler when you know
* that this is the last thing called after an input change.)
*/
private void validateFields() {
AuthType authType = getSelectedAuthType();
boolean isAuthTypeExternal = (AuthType.EXTERNAL == authType);
2014-05-25 16:45:14 -04:00
ConnectionSecurity connectionSecurity = getSelectedSecurity();
boolean hasConnectionSecurity = (connectionSecurity != ConnectionSecurity.NONE);
if (isAuthTypeExternal && !hasConnectionSecurity) {
// Notify user of an invalid combination of AuthType.EXTERNAL & ConnectionSecurity.NONE
String toastText = getString(R.string.account_setup_incoming_invalid_setting_combo_notice,
getString(R.string.account_setup_incoming_auth_type_label),
AuthType.EXTERNAL.toString(),
getString(R.string.account_setup_incoming_security_label),
ConnectionSecurity.NONE.toString());
Toast.makeText(this, toastText, Toast.LENGTH_LONG).show();
// Reset the views back to their previous settings without recursing through here again
OnItemSelectedListener onItemSelectedListener = mAuthTypeView.getOnItemSelectedListener();
mAuthTypeView.setOnItemSelectedListener(null);
mAuthTypeView.setSelection(mCurrentAuthTypeViewPosition, false);
mAuthTypeView.setOnItemSelectedListener(onItemSelectedListener);
updateViewFromAuthType();
onItemSelectedListener = mSecurityTypeView.getOnItemSelectedListener();
mSecurityTypeView.setOnItemSelectedListener(null);
mSecurityTypeView.setSelection(mCurrentSecurityTypeViewPosition, false);
mSecurityTypeView.setOnItemSelectedListener(onItemSelectedListener);
updateAuthPlainTextFromSecurityType(getSelectedSecurity());
mPortView.removeTextChangedListener(validationTextWatcher);
mPortView.setText(mCurrentPortViewSetting);
mPortView.addTextChangedListener(validationTextWatcher);
authType = getSelectedAuthType();
isAuthTypeExternal = (AuthType.EXTERNAL == authType);
connectionSecurity = getSelectedSecurity();
hasConnectionSecurity = (connectionSecurity != ConnectionSecurity.NONE);
} else {
mCurrentAuthTypeViewPosition = mAuthTypeView.getSelectedItemPosition();
mCurrentSecurityTypeViewPosition = mSecurityTypeView.getSelectedItemPosition();
mCurrentPortViewSetting = mPortView.getText().toString();
}
boolean hasValidCertificateAlias = mClientCertificateSpinner.getAlias() != null;
boolean hasValidUserName = Utility.requiredFieldValid(mUsernameView);
2014-05-25 16:45:14 -04:00
boolean hasValidPasswordSettings = hasValidUserName
&& !isAuthTypeExternal
&& Utility.requiredFieldValid(mPasswordView);
boolean hasValidExternalAuthSettings = hasValidUserName
&& isAuthTypeExternal
&& hasConnectionSecurity
&& hasValidCertificateAlias;
mNextButton.setEnabled(Utility.domainFieldValid(mServerView)
&& Utility.requiredFieldValid(mPortView)
&& (hasValidPasswordSettings || hasValidExternalAuthSettings));
Utility.setCompoundDrawablesAlpha(mNextButton, mNextButton.isEnabled() ? 255 : 128);
}
private void updatePortFromSecurityType() {
ConnectionSecurity securityType = getSelectedSecurity();
updateAuthPlainTextFromSecurityType(securityType);
// Remove listener so as not to trigger validateFields() which is called
// elsewhere as a result of user interaction.
mPortView.removeTextChangedListener(validationTextWatcher);
mPortView.setText(getDefaultPort(securityType));
mPortView.addTextChangedListener(validationTextWatcher);
}
private String getDefaultPort(ConnectionSecurity securityType) {
String port;
switch (securityType) {
case NONE:
case STARTTLS_REQUIRED:
port = mDefaultPort;
break;
case SSL_TLS_REQUIRED:
port = mDefaultSslPort;
break;
default:
Log.e(K9.LOG_TAG, "Unhandled ConnectionSecurity type encountered");
port = "";
}
return port;
}
private void updateAuthPlainTextFromSecurityType(ConnectionSecurity securityType) {
mAuthTypeAdapter.useInsecureText(securityType == ConnectionSecurity.NONE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (Intent.ACTION_EDIT.equals(getIntent().getAction())) {
boolean isPushCapable = false;
try {
Store store = mAccount.getRemoteStore();
isPushCapable = store.isPushCapable();
} catch (Exception e) {
Log.e(K9.LOG_TAG, "Could not get remote store", e);
}
if (isPushCapable && mAccount.getFolderPushMode() != FolderMode.NONE) {
MailService.actionRestartPushers(this, null);
}
mAccount.save(Preferences.getPreferences(this));
finish();
} else {
/*
* Set the username and password for the outgoing settings to the username and
* password the user just set for incoming.
*/
try {
2014-05-25 16:45:14 -04:00
String username = mUsernameView.getText().toString();
String password = null;
String clientCertificateAlias = null;
AuthType authType = getSelectedAuthType();
if (AuthType.EXTERNAL == authType) {
2014-05-25 16:45:14 -04:00
clientCertificateAlias = mClientCertificateSpinner.getAlias();
} else {
password = mPasswordView.getText().toString();
}
URI oldUri = new URI(mAccount.getTransportUri());
2014-05-25 16:45:14 -04:00
ServerSettings transportServer = new ServerSettings(SmtpTransport.TRANSPORT_TYPE, oldUri.getHost(), oldUri.getPort(),
ConnectionSecurity.SSL_TLS_REQUIRED, authType, username, password, clientCertificateAlias);
String transportUri = Transport.createTransportUri(transportServer);
mAccount.setTransportUri(transportUri);
} catch (URISyntaxException use) {
/*
* If we can't set up the URL we just continue. It's only for
* convenience.
*/
}
AccountSetupOutgoing.actionOutgoingSettings(this, mAccount, mMakeDefault);
finish();
}
}
}
protected void onNext() {
try {
ConnectionSecurity connectionSecurity = getSelectedSecurity();
String username = mUsernameView.getText().toString();
2014-05-25 16:45:14 -04:00
String password = null;
String clientCertificateAlias = null;
AuthType authType = getSelectedAuthType();
if (authType == AuthType.EXTERNAL) {
2014-05-25 16:45:14 -04:00
clientCertificateAlias = mClientCertificateSpinner.getAlias();
} else {
password = mPasswordView.getText().toString();
}
String host = mServerView.getText().toString();
int port = Integer.parseInt(mPortView.getText().toString());
Map<String, String> extra = null;
if (ImapStore.STORE_TYPE.equals(mStoreType)) {
extra = new HashMap<String, String>();
extra.put(ImapStoreSettings.AUTODETECT_NAMESPACE_KEY,
Boolean.toString(mImapAutoDetectNamespaceView.isChecked()));
extra.put(ImapStoreSettings.PATH_PREFIX_KEY,
mImapPathPrefixView.getText().toString());
} else if (WebDavStore.STORE_TYPE.equals(mStoreType)) {
extra = new HashMap<String, String>();
extra.put(WebDavStoreSettings.PATH_KEY,
mWebdavPathPrefixView.getText().toString());
extra.put(WebDavStoreSettings.AUTH_PATH_KEY,
mWebdavAuthPathView.getText().toString());
extra.put(WebDavStoreSettings.MAILBOX_PATH_KEY,
mWebdavMailboxPathView.getText().toString());
}
mAccount.deleteCertificate(host, port, CheckDirection.INCOMING);
ServerSettings settings = new ServerSettings(mStoreType, host, port,
2014-05-25 16:45:14 -04:00
connectionSecurity, authType, username, password, clientCertificateAlias, extra);
mAccount.setStoreUri(RemoteStore.createStoreUri(settings));
mAccount.setCompression(NetworkType.MOBILE, mCompressionMobile.isChecked());
mAccount.setCompression(NetworkType.WIFI, mCompressionWifi.isChecked());
mAccount.setCompression(NetworkType.OTHER, mCompressionOther.isChecked());
mAccount.setSubscribedFoldersOnly(mSubscribedFoldersOnly.isChecked());
2010-05-11 22:51:59 -04:00
Fix inadequate certificate validation Proper host name validation was not being performed for certificates kept in the local keystore. If an attacker could convince a user to accept and store an attacker's certificate, then that certificate could be used for MITM attacks, giving the attacker access to all connections to all servers in all accounts in K-9. This commit changes how the certificates are stored. Previously, an entire certificate chain was stored for a server (and any of those certificates in the chain were available for validating signatures on certificates received when connecting). Now just the single certificate for the server is stored. This commit changes how locally stored certificates are retrieved. They can only be retrieved using the host:port that the user configured for the server. This also fixes issue 1326. Users can now use different certificates for different servers on the same host (listening to different ports). The above changes mean that users might have to re-accept certificates that they had previously accepted and are still using (but only if the certificate's Subject doesn't match the host that they are connecting to). This commit modifies AccountSetupBasics so that it now calls AccountSetupCheckSettings twice -- once for checking the incoming settings and once for the outgoing settings. Otherwise, an exception could occur while checking incoming settings, the user could say continue (or the user could accept a certificate key), and the outgoing settings would not be checked. This also helps with determining if a certificate exception was for the incoming or outgoing server, which is needed if the user decides to add the certificate to the keystore.
2013-11-23 13:26:57 -05:00
AccountSetupCheckSettings.actionCheckSettings(this, mAccount, CheckDirection.INCOMING);
} catch (Exception e) {
failure(e);
}
2008-12-18 19:20:56 -05:00
}
public void onClick(View v) {
try {
switch (v.getId()) {
case R.id.next:
onNext();
break;
}
} catch (Exception e) {
failure(e);
}
}
private void failure(Exception use) {
Log.e(K9.LOG_TAG, "Failure", use);
String toastText = getString(R.string.account_setup_bad_uri, use.getMessage());
Toast toast = Toast.makeText(getApplication(), toastText, Toast.LENGTH_LONG);
toast.show();
}
2014-05-25 16:45:14 -04:00
2014-05-25 16:45:14 -04:00
/*
* Calls validateFields() which enables or disables the Next button
* based on the fields' validity.
*/
TextWatcher validationTextWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) {
validateFields();
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
/* unused */
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
/* unused */
}
};
OnClientCertificateChangedListener clientCertificateChangedListener = new OnClientCertificateChangedListener() {
@Override
public void onClientCertificateChanged(String alias) {
validateFields();
}
};
private AuthType getSelectedAuthType() {
AuthTypeHolder holder = (AuthTypeHolder) mAuthTypeView.getSelectedItem();
return holder.authType;
}
private ConnectionSecurity getSelectedSecurity() {
ConnectionSecurityHolder holder = (ConnectionSecurityHolder) mSecurityTypeView.getSelectedItem();
return holder.connectionSecurity;
}
}