1 # -*- Mode: Java; 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
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) 2001
20 # the Initial Developer. All Rights Reserved.
23 # Bob Lord <lord@netscape.com>
24 # Terry Hayes <thayes@netscape.com>
26 # Alternatively, the contents of this file may be used under the terms of
27 # either the GNU General Public License Version 2 or later (the "GPL"), or
28 # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 # in which case the provisions of the GPL or the LGPL are applicable instead
30 # of those above. If you wish to allow use of your version of this file only
31 # under the terms of either the GPL or the LGPL, and not to allow others to
32 # use your version of this file under the terms of the MPL, indicate your
33 # decision by deleting the provisions above and replace them with the notice
34 # and other provisions required by the GPL or the LGPL. If you do not delete
35 # the provisions above, a recipient may use your version of this file under
36 # the terms of any one of the MPL, the GPL or the LGPL.
38 # ***** END LICENSE BLOCK *****
40 const nsPK11TokenDB = "@mozilla.org/security/pk11tokendb;1";
41 const nsIPK11TokenDB = Components.interfaces.nsIPK11TokenDB;
42 const nsIDialogParamBlock = Components.interfaces.nsIDialogParamBlock;
43 const nsPKCS11ModuleDB = "@mozilla.org/security/pkcs11moduledb;1";
44 const nsIPKCS11ModuleDB = Components.interfaces.nsIPKCS11ModuleDB;
45 const nsIPKCS11Slot = Components.interfaces.nsIPKCS11Slot;
46 const nsIPK11Token = Components.interfaces.nsIPK11Token;
55 pw1 = document.getElementById("pw1");
63 var secmoddb = Components.classes[nsPKCS11ModuleDB].getService(nsIPKCS11ModuleDB);
64 var bundle = document.getElementById("bundlePreferences");
66 // If the token is unitialized, don't use the old password box.
69 var slot = secmoddb.findSlotByName(tokenName);
71 var oldpwbox = document.getElementById("oldpw");
72 var msgBox = document.getElementById("message");
73 var status = slot.status;
74 if (status == nsIPKCS11Slot.SLOT_UNINITIALIZED
75 || status == nsIPKCS11Slot.SLOT_READY) {
77 oldpwbox.setAttribute("hidden", "true");
78 msgBox.setAttribute("value", bundle.getString("password_not_set"));
79 msgBox.setAttribute("hidden", "false");
81 if (status == nsIPKCS11Slot.SLOT_READY) {
82 oldpwbox.setAttribute("inited", "empty");
84 oldpwbox.setAttribute("inited", "true");
87 // Select first password field
88 document.getElementById('pw1').focus();
91 // Select old password field
92 oldpwbox.setAttribute("hidden", "false");
93 msgBox.setAttribute("hidden", "true");
94 oldpwbox.setAttribute("inited", "false");
100 // Return value 0 means "canceled"
107 function setPassword()
109 var pk11db = Components.classes[nsPK11TokenDB].getService(nsIPK11TokenDB);
110 var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
111 .getService(Components.interfaces.nsIPromptService);
112 var token = pk11db.findTokenByName(tokenName);
113 dump("*** TOKEN!!!! (name = |" + token + "|\n");
115 var oldpwbox = document.getElementById("oldpw");
116 var initpw = oldpwbox.getAttribute("inited");
117 var bundle = document.getElementById("bundlePreferences");
121 if (initpw == "false" || initpw == "empty") {
126 if (initpw == "empty") {
129 oldpw = oldpwbox.value;
130 passok = token.checkPassword(oldpw);
134 if (initpw == "empty" && pw1.value == "") {
135 // This makes no sense that we arrive here,
136 // we reached a case that should have been prevented by checkPasswords.
138 if (pw1.value == "") {
139 var secmoddb = Components.classes[nsPKCS11ModuleDB].getService(nsIPKCS11ModuleDB);
140 if (secmoddb.isFIPSEnabled) {
141 // empty passwords are not allowed in FIPS mode
142 promptService.alert(window,
143 bundle.getString("pw_change_failed_title"),
144 bundle.getString("pw_change2empty_in_fips_mode"));
149 token.changePassword(oldpw, pw1.value);
150 if (pw1.value == "") {
151 promptService.alert(window,
152 bundle.getString("pw_change_success_title"),
153 bundle.getString("pw_erased_ok")
154 + " " + bundle.getString("pw_empty_warning"));
156 promptService.alert(window,
157 bundle.getString("pw_change_success_title"),
158 bundle.getString("pw_change_ok"));
165 oldpwbox.setAttribute("value", "");
166 promptService.alert(window,
167 bundle.getString("pw_change_failed_title"),
168 bundle.getString("incorrect_pw"));
171 promptService.alert(window,
172 bundle.getString("pw_change_failed_title"),
173 bundle.getString("failed_pw_change"));
176 token.initPassword(pw1.value);
177 if (pw1.value == "") {
178 promptService.alert(window,
179 bundle.getString("pw_change_success_title"),
180 bundle.getString("pw_not_wanted")
181 + " " + bundle.getString("pw_empty_warning"));
191 function setPasswordStrength()
193 // Here is how we weigh the quality of the password
194 // number of characters
196 // non-alpha-numeric chars
197 // upper and lower case characters
199 var pw=document.getElementById('pw1').value;
201 //length of the password
202 var pwlength=(pw.length);
207 //use of numbers in the password
208 var numnumeric = pw.replace (/[0-9]/g, "");
209 var numeric=(pw.length - numnumeric.length);
213 //use of symbols in the password
214 var symbols = pw.replace (/\W/g, "");
215 var numsymbols=(pw.length - symbols.length);
219 //use of uppercase in the password
220 var numupper = pw.replace (/[A-Z]/g, "");
221 var upper=(pw.length - numupper.length);
226 var pwstrength=((pwlength*10)-20) + (numeric*10) + (numsymbols*15) + (upper*10);
228 // make sure we're give a value between 0 and 100
229 if ( pwstrength < 0 ) {
233 if ( pwstrength > 100 ) {
237 var mymeter=document.getElementById('pwmeter');
238 mymeter.value = pwstrength;
243 function checkPasswords()
245 var pw1=document.getElementById('pw1').value;
246 var pw2=document.getElementById('pw2').value;
247 var ok=document.documentElement.getButton("accept");
249 var oldpwbox = document.getElementById("oldpw");
251 var initpw = oldpwbox.getAttribute("inited");
253 if (initpw == "empty" && pw1 == "") {
254 // The token has already been initialized, therefore this dialog
255 // was called with the intention to change the password.
256 // The token currently uses an empty password.
257 // We will not allow changing the password from empty to empty.
258 ok.setAttribute("disabled","true");
264 ok.setAttribute("disabled","false");
267 ok.setAttribute("disabled","true");