toolkit/mozapps/preferences/changemp.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/mozapps/preferences/changemp.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,237 @@
     1.4 +// -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 +
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +const nsPK11TokenDB = "@mozilla.org/security/pk11tokendb;1";
    1.11 +const nsIPK11TokenDB = Components.interfaces.nsIPK11TokenDB;
    1.12 +const nsIDialogParamBlock = Components.interfaces.nsIDialogParamBlock;
    1.13 +const nsPKCS11ModuleDB = "@mozilla.org/security/pkcs11moduledb;1";
    1.14 +const nsIPKCS11ModuleDB = Components.interfaces.nsIPKCS11ModuleDB;
    1.15 +const nsIPKCS11Slot = Components.interfaces.nsIPKCS11Slot;
    1.16 +const nsIPK11Token = Components.interfaces.nsIPK11Token;
    1.17 +
    1.18 +
    1.19 +var params;
    1.20 +var tokenName="";
    1.21 +var pw1;
    1.22 +
    1.23 +function init()
    1.24 +{
    1.25 +  pw1 = document.getElementById("pw1");
    1.26 +	 	 
    1.27 +  process();
    1.28 +}
    1.29 +
    1.30 +
    1.31 +function process()
    1.32 +{
    1.33 +   var secmoddb = Components.classes[nsPKCS11ModuleDB].getService(nsIPKCS11ModuleDB);
    1.34 +   var bundle = document.getElementById("bundlePreferences");
    1.35 +
    1.36 +   // If the token is unitialized, don't use the old password box.
    1.37 +   // Otherwise, do.
    1.38 +
    1.39 +   var slot = secmoddb.findSlotByName(tokenName);
    1.40 +   if (slot) {
    1.41 +     var oldpwbox = document.getElementById("oldpw");
    1.42 +     var msgBox = document.getElementById("message");
    1.43 +     var status = slot.status;
    1.44 +     if (status == nsIPKCS11Slot.SLOT_UNINITIALIZED
    1.45 +         || status == nsIPKCS11Slot.SLOT_READY) {
    1.46 +      
    1.47 +       oldpwbox.setAttribute("hidden", "true");
    1.48 +       msgBox.setAttribute("value", bundle.getString("password_not_set")); 
    1.49 +       msgBox.setAttribute("hidden", "false");
    1.50 +
    1.51 +       if (status == nsIPKCS11Slot.SLOT_READY) {
    1.52 +         oldpwbox.setAttribute("inited", "empty");
    1.53 +       } else {
    1.54 +         oldpwbox.setAttribute("inited", "true");
    1.55 +       }
    1.56 +      
    1.57 +       // Select first password field
    1.58 +       document.getElementById('pw1').focus();
    1.59 +    
    1.60 +     } else {
    1.61 +       // Select old password field
    1.62 +       oldpwbox.setAttribute("hidden", "false");
    1.63 +       msgBox.setAttribute("hidden", "true");
    1.64 +       oldpwbox.setAttribute("inited", "false");
    1.65 +       oldpwbox.focus();
    1.66 +     }
    1.67 +   }
    1.68 +
    1.69 +  if (params) {
    1.70 +    // Return value 0 means "canceled"
    1.71 +    params.SetInt(1, 0);
    1.72 +  }
    1.73 +  
    1.74 +  checkPasswords();
    1.75 +}
    1.76 +
    1.77 +function setPassword()
    1.78 +{
    1.79 +  var pk11db = Components.classes[nsPK11TokenDB].getService(nsIPK11TokenDB);
    1.80 +  var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
    1.81 +                                .getService(Components.interfaces.nsIPromptService);
    1.82 +  var token = pk11db.findTokenByName(tokenName);
    1.83 +  dump("*** TOKEN!!!! (name = |" + token + "|\n");
    1.84 +
    1.85 +  var oldpwbox = document.getElementById("oldpw");
    1.86 +  var initpw = oldpwbox.getAttribute("inited");
    1.87 +  var bundle = document.getElementById("bundlePreferences");
    1.88 +  
    1.89 +  var success = false;
    1.90 +  
    1.91 +  if (initpw == "false" || initpw == "empty") {
    1.92 +    try {
    1.93 +      var oldpw = "";
    1.94 +      var passok = 0;
    1.95 +      
    1.96 +      if (initpw == "empty") {
    1.97 +        passok = 1;
    1.98 +      } else {
    1.99 +        oldpw = oldpwbox.value;
   1.100 +        passok = token.checkPassword(oldpw);
   1.101 +      }
   1.102 +      
   1.103 +      if (passok) {
   1.104 +        if (initpw == "empty" && pw1.value == "") {
   1.105 +          // This makes no sense that we arrive here, 
   1.106 +          // we reached a case that should have been prevented by checkPasswords.
   1.107 +        } else {
   1.108 +          if (pw1.value == "") {
   1.109 +            var secmoddb = Components.classes[nsPKCS11ModuleDB].getService(nsIPKCS11ModuleDB);
   1.110 +            if (secmoddb.isFIPSEnabled) {
   1.111 +              // empty passwords are not allowed in FIPS mode
   1.112 +              promptService.alert(window,
   1.113 +                                  bundle.getString("pw_change_failed_title"),
   1.114 +                                  bundle.getString("pw_change2empty_in_fips_mode"));
   1.115 +              passok = 0;
   1.116 +            }
   1.117 +          }
   1.118 +          if (passok) {
   1.119 +            token.changePassword(oldpw, pw1.value);
   1.120 +            if (pw1.value == "") {
   1.121 +              promptService.alert(window,
   1.122 +                                  bundle.getString("pw_change_success_title"),
   1.123 +                                  bundle.getString("pw_erased_ok") 
   1.124 +                                  + " " + bundle.getString("pw_empty_warning"));
   1.125 +            } else {
   1.126 +              promptService.alert(window,
   1.127 +                                  bundle.getString("pw_change_success_title"),
   1.128 +                                  bundle.getString("pw_change_ok"));
   1.129 +            }
   1.130 +            success = true;
   1.131 +          }
   1.132 +        }
   1.133 +      } else {
   1.134 +        oldpwbox.focus();
   1.135 +        oldpwbox.setAttribute("value", "");
   1.136 +        promptService.alert(window,
   1.137 +                            bundle.getString("pw_change_failed_title"),
   1.138 +                            bundle.getString("incorrect_pw"));
   1.139 +      }
   1.140 +    } catch (e) {
   1.141 +      promptService.alert(window,
   1.142 +                          bundle.getString("pw_change_failed_title"),
   1.143 +                          bundle.getString("failed_pw_change"));
   1.144 +    }
   1.145 +  } else {
   1.146 +    token.initPassword(pw1.value);
   1.147 +    if (pw1.value == "") {
   1.148 +      promptService.alert(window,
   1.149 +                          bundle.getString("pw_change_success_title"),
   1.150 +                          bundle.getString("pw_not_wanted")
   1.151 +                          + " " + bundle.getString("pw_empty_warning"));
   1.152 +    }
   1.153 +    success = true;
   1.154 +  }
   1.155 +
   1.156 +  // Terminate dialog
   1.157 +  if (success)
   1.158 +    window.close();
   1.159 +}
   1.160 +
   1.161 +function setPasswordStrength()
   1.162 +{
   1.163 +// Here is how we weigh the quality of the password
   1.164 +// number of characters
   1.165 +// numbers
   1.166 +// non-alpha-numeric chars
   1.167 +// upper and lower case characters
   1.168 +
   1.169 +  var pw=document.getElementById('pw1').value;
   1.170 +
   1.171 +//length of the password
   1.172 +  var pwlength=(pw.length);
   1.173 +  if (pwlength>5)
   1.174 +    pwlength=5;
   1.175 +
   1.176 +
   1.177 +//use of numbers in the password
   1.178 +  var numnumeric = pw.replace (/[0-9]/g, "");
   1.179 +  var numeric=(pw.length - numnumeric.length);
   1.180 +  if (numeric>3)
   1.181 +    numeric=3;
   1.182 +
   1.183 +//use of symbols in the password
   1.184 +  var symbols = pw.replace (/\W/g, "");
   1.185 +  var numsymbols=(pw.length - symbols.length);
   1.186 +  if (numsymbols>3)
   1.187 +    numsymbols=3;
   1.188 +
   1.189 +//use of uppercase in the password
   1.190 +  var numupper = pw.replace (/[A-Z]/g, "");
   1.191 +  var upper=(pw.length - numupper.length);
   1.192 +  if (upper>3)
   1.193 +    upper=3;
   1.194 +
   1.195 +
   1.196 +  var pwstrength=((pwlength*10)-20) + (numeric*10) + (numsymbols*15) + (upper*10);
   1.197 +
   1.198 +  // make sure we're give a value between 0 and 100
   1.199 +  if ( pwstrength < 0 ) {
   1.200 +    pwstrength = 0;
   1.201 +  }
   1.202 +  
   1.203 +  if ( pwstrength > 100 ) {
   1.204 +    pwstrength = 100;
   1.205 +  }
   1.206 +
   1.207 +  var mymeter=document.getElementById('pwmeter');
   1.208 +  mymeter.value = pwstrength;
   1.209 +
   1.210 +  return;
   1.211 +}
   1.212 +
   1.213 +function checkPasswords()
   1.214 +{
   1.215 +  var pw1=document.getElementById('pw1').value;
   1.216 +  var pw2=document.getElementById('pw2').value;
   1.217 +  var ok=document.documentElement.getButton("accept");
   1.218 +
   1.219 +  var oldpwbox = document.getElementById("oldpw");
   1.220 +  if (oldpwbox) {
   1.221 +    var initpw = oldpwbox.getAttribute("inited");
   1.222 +
   1.223 +    if (initpw == "empty" && pw1 == "") {
   1.224 +      // The token has already been initialized, therefore this dialog
   1.225 +      // was called with the intention to change the password.
   1.226 +      // The token currently uses an empty password.
   1.227 +      // We will not allow changing the password from empty to empty.
   1.228 +      ok.setAttribute("disabled","true");
   1.229 +      return;
   1.230 +    }
   1.231 +  }
   1.232 +
   1.233 +  if (pw1 == pw2){
   1.234 +    ok.setAttribute("disabled","false");
   1.235 +  } else
   1.236 +  {
   1.237 +    ok.setAttribute("disabled","true");
   1.238 +  }
   1.239 +
   1.240 +}

mercurial