toolkit/mozapps/preferences/changemp.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 // -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
michael@0 2
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 const nsPK11TokenDB = "@mozilla.org/security/pk11tokendb;1";
michael@0 8 const nsIPK11TokenDB = Components.interfaces.nsIPK11TokenDB;
michael@0 9 const nsIDialogParamBlock = Components.interfaces.nsIDialogParamBlock;
michael@0 10 const nsPKCS11ModuleDB = "@mozilla.org/security/pkcs11moduledb;1";
michael@0 11 const nsIPKCS11ModuleDB = Components.interfaces.nsIPKCS11ModuleDB;
michael@0 12 const nsIPKCS11Slot = Components.interfaces.nsIPKCS11Slot;
michael@0 13 const nsIPK11Token = Components.interfaces.nsIPK11Token;
michael@0 14
michael@0 15
michael@0 16 var params;
michael@0 17 var tokenName="";
michael@0 18 var pw1;
michael@0 19
michael@0 20 function init()
michael@0 21 {
michael@0 22 pw1 = document.getElementById("pw1");
michael@0 23
michael@0 24 process();
michael@0 25 }
michael@0 26
michael@0 27
michael@0 28 function process()
michael@0 29 {
michael@0 30 var secmoddb = Components.classes[nsPKCS11ModuleDB].getService(nsIPKCS11ModuleDB);
michael@0 31 var bundle = document.getElementById("bundlePreferences");
michael@0 32
michael@0 33 // If the token is unitialized, don't use the old password box.
michael@0 34 // Otherwise, do.
michael@0 35
michael@0 36 var slot = secmoddb.findSlotByName(tokenName);
michael@0 37 if (slot) {
michael@0 38 var oldpwbox = document.getElementById("oldpw");
michael@0 39 var msgBox = document.getElementById("message");
michael@0 40 var status = slot.status;
michael@0 41 if (status == nsIPKCS11Slot.SLOT_UNINITIALIZED
michael@0 42 || status == nsIPKCS11Slot.SLOT_READY) {
michael@0 43
michael@0 44 oldpwbox.setAttribute("hidden", "true");
michael@0 45 msgBox.setAttribute("value", bundle.getString("password_not_set"));
michael@0 46 msgBox.setAttribute("hidden", "false");
michael@0 47
michael@0 48 if (status == nsIPKCS11Slot.SLOT_READY) {
michael@0 49 oldpwbox.setAttribute("inited", "empty");
michael@0 50 } else {
michael@0 51 oldpwbox.setAttribute("inited", "true");
michael@0 52 }
michael@0 53
michael@0 54 // Select first password field
michael@0 55 document.getElementById('pw1').focus();
michael@0 56
michael@0 57 } else {
michael@0 58 // Select old password field
michael@0 59 oldpwbox.setAttribute("hidden", "false");
michael@0 60 msgBox.setAttribute("hidden", "true");
michael@0 61 oldpwbox.setAttribute("inited", "false");
michael@0 62 oldpwbox.focus();
michael@0 63 }
michael@0 64 }
michael@0 65
michael@0 66 if (params) {
michael@0 67 // Return value 0 means "canceled"
michael@0 68 params.SetInt(1, 0);
michael@0 69 }
michael@0 70
michael@0 71 checkPasswords();
michael@0 72 }
michael@0 73
michael@0 74 function setPassword()
michael@0 75 {
michael@0 76 var pk11db = Components.classes[nsPK11TokenDB].getService(nsIPK11TokenDB);
michael@0 77 var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
michael@0 78 .getService(Components.interfaces.nsIPromptService);
michael@0 79 var token = pk11db.findTokenByName(tokenName);
michael@0 80 dump("*** TOKEN!!!! (name = |" + token + "|\n");
michael@0 81
michael@0 82 var oldpwbox = document.getElementById("oldpw");
michael@0 83 var initpw = oldpwbox.getAttribute("inited");
michael@0 84 var bundle = document.getElementById("bundlePreferences");
michael@0 85
michael@0 86 var success = false;
michael@0 87
michael@0 88 if (initpw == "false" || initpw == "empty") {
michael@0 89 try {
michael@0 90 var oldpw = "";
michael@0 91 var passok = 0;
michael@0 92
michael@0 93 if (initpw == "empty") {
michael@0 94 passok = 1;
michael@0 95 } else {
michael@0 96 oldpw = oldpwbox.value;
michael@0 97 passok = token.checkPassword(oldpw);
michael@0 98 }
michael@0 99
michael@0 100 if (passok) {
michael@0 101 if (initpw == "empty" && pw1.value == "") {
michael@0 102 // This makes no sense that we arrive here,
michael@0 103 // we reached a case that should have been prevented by checkPasswords.
michael@0 104 } else {
michael@0 105 if (pw1.value == "") {
michael@0 106 var secmoddb = Components.classes[nsPKCS11ModuleDB].getService(nsIPKCS11ModuleDB);
michael@0 107 if (secmoddb.isFIPSEnabled) {
michael@0 108 // empty passwords are not allowed in FIPS mode
michael@0 109 promptService.alert(window,
michael@0 110 bundle.getString("pw_change_failed_title"),
michael@0 111 bundle.getString("pw_change2empty_in_fips_mode"));
michael@0 112 passok = 0;
michael@0 113 }
michael@0 114 }
michael@0 115 if (passok) {
michael@0 116 token.changePassword(oldpw, pw1.value);
michael@0 117 if (pw1.value == "") {
michael@0 118 promptService.alert(window,
michael@0 119 bundle.getString("pw_change_success_title"),
michael@0 120 bundle.getString("pw_erased_ok")
michael@0 121 + " " + bundle.getString("pw_empty_warning"));
michael@0 122 } else {
michael@0 123 promptService.alert(window,
michael@0 124 bundle.getString("pw_change_success_title"),
michael@0 125 bundle.getString("pw_change_ok"));
michael@0 126 }
michael@0 127 success = true;
michael@0 128 }
michael@0 129 }
michael@0 130 } else {
michael@0 131 oldpwbox.focus();
michael@0 132 oldpwbox.setAttribute("value", "");
michael@0 133 promptService.alert(window,
michael@0 134 bundle.getString("pw_change_failed_title"),
michael@0 135 bundle.getString("incorrect_pw"));
michael@0 136 }
michael@0 137 } catch (e) {
michael@0 138 promptService.alert(window,
michael@0 139 bundle.getString("pw_change_failed_title"),
michael@0 140 bundle.getString("failed_pw_change"));
michael@0 141 }
michael@0 142 } else {
michael@0 143 token.initPassword(pw1.value);
michael@0 144 if (pw1.value == "") {
michael@0 145 promptService.alert(window,
michael@0 146 bundle.getString("pw_change_success_title"),
michael@0 147 bundle.getString("pw_not_wanted")
michael@0 148 + " " + bundle.getString("pw_empty_warning"));
michael@0 149 }
michael@0 150 success = true;
michael@0 151 }
michael@0 152
michael@0 153 // Terminate dialog
michael@0 154 if (success)
michael@0 155 window.close();
michael@0 156 }
michael@0 157
michael@0 158 function setPasswordStrength()
michael@0 159 {
michael@0 160 // Here is how we weigh the quality of the password
michael@0 161 // number of characters
michael@0 162 // numbers
michael@0 163 // non-alpha-numeric chars
michael@0 164 // upper and lower case characters
michael@0 165
michael@0 166 var pw=document.getElementById('pw1').value;
michael@0 167
michael@0 168 //length of the password
michael@0 169 var pwlength=(pw.length);
michael@0 170 if (pwlength>5)
michael@0 171 pwlength=5;
michael@0 172
michael@0 173
michael@0 174 //use of numbers in the password
michael@0 175 var numnumeric = pw.replace (/[0-9]/g, "");
michael@0 176 var numeric=(pw.length - numnumeric.length);
michael@0 177 if (numeric>3)
michael@0 178 numeric=3;
michael@0 179
michael@0 180 //use of symbols in the password
michael@0 181 var symbols = pw.replace (/\W/g, "");
michael@0 182 var numsymbols=(pw.length - symbols.length);
michael@0 183 if (numsymbols>3)
michael@0 184 numsymbols=3;
michael@0 185
michael@0 186 //use of uppercase in the password
michael@0 187 var numupper = pw.replace (/[A-Z]/g, "");
michael@0 188 var upper=(pw.length - numupper.length);
michael@0 189 if (upper>3)
michael@0 190 upper=3;
michael@0 191
michael@0 192
michael@0 193 var pwstrength=((pwlength*10)-20) + (numeric*10) + (numsymbols*15) + (upper*10);
michael@0 194
michael@0 195 // make sure we're give a value between 0 and 100
michael@0 196 if ( pwstrength < 0 ) {
michael@0 197 pwstrength = 0;
michael@0 198 }
michael@0 199
michael@0 200 if ( pwstrength > 100 ) {
michael@0 201 pwstrength = 100;
michael@0 202 }
michael@0 203
michael@0 204 var mymeter=document.getElementById('pwmeter');
michael@0 205 mymeter.value = pwstrength;
michael@0 206
michael@0 207 return;
michael@0 208 }
michael@0 209
michael@0 210 function checkPasswords()
michael@0 211 {
michael@0 212 var pw1=document.getElementById('pw1').value;
michael@0 213 var pw2=document.getElementById('pw2').value;
michael@0 214 var ok=document.documentElement.getButton("accept");
michael@0 215
michael@0 216 var oldpwbox = document.getElementById("oldpw");
michael@0 217 if (oldpwbox) {
michael@0 218 var initpw = oldpwbox.getAttribute("inited");
michael@0 219
michael@0 220 if (initpw == "empty" && pw1 == "") {
michael@0 221 // The token has already been initialized, therefore this dialog
michael@0 222 // was called with the intention to change the password.
michael@0 223 // The token currently uses an empty password.
michael@0 224 // We will not allow changing the password from empty to empty.
michael@0 225 ok.setAttribute("disabled","true");
michael@0 226 return;
michael@0 227 }
michael@0 228 }
michael@0 229
michael@0 230 if (pw1 == pw2){
michael@0 231 ok.setAttribute("disabled","false");
michael@0 232 } else
michael@0 233 {
michael@0 234 ok.setAttribute("disabled","true");
michael@0 235 }
michael@0 236
michael@0 237 }

mercurial