extra: import at 3.0.1 beta 1
[mozilla-extra.git] / extensions / wallet / src / nsPasswordManager.cpp
blob7eacf0174e2cc40f5517a24c364425265dedc7fd
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Mike Calmus
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include "nsPasswordManager.h"
40 #include "nsPassword.h"
41 #include "singsign.h"
42 #include "wallet.h"
43 #include "nsReadableUtils.h"
45 ////////////////////////////////////////////////////////////////////////////////
47 class nsPasswordManagerEnumerator : public nsISimpleEnumerator
49 public:
51 NS_DECL_ISUPPORTS
53 nsPasswordManagerEnumerator(PRBool aDecrypt = PR_TRUE) :
54 mHostCount(0), mUserCount(0), mDecrypt(aDecrypt)
58 NS_IMETHOD HasMoreElements(PRBool *result)
60 *result = SINGSIGN_HostCount() > mHostCount;
61 return NS_OK;
64 NS_IMETHOD GetNext(nsISupports **result)
66 char * host;
67 PRUnichar * user;
68 PRUnichar * pswd;
69 nsresult rv = SINGSIGN_Enumerate(mHostCount, mUserCount++, mDecrypt, &host, &user, &pswd);
70 if (NS_FAILED(rv)) {
71 mUserCount = 0;
72 mHostCount++;
73 return rv;
75 if (mUserCount == SINGSIGN_UserCount(mHostCount)) {
76 mUserCount = 0;
77 mHostCount++;
79 nsIPassword *password = new nsPassword(host, user, pswd);
80 // note that memory is handed off to "new nsPassword" in a non-xpcom fashion
81 if (password == nsnull) {
82 nsMemory::Free(host);
83 nsMemory::Free(user);
84 nsMemory::Free(pswd);
85 return NS_ERROR_OUT_OF_MEMORY;
87 *result = password;
88 NS_ADDREF(*result);
89 return NS_OK;
92 virtual ~nsPasswordManagerEnumerator()
96 protected:
97 PRInt32 mHostCount;
98 PRInt32 mUserCount;
99 PRBool mDecrypt;
102 NS_IMPL_ISUPPORTS1(nsPasswordManagerEnumerator, nsISimpleEnumerator)
104 ////////////////////////////////////////////////////////////////////////////////
106 class nsPasswordManagerRejectEnumerator : public nsISimpleEnumerator
108 public:
110 NS_DECL_ISUPPORTS
112 nsPasswordManagerRejectEnumerator() : mRejectCount(0)
116 NS_IMETHOD HasMoreElements(PRBool *result)
118 *result = SINGSIGN_RejectCount() > mRejectCount;
119 return NS_OK;
122 NS_IMETHOD GetNext(nsISupports **result)
124 char * host;
125 nsresult rv = SINGSIGN_RejectEnumerate(mRejectCount++, &host);
126 if (NS_FAILED(rv)) {
127 return rv;
130 nsIPassword *password = new nsPassword(host, nsnull, nsnull); /* only first argument used */
131 if (password == nsnull) {
132 nsMemory::Free(host);
133 return NS_ERROR_OUT_OF_MEMORY;
135 *result = password;
136 NS_ADDREF(*result);
137 return NS_OK;
140 virtual ~nsPasswordManagerRejectEnumerator()
144 protected:
145 PRInt32 mRejectCount;
148 NS_IMPL_ISUPPORTS1(nsPasswordManagerRejectEnumerator, nsISimpleEnumerator)
150 ////////////////////////////////////////////////////////////////////////////////
151 // nsPasswordManager Implementation
153 NS_IMPL_ISUPPORTS3(nsPasswordManager, nsIPasswordManager, nsIPasswordManagerInternal, nsISupportsWeakReference)
155 nsPasswordManager::nsPasswordManager()
159 nsPasswordManager::~nsPasswordManager(void)
161 SI_ShutdownModule();
164 nsresult nsPasswordManager::Init()
166 return NS_OK;
169 NS_IMETHODIMP nsPasswordManager::GetEnumerator(nsISimpleEnumerator * *entries)
171 *entries = nsnull;
172 nsPasswordManagerEnumerator* enumerator = new nsPasswordManagerEnumerator();
173 if (enumerator == nsnull) {
174 return NS_ERROR_OUT_OF_MEMORY;
176 NS_ADDREF(enumerator);
177 *entries = enumerator;
178 return NS_OK;
181 NS_IMETHODIMP nsPasswordManager::AddUser(const nsACString& aHost, const nsAString& aUser, const nsAString& aPwd) {
182 SINGSIGN_StorePassword(PromiseFlatCString(aHost).get(),
183 PromiseFlatString(aUser).get(),
184 PromiseFlatString(aPwd).get());
185 return NS_OK;
188 NS_IMETHODIMP nsPasswordManager::RemoveUser(const nsACString& aHost, const nsAString& aUser)
190 return ::SINGSIGN_RemoveUser(PromiseFlatCString(aHost).get(),
191 PromiseFlatString(aUser).get(), PR_TRUE);
194 NS_IMETHODIMP nsPasswordManager::GetRejectEnumerator(nsISimpleEnumerator * *entries)
196 *entries = nsnull;
197 nsPasswordManagerRejectEnumerator* enumerator = new nsPasswordManagerRejectEnumerator();
198 if (enumerator == nsnull) {
199 return NS_ERROR_OUT_OF_MEMORY;
201 NS_ADDREF(enumerator);
202 *entries = enumerator;
203 return NS_OK;
206 NS_IMETHODIMP nsPasswordManager::RemoveReject(const nsACString& aHost)
208 return ::SINGSIGN_RemoveReject(PromiseFlatCString(aHost).get());
211 NS_IMETHODIMP
212 nsPasswordManager::FindPasswordEntry
213 (const nsACString& aHostURI, const nsAString& aUsername, const nsAString& aPassword,
214 nsACString& aHostURIFound, nsAString& aUsernameFound, nsAString& aPasswordFound)
216 nsresult rv;
217 nsCOMPtr<nsIPassword> passwordElem;
219 nsCOMPtr<nsISimpleEnumerator> enumerator = new nsPasswordManagerEnumerator(PR_FALSE);
220 if (!enumerator) {
221 return NS_ERROR_OUT_OF_MEMORY;
224 PRBool hasMoreElements = PR_FALSE;
225 enumerator->HasMoreElements(&hasMoreElements);
227 // Emumerate through set of saved logins
228 while (hasMoreElements) {
229 rv = enumerator->GetNext(getter_AddRefs(passwordElem));
230 if (NS_FAILED(rv))
231 return rv;
233 if (passwordElem) {
235 // Get the contents of this saved login
236 nsCAutoString hostURI;
237 nsAutoString encUsername, username;
238 nsAutoString encPassword, password;
240 passwordElem->GetHost(hostURI);
241 passwordElem->GetUser(encUsername);
242 passwordElem->GetPassword(encPassword);
244 // Check for a match with the input parameters, treating null input values as
245 // wild cards
246 PRBool hostURIOK = aHostURI.IsEmpty() || hostURI.Equals(aHostURI);
247 if (hostURIOK) {
248 rv = Wallet_Decrypt(encUsername, username);
249 if (NS_FAILED(rv)) {
250 return rv;
252 rv = Wallet_Decrypt(encPassword, password);
253 if (NS_FAILED(rv)) {
254 return rv;
258 PRBool usernameOK = aUsername.IsEmpty() || username.Equals(aUsername);
259 PRBool passwordOK = aPassword.IsEmpty() || password.Equals(aPassword);
261 // If a match is found, return success
262 if (hostURIOK && usernameOK && passwordOK) {
263 aHostURIFound = hostURI;
264 aUsernameFound = username;
265 aPasswordFound = password;
266 return NS_OK;
269 enumerator->HasMoreElements(&hasMoreElements);
272 // no match found
273 return NS_ERROR_FAILURE;
276 NS_IMETHODIMP nsPasswordManager::AddReject(const nsACString& host)
278 return ::SINGSIGN_AddReject(PromiseFlatCString(host).get());
281 NS_IMETHODIMP
282 nsPasswordManager::AddUserFull(const nsACString& aKey,
283 const nsAString& aUser,
284 const nsAString& aPassword,
285 const nsAString& aUserFieldName,
286 const nsAString& aPassFieldName)
288 return NS_ERROR_NOT_IMPLEMENTED;
291 NS_IMETHODIMP
292 nsPasswordManager::AddUserFull2(const nsACString& aKey,
293 const nsAString& aUser,
294 const nsAString& aPassword,
295 const nsAString& aUserFieldName,
296 const nsAString& aPassFieldName,
297 const nsACString& aActionURL)
299 return NS_ERROR_NOT_IMPLEMENTED;
302 NS_IMETHODIMP nsPasswordManager::ReadPasswords(nsIFile* aPasswordFile)
304 return NS_ERROR_NOT_IMPLEMENTED;