Allow untrusted /quick setup wizard

This commit is contained in:
M66B 2020-05-18 21:41:56 +02:00
parent 59d3428c2e
commit 3fa68990d2
2 changed files with 68 additions and 10 deletions

View File

@ -71,7 +71,9 @@ public class FragmentQuickSetup extends FragmentBase {
private TextView tvInstructions; private TextView tvInstructions;
private TextView tvImap; private TextView tvImap;
private TextView tvImapFingerprint;
private TextView tvSmtp; private TextView tvSmtp;
private TextView tvSmtpFingerprint;
private Button btnSave; private Button btnSave;
private ContentLoadingProgressBar pbSave; private ContentLoadingProgressBar pbSave;
@ -102,7 +104,9 @@ public class FragmentQuickSetup extends FragmentBase {
tvInstructions = view.findViewById(R.id.tvInstructions); tvInstructions = view.findViewById(R.id.tvInstructions);
tvImap = view.findViewById(R.id.tvImap); tvImap = view.findViewById(R.id.tvImap);
tvImapFingerprint = view.findViewById(R.id.tvImapFingerprint);
tvSmtp = view.findViewById(R.id.tvSmtp); tvSmtp = view.findViewById(R.id.tvSmtp);
tvSmtpFingerprint = view.findViewById(R.id.tvSmtpFingerprint);
btnSave = view.findViewById(R.id.btnSave); btnSave = view.findViewById(R.id.btnSave);
pbSave = view.findViewById(R.id.pbSave); pbSave = view.findViewById(R.id.pbSave);
@ -177,6 +181,8 @@ public class FragmentQuickSetup extends FragmentBase {
// Initialize // Initialize
tvCharacters.setVisibility(View.GONE); tvCharacters.setVisibility(View.GONE);
tvImapFingerprint.setText(null);
tvSmtpFingerprint.setText(null);
pbCheck.setVisibility(View.GONE); pbCheck.setVisibility(View.GONE);
pbSave.setVisibility(View.GONE); pbSave.setVisibility(View.GONE);
btnHelp.setVisibility(View.GONE); btnHelp.setVisibility(View.GONE);
@ -220,6 +226,8 @@ public class FragmentQuickSetup extends FragmentBase {
args.putString("name", etName.getText().toString().trim()); args.putString("name", etName.getText().toString().trim());
args.putString("email", etEmail.getText().toString().trim()); args.putString("email", etEmail.getText().toString().trim());
args.putString("password", tilPassword.getEditText().getText().toString()); args.putString("password", tilPassword.getEditText().getText().toString());
args.putString("imap_fingerprint", tvImapFingerprint.getText().toString());
args.putString("smtp_fingerprint", tvSmtpFingerprint.getText().toString());
args.putBoolean("check", check); args.putBoolean("check", check);
new SimpleTask<EmailProvider>() { new SimpleTask<EmailProvider>() {
@ -250,6 +258,8 @@ public class FragmentQuickSetup extends FragmentBase {
String email = args.getString("email"); String email = args.getString("email");
String password = args.getString("password"); String password = args.getString("password");
boolean check = args.getBoolean("check"); boolean check = args.getBoolean("check");
String imap_fingerprint = args.getString("imap_fingerprint");
String smtp_fingerprint = args.getString("smtp_fingerprint");
if (TextUtils.isEmpty(name)) if (TextUtils.isEmpty(name))
throw new IllegalArgumentException(context.getString(R.string.title_no_name)); throw new IllegalArgumentException(context.getString(R.string.title_no_name));
@ -259,6 +269,10 @@ public class FragmentQuickSetup extends FragmentBase {
throw new IllegalArgumentException(context.getString(R.string.title_email_invalid, email)); throw new IllegalArgumentException(context.getString(R.string.title_email_invalid, email));
if (TextUtils.isEmpty(password)) if (TextUtils.isEmpty(password))
throw new IllegalArgumentException(context.getString(R.string.title_no_password)); throw new IllegalArgumentException(context.getString(R.string.title_no_password));
if (TextUtils.isEmpty(imap_fingerprint))
imap_fingerprint = null;
if (TextUtils.isEmpty(smtp_fingerprint))
smtp_fingerprint = null;
EmailProvider provider = EmailProvider.fromEmail(context, email, EmailProvider.Discover.ALL); EmailProvider provider = EmailProvider.fromEmail(context, email, EmailProvider.Discover.ALL);
args.putBoolean("appPassword", provider.appPassword); args.putBoolean("appPassword", provider.appPassword);
@ -274,17 +288,24 @@ public class FragmentQuickSetup extends FragmentBase {
String user = (provider.user == EmailProvider.UserType.EMAIL ? email : username); String user = (provider.user == EmailProvider.UserType.EMAIL ? email : username);
Log.i("User type=" + provider.user + " name=" + user); Log.i("User type=" + provider.user + " name=" + user);
List<EntityFolder> folders; List<EntityFolder> folders = null;
String aprotocol = provider.imap.starttls ? "imap" : "imaps"; String aprotocol = provider.imap.starttls ? "imap" : "imaps";
try (EmailService iservice = new EmailService( try (EmailService iservice = new EmailService(
context, aprotocol, null, false, EmailService.PURPOSE_CHECK, true)) { context, aprotocol, null, false, EmailService.PURPOSE_CHECK, true)) {
boolean check_folders = false;
try { try {
iservice.connect( iservice.connect(
provider.imap.host, provider.imap.port, provider.imap.host, provider.imap.port,
EmailService.AUTH_TYPE_PASSWORD, null, EmailService.AUTH_TYPE_PASSWORD, null,
user, password, user, password,
null, null); null, imap_fingerprint);
check_folders = true;
} catch (EmailService.UntrustedException ex) {
if (check)
imap_fingerprint = ex.getFingerprint();
else
throw ex;
} catch (AuthenticationFailedException ex) { } catch (AuthenticationFailedException ex) {
if (!user.equals(username)) { if (!user.equals(username)) {
Log.w(ex); Log.w(ex);
@ -299,10 +320,12 @@ public class FragmentQuickSetup extends FragmentBase {
throw ex; throw ex;
} }
folders = iservice.getFolders(); if (check_folders) {
folders = iservice.getFolders();
if (folders == null) if (folders == null)
throw new IllegalArgumentException(context.getString(R.string.title_setup_no_system_folders)); throw new IllegalArgumentException(context.getString(R.string.title_setup_no_system_folders));
}
} }
String iprotocol = provider.smtp.starttls ? "smtp" : "smtps"; String iprotocol = provider.smtp.starttls ? "smtp" : "smtps";
@ -313,11 +336,19 @@ public class FragmentQuickSetup extends FragmentBase {
provider.smtp.host, provider.smtp.port, provider.smtp.host, provider.smtp.port,
EmailService.AUTH_TYPE_PASSWORD, null, EmailService.AUTH_TYPE_PASSWORD, null,
user, password, user, password,
null, null); null, smtp_fingerprint);
} catch (EmailService.UntrustedException ex) {
if (check)
smtp_fingerprint = ex.getFingerprint();
else
throw ex;
} }
if (check) if (check) {
args.putString("imap_fingerprint", imap_fingerprint);
args.putString("smtp_fingerprint", smtp_fingerprint);
return provider; return provider;
}
DB db = DB.getInstance(context); DB db = DB.getInstance(context);
try { try {
@ -334,6 +365,7 @@ public class FragmentQuickSetup extends FragmentBase {
account.auth_type = EmailService.AUTH_TYPE_PASSWORD; account.auth_type = EmailService.AUTH_TYPE_PASSWORD;
account.user = user; account.user = user;
account.password = password; account.password = password;
account.fingerprint = imap_fingerprint;
account.name = provider.name; account.name = provider.name;
@ -383,6 +415,7 @@ public class FragmentQuickSetup extends FragmentBase {
identity.auth_type = EmailService.AUTH_TYPE_PASSWORD; identity.auth_type = EmailService.AUTH_TYPE_PASSWORD;
identity.user = user; identity.user = user;
identity.password = password; identity.password = password;
identity.fingerprint = smtp_fingerprint;
identity.use_ip = provider.useip; identity.use_ip = provider.useip;
identity.synchronize = true; identity.synchronize = true;
identity.primary = true; identity.primary = true;
@ -406,8 +439,10 @@ public class FragmentQuickSetup extends FragmentBase {
if (check) { if (check) {
tvImap.setText(result == null ? null tvImap.setText(result == null ? null
: result.imap.host + ":" + result.imap.port + (result.imap.starttls ? " starttls" : " ssl")); : result.imap.host + ":" + result.imap.port + (result.imap.starttls ? " starttls" : " ssl"));
tvImapFingerprint.setText(args.getString("imap_fingerprint"));
tvSmtp.setText(result == null ? null tvSmtp.setText(result == null ? null
: result.smtp.host + ":" + result.smtp.port + (result.smtp.starttls ? " starttls" : " ssl")); : result.smtp.host + ":" + result.smtp.port + (result.smtp.starttls ? " starttls" : " ssl"));
tvSmtpFingerprint.setText(args.getString("smtp_fingerprint"));
grpSetup.setVisibility(result == null ? View.GONE : View.VISIBLE); grpSetup.setVisibility(result == null ? View.GONE : View.VISIBLE);
} else { } else {
FragmentReview fragment = new FragmentReview(); FragmentReview fragment = new FragmentReview();

View File

@ -193,6 +193,16 @@
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvImapTitle" /> app:layout_constraintTop_toBottomOf="@id/tvImapTitle" />
<eu.faircode.email.FixedTextView
android:id="@+id/tvImapFingerprint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fingerprint"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="?attr/colorWarning"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvImap" />
<eu.faircode.email.FixedTextView <eu.faircode.email.FixedTextView
android:id="@+id/tvSmtpTitle" android:id="@+id/tvSmtpTitle"
android:layout_width="wrap_content" android:layout_width="wrap_content"
@ -201,7 +211,7 @@
android:text="@string/title_setup_quick_smtp" android:text="@string/title_setup_quick_smtp"
android:textAppearance="@style/TextAppearance.AppCompat.Small" android:textAppearance="@style/TextAppearance.AppCompat.Small"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvImap" /> app:layout_constraintTop_toBottomOf="@id/tvImapFingerprint" />
<eu.faircode.email.FixedTextView <eu.faircode.email.FixedTextView
android:id="@+id/tvSmtp" android:id="@+id/tvSmtp"
@ -213,6 +223,16 @@
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvSmtpTitle" /> app:layout_constraintTop_toBottomOf="@id/tvSmtpTitle" />
<eu.faircode.email.FixedTextView
android:id="@+id/tvSmtpFingerprint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fingerprint"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="?attr/colorWarning"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvSmtp" />
<Button <Button
android:id="@+id/btnSave" android:id="@+id/btnSave"
android:layout_width="wrap_content" android:layout_width="wrap_content"
@ -221,7 +241,7 @@
android:tag="disable" android:tag="disable"
android:text="@string/title_save" android:text="@string/title_save"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvSmtp" /> app:layout_constraintTop_toBottomOf="@id/tvSmtpFingerprint" />
<eu.faircode.email.ContentLoadingProgressBar <eu.faircode.email.ContentLoadingProgressBar
android:id="@+id/pbSave" android:id="@+id/pbSave"
@ -238,7 +258,10 @@
android:id="@+id/grpSetup" android:id="@+id/grpSetup"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="0dp" android:layout_height="0dp"
app:constraint_referenced_ids="tvImapTitle,tvImap,tvSmtpTitle,tvSmtp,btnSave" /> app:constraint_referenced_ids="
tvImapTitle,tvImap,tvImapFingerprint,tvSmtpTitle,
tvSmtp,tvSmtpFingerprint,
btnSave" />
<androidx.constraintlayout.widget.Group <androidx.constraintlayout.widget.Group
android:id="@+id/grpError" android:id="@+id/grpError"