Bug 1315662 - delete SMTP server login credentials when deleting the account or if hostname/username changes.

Tag #1273
This commit is contained in:
Matt A. Tobin 2019-11-11 01:26:42 -05:00 • committed by Roy Tam
commit 97e994c45a
3 changed files with 47 additions and 18 deletions

View file

@ -54,6 +54,11 @@ var gSmtpServerListWindow =
if (!cancel) if (!cancel)
{ {
// Remove password information first.
try {
server.forgetPassword();
} catch (e) { /* It is OK if this fails. */ }
// Remove the server.
MailServices.smtp.deleteServer(server); MailServices.smtp.deleteServer(server);
parent.replaceWithDefaultSmtpServer(server.key); parent.replaceWithDefaultSmtpServer(server.key);
this.refreshServerList("", true); this.refreshServerList("", true);

View file

@ -121,6 +121,18 @@ nsresult nsSmtpServer::getPrefs()
return NS_OK; return NS_OK;
} }
// This function is intentionally called the same as in nsIMsgIncomingServer.
nsresult
nsSmtpServer::OnUserOrHostNameChanged(const nsACString& oldName,
const nsACString& newName,
bool hostnameChanged)
{
// Reset password so that users are prompted for new password for the new user/host.
(void)ForgetPassword();
return NS_OK;
}
NS_IMETHODIMP NS_IMETHODIMP
nsSmtpServer::GetHostname(nsACString &aHostname) nsSmtpServer::GetHostname(nsACString &aHostname)
{ {
@ -137,12 +149,22 @@ nsSmtpServer::GetHostname(nsACString &aHostname)
NS_IMETHODIMP NS_IMETHODIMP
nsSmtpServer::SetHostname(const nsACString &aHostname) nsSmtpServer::SetHostname(const nsACString &aHostname)
{ {
nsCString oldName;
nsresult rv = GetHostname(oldName);
NS_ENSURE_SUCCESS(rv, rv);
// A few things to take care of if we're changing the hostname.
if (!oldName.Equals(aHostname, nsCaseInsensitiveCStringComparator())) {
rv = OnUserOrHostNameChanged(oldName, aHostname, true);
NS_ENSURE_SUCCESS(rv, rv);
}
if (!aHostname.IsEmpty()) if (!aHostname.IsEmpty())
return mPrefBranch->SetCharPref("hostname", PromiseFlatCString(aHostname).get()); return mPrefBranch->SetCharPref("hostname", PromiseFlatCString(aHostname).get());
// If the pref value is already empty, ClearUserPref will return // If the pref value is already empty, ClearUserPref will return
// NS_ERROR_UNEXPECTED, so don't check the rv here. // NS_ERROR_UNEXPECTED, so don't check the rv here.
mPrefBranch->ClearUserPref("hostname"); (void)mPrefBranch->ClearUserPref("hostname");
return NS_OK; return NS_OK;
} }
@ -284,12 +306,22 @@ nsSmtpServer::GetUsername(nsACString &aUsername)
NS_IMETHODIMP NS_IMETHODIMP
nsSmtpServer::SetUsername(const nsACString &aUsername) nsSmtpServer::SetUsername(const nsACString &aUsername)
{ {
// Need to take care of few things if we're changing the username.
nsCString oldName;
nsresult rv = GetUsername(oldName);
NS_ENSURE_SUCCESS(rv, rv);
if (!oldName.Equals(aUsername)) {
rv = OnUserOrHostNameChanged(oldName, aUsername, false);
NS_ENSURE_SUCCESS(rv, rv);
}
if (!aUsername.IsEmpty()) if (!aUsername.IsEmpty())
return mPrefBranch->SetCharPref("username", PromiseFlatCString(aUsername).get()); return mPrefBranch->SetCharPref("username", PromiseFlatCString(aUsername).get());
// If the pref value is already empty, ClearUserPref will return // If the pref value is already empty, ClearUserPref will return
// NS_ERROR_UNEXPECTED, so don't check the rv here. // NS_ERROR_UNEXPECTED, so don't check the rv here.
mPrefBranch->ClearUserPref("username"); (void)mPrefBranch->ClearUserPref("username");
return NS_OK; return NS_OK;
} }
@ -575,22 +607,10 @@ nsSmtpServer::ForgetPassword()
NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_SUCCESS(rv, rv);
// Get the current server URI without the username // Get the current server URI without the username
nsAutoCString serverUri(NS_LITERAL_CSTRING("smtp://"));
nsCString hostname;
rv = GetHostname(hostname);
if (NS_SUCCEEDED(rv) && !hostname.IsEmpty()) {
nsCString escapedHostname;
MsgEscapeString(hostname, nsINetUtil::ESCAPE_URL_PATH, escapedHostname);
// not all servers have a hostname
serverUri.Append(escapedHostname);
}
uint32_t count; uint32_t count;
nsILoginInfo** logins; nsILoginInfo** logins;
NS_ConvertUTF8toUTF16 currServer(serverUri); NS_ConvertASCIItoUTF16 serverUri(GetServerURIInternal(false));
nsCString serverCUsername; nsCString serverCUsername;
rv = GetUsername(serverCUsername); rv = GetUsername(serverCUsername);
@ -598,8 +618,8 @@ nsSmtpServer::ForgetPassword()
NS_ConvertUTF8toUTF16 serverUsername(serverCUsername); NS_ConvertUTF8toUTF16 serverUsername(serverCUsername);
rv = loginMgr->FindLogins(&count, currServer, EmptyString(), rv = loginMgr->FindLogins(&count, serverUri, EmptyString(),
currServer, &logins); serverUri, &logins);
NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_SUCCESS(rv, rv);
// There should only be one-login stored for this url, however just in case // There should only be one-login stored for this url, however just in case

View file

@ -37,6 +37,10 @@ private:
nsresult GetPasswordWithoutUI(); nsresult GetPasswordWithoutUI();
nsCString GetServerURIInternal(const bool aIncludeUsername); nsCString GetServerURIInternal(const bool aIncludeUsername);
nsresult OnUserOrHostNameChanged(const nsACString& oldName,
const nsACString& newName,
bool hostnameChanged);
nsCString m_password; nsCString m_password;
bool m_logonFailed; bool m_logonFailed;
}; };