Back to index

lightning-sunbird  0.9+nobinonly
nsPasswordManager.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Mike Calmus
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsPasswordManager.h"
00040 #include "nsPassword.h"
00041 #include "singsign.h"
00042 #include "wallet.h"
00043 #include "nsReadableUtils.h"
00044 
00046 
00047 class nsPasswordManagerEnumerator : public nsISimpleEnumerator
00048 {
00049   public:
00050 
00051     NS_DECL_ISUPPORTS
00052 
00053     nsPasswordManagerEnumerator(PRBool aDecrypt = PR_TRUE) : 
00054       mHostCount(0), mUserCount(0), mDecrypt(aDecrypt)
00055     {
00056     }
00057 
00058     NS_IMETHOD HasMoreElements(PRBool *result) 
00059     {
00060       *result = SINGSIGN_HostCount() > mHostCount;
00061       return NS_OK;
00062     }
00063 
00064     NS_IMETHOD GetNext(nsISupports **result) 
00065     {
00066       char * host;
00067       PRUnichar * user;
00068       PRUnichar * pswd;
00069       nsresult rv = SINGSIGN_Enumerate(mHostCount, mUserCount++, mDecrypt, &host, &user, &pswd);
00070       if (NS_FAILED(rv)) {
00071         mUserCount = 0;
00072         mHostCount++;
00073         return rv;
00074       }
00075       if (mUserCount == SINGSIGN_UserCount(mHostCount)) {
00076         mUserCount = 0;
00077         mHostCount++;
00078       }
00079       nsIPassword *password = new nsPassword(host, user, pswd);
00080       // note that memory is handed off to "new nsPassword" in a non-xpcom fashion 
00081       if (password == nsnull) {
00082         nsMemory::Free(host);
00083         nsMemory::Free(user);
00084         nsMemory::Free(pswd);
00085         return NS_ERROR_OUT_OF_MEMORY;
00086       }
00087       *result = password;
00088       NS_ADDREF(*result);
00089       return NS_OK;
00090     }
00091 
00092     virtual ~nsPasswordManagerEnumerator() 
00093     {
00094     }
00095 
00096   protected:
00097     PRInt32 mHostCount;
00098     PRInt32 mUserCount;
00099     PRBool  mDecrypt;
00100 };
00101 
00102 NS_IMPL_ISUPPORTS1(nsPasswordManagerEnumerator, nsISimpleEnumerator)
00103 
00104 
00105 
00106 class nsPasswordManagerRejectEnumerator : public nsISimpleEnumerator
00107 {
00108   public:
00109 
00110     NS_DECL_ISUPPORTS
00111 
00112     nsPasswordManagerRejectEnumerator() : mRejectCount(0)
00113     {
00114     }
00115 
00116     NS_IMETHOD HasMoreElements(PRBool *result) 
00117     {
00118       *result = SINGSIGN_RejectCount() > mRejectCount;
00119       return NS_OK;
00120     }
00121 
00122     NS_IMETHOD GetNext(nsISupports **result) 
00123     {
00124       char * host;
00125       nsresult rv = SINGSIGN_RejectEnumerate(mRejectCount++, &host);
00126       if (NS_FAILED(rv)) {
00127         return rv;
00128       }
00129 
00130       nsIPassword *password = new nsPassword(host, nsnull, nsnull); /* only first argument used */
00131       if (password == nsnull) {
00132         nsMemory::Free(host);
00133         return NS_ERROR_OUT_OF_MEMORY;
00134       }
00135       *result = password;
00136       NS_ADDREF(*result);
00137       return NS_OK;
00138     }
00139 
00140     virtual ~nsPasswordManagerRejectEnumerator() 
00141     {
00142     }
00143 
00144   protected:
00145     PRInt32 mRejectCount;
00146 };
00147 
00148 NS_IMPL_ISUPPORTS1(nsPasswordManagerRejectEnumerator, nsISimpleEnumerator)
00149 
00150 
00151 // nsPasswordManager Implementation
00152 
00153 NS_IMPL_ISUPPORTS3(nsPasswordManager, nsIPasswordManager, nsIPasswordManagerInternal, nsISupportsWeakReference)
00154  
00155 nsPasswordManager::nsPasswordManager()
00156 {
00157 }
00158 
00159 nsPasswordManager::~nsPasswordManager(void)
00160 {
00161 }
00162 
00163 nsresult nsPasswordManager::Init()
00164 {
00165   return NS_OK;
00166 }
00167 
00168 NS_IMETHODIMP nsPasswordManager::GetEnumerator(nsISimpleEnumerator * *entries)
00169 {
00170   *entries = nsnull;
00171   nsPasswordManagerEnumerator* enumerator = new nsPasswordManagerEnumerator();
00172   if (enumerator == nsnull) {
00173     return NS_ERROR_OUT_OF_MEMORY;
00174   }
00175   NS_ADDREF(enumerator);
00176   *entries = enumerator;
00177   return NS_OK;
00178 }
00179 
00180 NS_IMETHODIMP nsPasswordManager::AddUser(const nsACString& aHost, const nsAString& aUser, const nsAString& aPwd) {
00181   SINGSIGN_StorePassword(PromiseFlatCString(aHost).get(),
00182                          PromiseFlatString(aUser).get(),
00183                          PromiseFlatString(aPwd).get());
00184   return NS_OK;
00185 }
00186 
00187 NS_IMETHODIMP nsPasswordManager::RemoveUser(const nsACString& aHost, const nsAString& aUser)
00188 {
00189   return ::SINGSIGN_RemoveUser(PromiseFlatCString(aHost).get(),
00190                                PromiseFlatString(aUser).get(), PR_TRUE);
00191 }
00192 
00193 NS_IMETHODIMP nsPasswordManager::GetRejectEnumerator(nsISimpleEnumerator * *entries)
00194 {
00195   *entries = nsnull;
00196   nsPasswordManagerRejectEnumerator* enumerator = new nsPasswordManagerRejectEnumerator();
00197   if (enumerator == nsnull) {
00198     return NS_ERROR_OUT_OF_MEMORY;
00199   }
00200   NS_ADDREF(enumerator);
00201   *entries = enumerator;
00202   return NS_OK;
00203 }
00204 
00205 NS_IMETHODIMP nsPasswordManager::RemoveReject(const nsACString& aHost)
00206 {
00207   return ::SINGSIGN_RemoveReject(PromiseFlatCString(aHost).get());
00208 }
00209 
00210 NS_IMETHODIMP
00211 nsPasswordManager::FindPasswordEntry
00212   (const nsACString& aHostURI, const nsAString& aUsername, const nsAString& aPassword,
00213    nsACString& aHostURIFound, nsAString& aUsernameFound, nsAString& aPasswordFound)
00214 {
00215   nsresult rv;
00216   nsCOMPtr<nsIPassword> passwordElem;
00217 
00218   nsCOMPtr<nsISimpleEnumerator> enumerator = new nsPasswordManagerEnumerator(PR_FALSE);
00219   if (!enumerator) {
00220     return NS_ERROR_OUT_OF_MEMORY;
00221   }
00222 
00223   PRBool hasMoreElements = PR_FALSE;
00224   enumerator->HasMoreElements(&hasMoreElements);
00225 
00226   // Emumerate through set of saved logins
00227   while (hasMoreElements) {
00228     rv = enumerator->GetNext(getter_AddRefs(passwordElem));
00229     if (NS_FAILED(rv))
00230       return rv;
00231 
00232     if (passwordElem) {
00233 
00234       // Get the contents of this saved login
00235       nsCAutoString hostURI;
00236       nsAutoString encUsername, username;
00237       nsAutoString encPassword, password;
00238 
00239       passwordElem->GetHost(hostURI);
00240       passwordElem->GetUser(encUsername);
00241       passwordElem->GetPassword(encPassword);
00242 
00243       // Check for a match with the input parameters, treating null input values as
00244       // wild cards
00245       PRBool hostURIOK  = aHostURI.IsEmpty()  || hostURI.Equals(aHostURI);
00246       if (hostURIOK) {
00247         rv = Wallet_Decrypt(encUsername, username);
00248         if (NS_FAILED(rv)) {
00249           return rv;
00250         }
00251         rv = Wallet_Decrypt(encPassword, password);
00252         if (NS_FAILED(rv)) {
00253           return rv;
00254         }
00255       }
00256 
00257       PRBool usernameOK = aUsername.IsEmpty() || username.Equals(aUsername);
00258       PRBool passwordOK = aPassword.IsEmpty() || password.Equals(aPassword);
00259 
00260       // If a match is found, return success
00261       if (hostURIOK && usernameOK && passwordOK) {
00262         aHostURIFound = hostURI;
00263         aUsernameFound = username;
00264         aPasswordFound = password;
00265         return NS_OK;
00266       }
00267     }
00268     enumerator->HasMoreElements(&hasMoreElements);
00269   }
00270 
00271   // no match found
00272   return NS_ERROR_FAILURE;
00273 }
00274 
00275 NS_IMETHODIMP nsPasswordManager::AddReject(const nsACString& host)
00276 {
00277   return ::SINGSIGN_AddReject(PromiseFlatCString(host).get());
00278 }
00279 
00280 NS_IMETHODIMP
00281 nsPasswordManager::AddUserFull(const nsACString& aKey,
00282                                const nsAString& aUser,
00283                                const nsAString& aPassword,
00284                                const nsAString& aUserFieldName,
00285                                const nsAString& aPassFieldName)
00286 {
00287   return NS_ERROR_NOT_IMPLEMENTED;
00288 }
00289 
00290 NS_IMETHODIMP nsPasswordManager::ReadPasswords(nsIFile* aPasswordFile)
00291 {
00292   return NS_ERROR_NOT_IMPLEMENTED;
00293 }
00294